Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Fixed Counting bug with invasions
Browse files Browse the repository at this point in the history
Tried to Limit Monsters in the Custom invasions (should obey maxspawns now)
Updated code to support new Biomes and events

Currently Broken

Custom Loot & OnDeath
  • Loading branch information
Pychnight committed Jul 9, 2015
1 parent a8a7377 commit 9c0594e
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 38 deletions.
62 changes: 52 additions & 10 deletions CustomNPC/CustomNPC/Biomes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,85 @@ public enum BiomeTypes
Grass = 1 << 0,

/// <summary>
/// Player is in zoneEvil
/// Player is in Corruption
/// </summary>
Corruption = 1 << 1,

/// <summary>
/// Player is in Crimsion
/// </summary>
Crimsion = 1 << 2,

/// <summary>
/// Player is in Crimsion
/// </summary>
Desert = 1 << 3,

/// <summary>
/// Player is in zoneDungeon
/// </summary>
Dungeon = 1 << 2,
Dungeon = 1 << 4,

/// <summary>
/// Player is in Glowshroom
/// </summary>
Glowshroom = 1 << 5,

/// <summary>
/// Player is in zoneMeteor
/// </summary>
Meteor = 1 << 3,
Meteor = 1 << 6,

/// <summary>
/// Player is in zoneHoly
/// </summary>
Holy = 1 << 4,
Holy = 1 << 7,

/// <summary>
/// Player is in zoneJungle
/// </summary>
Jungle = 1 << 5,
Jungle = 1 << 8,

/// <summary>
/// Player is in Peace Candle
/// </summary>
PeaceCandle = 1 << 9,

/// <summary>
/// Player is in zoneSnow
/// </summary>
Snow = 1 << 6,
Snow = 1 << 10,

/// <summary>
/// Player is in TowerNebula
/// </summary>
TowerNebula = 1 << 11,

/// <summary>
/// Player is in zoneBlood
/// Player is in TowerSolar
/// </summary>
Blood = 1 << 7,
TowerSolar = 1 << 12,

/// <summary>
/// Player is in zoneCandle
/// Player is in TowerStardust
/// </summary>
Candle = 1 << 8
TowerStardust = 1 << 13,

/// <summary>
/// Player is in TowerVortex
/// </summary>
TowerVortex = 1 << 14,

/// <summary>
/// Player is in UndergroundDesert
/// </summary>
UndergroundDesert = 1 << 15,

