Skip to content

Commit bb93895

Browse files
authored
Extract monsters and NPCs into a spawns XML file (jo3bingham#42)
1 parent 3628e3f commit bb93895

1 file changed

Lines changed: 81 additions & 1 deletion

File tree

Apps/Extract/Program.cs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Program
2020
private static readonly HashSet<ulong> _knownPositions = new HashSet<ulong>();
2121
private static readonly HashSet<uint> _knownMonsterIds = new HashSet<uint>();
2222
private static readonly HashSet<uint> _knownNpcIds = new HashSet<uint>();
23+
private static readonly HashSet<uint> _knownSpawnIds = new HashSet<uint>();
2324

2425
private static readonly HashSet<uint> _ignoreIds = new HashSet<uint>();
2526

@@ -37,6 +38,8 @@ class Program
3738
private static StreamWriter _monsterFile;
3839
private static StreamWriter _npcFile;
3940

41+
private static XmlWriter _spawnsXml;
42+
4043
private static Logger.LogLevel _logLevel = Logger.LogLevel.Error;
4144

4245
private static Logger.LogOutput _logOutput = Logger.LogOutput.Console;
@@ -54,6 +57,7 @@ class Program
5457
private static bool _extractMapData = false;
5558
private static bool _extractMonsterData = false;
5659
private static bool _extractNpcData = false;
60+
private static bool _extractSpawns = false;
5761

5862
static bool ParseArgs(string[] args)
5963
{
@@ -89,6 +93,11 @@ static bool ParseArgs(string[] args)
8993
_extractNpcData = true;
9094
}
9195
break;
96+
case "--spawns":
97+
{
98+
_extractSpawns = true;
99+
}
100+
break;
92101
case "-h":
93102
case "--help":
94103
{
@@ -124,6 +133,7 @@ static bool ParseArgs(string[] args)
124133
Console.WriteLine("--map: Used for extracting map data to the OTBM format.\n");
125134
Console.WriteLine("--monsters: Used for extracting monster information to monsters.txt.\n");
126135
Console.WriteLine("--npcs: Used for extracting npc information to npcs.txt.\n");
136+
Console.WriteLine("--spawns: Used for extracting spawns to spawns.xml.\n");
127137
}
128138
return false;
129139
default:
@@ -208,7 +218,8 @@ static void Main(string[] args)
208218
return;
209219
}
210220

211-
if (!_extractItemData && !_extractLookItemText && !_extractMapData && !_extractMonsterData && !_extractNpcData)
221+
if (!_extractItemData && !_extractLookItemText && !_extractMapData &&
222+
!_extractMonsterData && !_extractNpcData && !_extractSpawns)
212223
{
213224
Console.WriteLine("You must specificy at least one extraction option.");
214225
Console.WriteLine("Use -h, or --help, for help.");
@@ -288,6 +299,18 @@ static void Main(string[] args)
288299
_npcFile = new StreamWriter("npcs.txt", true);
289300
}
290301

302+
if (_extractSpawns)
303+
{
304+
Console.WriteLine("Extracting spawns...");
305+
var settings = new XmlWriterSettings
306+
{
307+
Indent = true
308+
};
309+
_spawnsXml = XmlWriter.Create("spawns.xml", settings);
310+
_spawnsXml.WriteStartDocument();
311+
_spawnsXml.WriteStartElement("spawns");
312+
}
313+
291314
ExtractRecordings(filenames);
292315

293316
if (_itemFile != null)
@@ -310,6 +333,12 @@ static void Main(string[] args)
310333
_npcFile.Close();
311334
}
312335

336+
if (_spawnsXml != null)
337+
{
338+
_spawnsXml.WriteEndDocument();
339+
_spawnsXml.Close();
340+
}
341+
313342
Console.WriteLine("Extraction complete");
314343
}
315344
catch (Exception ex)
@@ -430,6 +459,32 @@ static void LoadXML(string filename)
430459
}
431460
}
432461

