forked from namreeb/PPather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNPCDatabase.cs
More file actions
197 lines (174 loc) · 4.02 KB
/
Copy pathNPCDatabase.cs
File metadata and controls
197 lines (174 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.Text;
using Glider.Common.Objects;
namespace Pather
{
public class NPCDatabase
{
public class NPC
{
public String name;
public GLocation location;
public int faction;
public GReaction reaction;
public override string ToString()
{
return String.Format(PPather.numberFormat, "{0}|{1},{2},{3}|{4}|{5}",
name, location.X, location.Y, location.Z, faction, reaction);
}
public bool FromString(string s)
{
char[] splitter = { '|' };
string[] sp = s.Split(splitter);
if (sp.Length < 4)
return false;
name = sp[0];
string locs = sp[1];
char[] splitter2 = { ',' };
string[] coords = locs.Split(splitter2);
if (coords.Length == 3)
{
float x = float.Parse(coords[0], PPather.numberFormat);
float y = float.Parse(coords[1], PPather.numberFormat);
float z = float.Parse(coords[2], PPather.numberFormat);
location = new GLocation(x, y, z);
}
else
return false;
faction = Int32.Parse(sp[2], PPather.numberFormat);
string rs = sp[3];
if (rs == "Friendly")
reaction = GReaction.Friendly;
if (rs == "Hostile")
reaction = GReaction.Hostile;
if (rs == "Neutral")
reaction = GReaction.Neutral;
if (rs == "Unknown")
reaction = GReaction.Unknown;
return true;
}
}
Dictionary<string, NPC> NPCs;
string continent = null;
string myFaction = "";
bool changed = false;
public void SetContinent(string continent, string myFaction)
{
lock (this)
{
// Continent change
Save();
this.continent = continent;
this.myFaction = myFaction;
Load();
}
}
public void Update()
{
lock (this)
{
if (continent == null)
return;
if (NPCs == null)
return;
GUnit[] units = GObjectList.GetUnits();
foreach (GUnit unit in units)
{
if ((unit.Reaction == GReaction.Friendly || unit.Reaction == GReaction.Neutral)
&& !unit.IsPlayer &&
unit.CreatureType != GCreatureType.Critter &&
unit.CreatureType != GCreatureType.Totem &&
!PPather.IsPlayerFaction(unit))
{
string name = unit.Name;
NPC n = null;
if (!NPCs.TryGetValue(name, out n))
{
n = new NPC();
n.name = name;
n.faction = unit.FactionID;
n.location = unit.Location;
n.reaction = unit.Reaction;
PPather.WriteLine("New NPC found: " + name);
NPCs.Add(name, n);
changed = true;
}
}
}
}
}
public void Save()
{
if (!changed)
return;
lock (this)
{
if (continent == null)
return;
string filename = "PPather\\NPCInfo\\" + continent + "_" + myFaction + ".txt";
try
{
System.IO.TextWriter s = System.IO.File.CreateText(filename);
foreach (string name in NPCs.Keys)
{
NPC n = NPCs[name];
s.WriteLine(n.ToString());
}
s.Close();
}
catch (Exception e)
{
PPather.WriteLine("!Error:Exception writing NPC data: " + e);
}
changed = false;
}
}
public void Load()
{
lock (this)
{
if (continent == null)
return;
NPCs = new Dictionary<string, NPC>(StringComparer.InvariantCultureIgnoreCase);
// Load from file
try
{
string filename = "PPather\\NPCInfo\\" + continent + "_" + myFaction + ".txt";
System.IO.TextReader s = System.IO.File.OpenText(filename);
int nr = 0;
string line;
while ((line = s.ReadLine()) != null)
{
NPC n = new NPC();
if (n.FromString(line))
{
if (!NPCs.ContainsKey(n.name))
{
NPCs.Add(n.name, n);
nr++;
}
}
}
PPather.WriteLine("Loaded " + nr + " NPCs");
s.Close();
}
catch (Exception)
{
//PPather.WriteLine("Exception reading NPC data: " + e);
PPather.WriteLine("!Warning:Failed to load NPC data");
}
changed = false;
}
}
public NPC Find(string name)
{
NPC n;
if (NPCs.TryGetValue(name, out n))
{
return n;
}
return null;
}
}
}