/// <summary>
/// Player is in zoneWaterCandle
/// </summary>
WaterCandle = 1 << 16


}
}
1 change: 1 addition & 0 deletions CustomNPC/CustomNPC/CustomNPC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="Plugins\PluginManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ShotTile.cs" />
<Compile Include="TaeirUtil.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
6 changes: 5 additions & 1 deletion CustomNPC/CustomNPC/CustomNPCConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public static CustomNPCConfig Read(Stream stream)
{
using (var sr = new StreamReader(stream))
{
var serializerSettings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace };
var cf = JsonConvert.DeserializeObject<CustomNPCConfig>(sr.ReadToEnd());

if (ConfigRead != null)
ConfigRead(cf);
return cf;
Expand All @@ -60,14 +62,16 @@ public void Write(string path)
public void Write(Stream stream)
{
//minion spawns
List<SpawnMinion> SpawnMinions1 = new List<SpawnMinion>();

List<SpawnMinion> SpawnMinions1 = new List<SpawnMinion>();
SpawnMinion SpawnMinion1 = new SpawnMinion("customnpc id", 100, BiomeTypes.None, SpawnConditions.None, false, true);
SpawnMinion SpawnMinion2 = new SpawnMinion("customnpc id2", 100, BiomeTypes.None, SpawnConditions.None, false, true);
SpawnMinions1.Add(SpawnMinion1);
SpawnMinions1.Add(SpawnMinion2);

//SpawnGroups
SpawnsGroups SpawnGroup1 = new SpawnsGroups(true, true, SpawnMinions1, 100);


//Waves
List<Waves> Waves1 = new List<Waves>();
Expand Down
3 changes: 1 addition & 2 deletions CustomNPC/CustomNPC/CustomNPCInvasion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ private void InvasionTimer_Elapsed(object sender, ElapsedEventArgs e)
{
return;
}

int spawnFails = 0;
int spawnsThisWave = 0;

Expand Down Expand Up @@ -134,7 +133,7 @@ private void InvasionTimer_Elapsed(object sender, ElapsedEventArgs e)
//Try max 3 times. Since every try checks 50 positions around the player to spawn the mob,
//3 tries means a maximum of 150 spawn attempts.
for (int i = 0; mobid == -1 && i < 3; i++)
{
{
mobid = NPCManager.SpawnMobAroundPlayer(player, npcdef);
}

Expand Down
42 changes: 38 additions & 4 deletions CustomNPC/CustomNPC/CustomNPCPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace CustomNPC
{
[ApiVersion(1, 17)]
[ApiVersion(1, 18)]
public class CustomNPCPlugin : TerrariaPlugin
{
internal Random rand = new Random();
Expand Down Expand Up @@ -113,6 +113,7 @@ private Assembly PluginDomain_OnAssemblyResolve(object sender, ResolveEventArgs
private void OnInitialize(EventArgs args)
{
Commands.ChatCommands.Add(new Command("customnpc.spawn", CommandSpawnNPC, "csm"));
Commands.ChatCommands.Add(new Command("customnpc.invadereload", CommandIReload, "csminvadereload"));
Commands.ChatCommands.Add(new Command("customnpc.list", CommandListNPCS, "csmlist", "clist"));
Commands.ChatCommands.Add(new Command("customnpc.invade", CommandInvade, "csminvade", "cinvade"));
Commands.ChatCommands.Add(new Command("customnpc.info", CommandNPCInfo, "csminfo", "cinfo"));
Expand Down Expand Up @@ -648,7 +649,7 @@ private void ProjectileCheck()
if (!projectile.projectileCheckCollision || Collision.CanHit(player.TPlayer.position, player.TPlayer.bodyFrame.Width, player.TPlayer.bodyFrame.Height, obj.mainNPC.position, obj.mainNPC.width, obj.mainNPC.height))
{
//Make sure distance isn't further then what tshock allows
float currDistance = Vector2.DistanceSquared(player.TPlayer.position, obj.mainNPC.center());
float currDistance = Vector2.DistanceSquared(player.TPlayer.position, obj.mainNPC.Center);

//Distance^2 < 4194304 is the same as Distance < 2048, but faster
if (currDistance < 4194304)
Expand Down Expand Up @@ -737,13 +738,13 @@ private void FireProjectile(TSPlayer target, CustomNPCVars origin, CustomNPCProj
private Vector2 GetStartPosition(CustomNPCVars origin, ShotTile shottile)
{
Vector2 offset = new Vector2(shottile.X, shottile.Y);
return origin.mainNPC.center() + offset;
return origin.mainNPC.Center + offset;
}

//calculates the x y speed required angle
private Vector2 CalculateSpeed(Vector2 start, TSPlayer target)
{
Vector2 targetCenter = target.TPlayer.center();
Vector2 targetCenter = target.TPlayer.Center;

float dirX = targetCenter.X - start.X;
float dirY = targetCenter.Y - start.Y;
Expand Down Expand Up @@ -809,6 +810,39 @@ private void SetupConfig()
}
}

/// <summary>
/// Reload Config File <type>
/// </summary>
/// <param name="args"></param>
private void CommandIReload(CommandArgs args)
{
if (NPCManager.CustomNPCInvasion.invasionStarted)
{
NPCManager.CustomNPCInvasion.StopInvasion();
}

try
{
if (File.Exists(filepath))
{
ConfigObj = new CustomNPCConfig();
ConfigObj = CustomNPCConfig.Read(filepath);
return;
}
else
{
TShock.Log.ConsoleError("Config not found. Creating new one");
ConfigObj.Write(filepath);
return;
}
}
catch (Exception ex)
{
TShock.Log.ConsoleError(ex.Message);
return;
}
}

/// <summary>
/// Start custom npc invasion using /cinvade <type>
/// </summary>
Expand Down
10 changes: 5 additions & 5 deletions CustomNPC/CustomNPC/CustomNPCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public CustomNPCVars(CustomNPCDefinition customnpc, DateTime[] lastattemptedproj

public void markDead()
{
isDead = true;
if (isUncounted) return;
isDead = true;
if (isUncounted) return;

isUncounted = true;
if (!isClone && !isInvasion) customNPC.currSpawnsVar--;
isUncounted = true;
if (!isClone) customNPC.currSpawnsVar--;
}

private void updateCustomAI()
Expand Down Expand Up @@ -90,7 +90,7 @@ private void CustomTransform()
mainNPC.lavaImmune = customNPC.lavaImmune;
mainNPC.noGravity = customNPC.noGravity;
mainNPC.noTileCollide = customNPC.noTileCollide;
//mainNPC.boss = customNPC.isBoss;
mainNPC.boss = customNPC.isBoss;

mainNPC.defense = customNPC.customDefense;
mainNPC.defDefense = customNPC.customDefense;
Expand Down
46 changes: 35 additions & 11 deletions CustomNPC/CustomNPC/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,53 @@ public static BiomeTypes GetCurrentBiomes(this TSPlayer player)
{
BiomeTypes biome = BiomeTypes.None;

if (player.TPlayer.zoneEvil)
if (player.TPlayer.ZoneCorrupt)
biome |= BiomeTypes.Corruption;

if (player.TPlayer.zoneDungeon)
if (player.TPlayer.ZoneCrimson)
biome |= BiomeTypes.Crimsion;

if (player.TPlayer.ZoneDesert)
biome |= BiomeTypes.Desert;

if (player.TPlayer.ZoneDungeon)
biome |= BiomeTypes.Dungeon;

if (player.TPlayer.zoneMeteor)
biome |= BiomeTypes.Meteor;
if (player.TPlayer.ZoneGlowshroom)
biome |= BiomeTypes.Glowshroom;

if (player.TPlayer.zoneHoly)
if (player.TPlayer.ZoneHoly)
biome |= BiomeTypes.Holy;

if (player.TPlayer.zoneJungle)
if (player.TPlayer.ZoneJungle)
biome |= BiomeTypes.Jungle;

if (player.TPlayer.zoneSnow)
if (player.TPlayer.ZoneMeteor)
biome |= BiomeTypes.Meteor;

if (player.TPlayer.ZonePeaceCandle)
biome |= BiomeTypes.PeaceCandle;

if (player.TPlayer.ZoneSnow)
biome |= BiomeTypes.Snow;

if (player.TPlayer.zoneBlood)
biome |= BiomeTypes.Blood;
if (player.TPlayer.ZoneTowerNebula)
biome |= BiomeTypes.TowerNebula;

if (player.TPlayer.ZoneTowerSolar)
biome |= BiomeTypes.TowerSolar;

if (player.TPlayer.ZoneTowerStardust)
biome |= BiomeTypes.TowerStardust;

if (player.TPlayer.ZoneTowerVortex)
biome |= BiomeTypes.TowerVortex;

if (player.TPlayer.ZoneUndergroundDesert)
biome |= BiomeTypes.UndergroundDesert;

if (player.TPlayer.zoneCandle)
biome |= BiomeTypes.Candle;
if (player.TPlayer.ZoneWaterCandle)
biome |= BiomeTypes.WaterCandle;

if (biome == BiomeTypes.None)
{
Expand Down
38 changes: 33 additions & 5 deletions CustomNPC/CustomNPC/NPCManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ internal static bool CheckSpawnConditions(SpawnConditions conditions)
//Log.ConsoleInfo("Failed on Eclipse");
return false;
}
if (conditions.HasFlag(SpawnConditions.SnowMoon) && !Main.snowMoon)
{
//Log.ConsoleInfo("Failed on SnowMoon");
return false;
}
if (conditions.HasFlag(SpawnConditions.DayTime) && !Main.dayTime)
{
//Log.ConsoleInfo("Failed on DayTime");
Expand Down Expand Up @@ -203,6 +208,11 @@ internal static bool CheckSpawnConditions(SpawnConditions conditions)
//Log.ConsoleInfo("Failed on Raining");
return false;
}
if (conditions.HasFlag(SpawnConditions.SlimeRaining) && !Main.slimeRain)
{
//Log.ConsoleInfo("Failed on Slime Raining");
return false;
}
return true;
}

Expand Down Expand Up @@ -248,6 +258,17 @@ public static int SpawnNPCAroundNPC(int npcindex, ShotTile shottile, CustomNPCDe
return SpawnCustomNPC(x, y, customnpc);
}

public static int SpawnNPCAroundNPColdpos(int npcindex, ShotTile shottile, CustomNPCDefinition customnpc)
{
NPC npc = Main.npc[npcindex];
if (npc == null) return -1;

int x = (int)(npc.oldPosition.X + shottile.X);
int y = (int)(npc.oldPosition.Y + shottile.Y);

return SpawnCustomNPC(x, y, customnpc);
}

public static int SpawnMobAroundPlayer(TSPlayer player, CustomNPCDefinition definition)
{
const int SpawnSpaceX = 3;
Expand Down Expand Up @@ -516,9 +537,9 @@ public static int AliveCount(string customid)
{
if (obj == null) continue;

if (obj.customNPC.customID.Equals(customid, StringComparison.InvariantCultureIgnoreCase))
if (obj.customNPC.customID.Equals(customid, StringComparison.InvariantCultureIgnoreCase) && !obj.isDead)
{
count++;
count++;
}
}
return count;
Expand Down Expand Up @@ -692,11 +713,18 @@ private static void SpawnMinion(Rectangle spawnRegion, SpawnMinion minion, Custo

int mobid = -1;

//Try max attempts times. This gives attempts*50 spawn attempts at random positions.
for (int i = 0; mobid == -1 && i < attempts; i++)
if (npcdef.maxSpawns != -1 && npcdef.currSpawnsVar >= npcdef.maxSpawns)
{
mobid = NPCManager.SpawnMobAroundPlayer(player, npcdef);
npcdef.currSpawnsVar = NPCManager.AliveCount(npcdef.customID);
continue;
}

//Try max attempts times. This gives attempts*50 spawn attempts at random positions.
for (int i = 0; mobid == -1 && i < attempts; i++)
{
mobid = NPCManager.SpawnMobAroundPlayer(player, npcdef);
npcdef.currSpawnsVar++;
}

//Spawning failed :(
if (mobid == -1) continue;
Expand Down

0 comments on commit 9c0594e

Please sign in to comment.