462+
private static void AddSpawnEntry(OXGaming.TibiaAPI.Creatures.Creature creature)
463+
{
464+
if ((creature.Type != OXGaming.TibiaAPI.Constants.CreatureType.Monster &&
465+
creature.Type != OXGaming.TibiaAPI.Constants.CreatureType.Npc) ||
466+
_knownSpawnIds.Contains(creature.Id))
467+
{
468+
return;
469+
}
470+
471+
_knownSpawnIds.Add(creature.Id);
472+
473+
_spawnsXml.WriteStartElement("spawn");
474+
_spawnsXml.WriteAttributeString("centerx", creature.Position.X.ToString());
475+
_spawnsXml.WriteAttributeString("centery", creature.Position.Y.ToString());
476+
_spawnsXml.WriteAttributeString("centerz", creature.Position.Z.ToString());
477+
_spawnsXml.WriteAttributeString("radius", "5");
478+
_spawnsXml.WriteStartElement(creature.Type == OXGaming.TibiaAPI.Constants.CreatureType.Monster ? "monster" : "npc");
479+
_spawnsXml.WriteAttributeString("name", creature.Name);
480+
_spawnsXml.WriteAttributeString("x", "0");
481+
_spawnsXml.WriteAttributeString("y", "0");
482+
_spawnsXml.WriteAttributeString("z", "0");
483+
_spawnsXml.WriteAttributeString("spawntime", "60");
484+
_spawnsXml.WriteEndElement();
485+
_spawnsXml.WriteEndElement();
486+
}
487+
433488
private static void ExtractRecordings(List<string> filenames)
434489
{
435490
Console.WriteLine($"Extracting data from {filenames.Count} recording(s)...");
@@ -685,6 +740,11 @@ private static bool Connection_OnReceivedServerFullMapPacket(Packet packet)
685740
continue;
686741
}
687742

743+
if (_extractSpawns)
744+
{
745+
AddSpawnEntry(creature);
746+
}
747+
688748
if (_extractMonsterData && creature.Type == OXGaming.TibiaAPI.Constants.CreatureType.Monster && !_knownMonsterIds.Contains(creature.Id))
689749
{
690750
_monsterFile.WriteLine($"{creature.Name} {creature.Position}");
@@ -707,6 +767,11 @@ private static bool Connection_OnReceivedServerCreatureDataPacket(Packet packet)
707767
var p = (CreatureData)packet;
708768
if (p.Creature != null)
709769
{
770+
if (_extractSpawns)
771+
{
772+
AddSpawnEntry(p.Creature);
773+
}
774+
710775
if (_extractMonsterData && p.Creature.Type == OXGaming.TibiaAPI.Constants.CreatureType.Monster && !_knownMonsterIds.Contains(p.Creature.Id))
711776
{
712777
_monsterFile.WriteLine($"{p.Creature.Name} {p.Creature.Position}");
@@ -730,6 +795,11 @@ private static bool Connection_OnReceivedServerChangeOnMapPacket(Packet packet)
730795
}
731796
else if (p.Creature != null)
732797
{
798+
if (_extractSpawns)
799+
{
800+
AddSpawnEntry(p.Creature);
801+
}
802+
733803
if (_extractMonsterData && p.Creature.Type == OXGaming.TibiaAPI.Constants.CreatureType.Monster && !_knownMonsterIds.Contains(p.Creature.Id))
734804
{
735805
_monsterFile.WriteLine($"{p.Creature.Name} {p.Creature.Position}");
@@ -753,6 +823,11 @@ private static bool Connection_OnReceivedServerCreateOnMapPacket(Packet packet)
753823
}
754824
else if (p.Creature != null)
755825
{
826+
if (_extractSpawns)
827+
{
828+
AddSpawnEntry(p.Creature);
829+
}
830+
756831
if (_extractMonsterData && p.Creature.Type == OXGaming.TibiaAPI.Constants.CreatureType.Monster && !_knownMonsterIds.Contains(p.Creature.Id))
757832
{
758833
_monsterFile.WriteLine($"{p.Creature.Name} {p.Creature.Position}");
@@ -800,6 +875,11 @@ static bool Connection_OnReceivedMapPacket(Packet packet)
800875
continue;
801876
}
802877

878+
if (_extractSpawns)
879+
{
880+
AddSpawnEntry(creature);
881+
}
882+
803883
if (_extractMonsterData && creature.Type == OXGaming.TibiaAPI.Constants.CreatureType.Monster && !_knownMonsterIds.Contains(creature.Id))
804884
{
805885
_monsterFile.WriteLine($"{creature.Name} {creature.Position}");

0 commit comments

Comments
 (0)