-
Notifications
You must be signed in to change notification settings - Fork 5
/
TeamType.cs
298 lines (244 loc) · 7.61 KB
/
TeamType.cs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace AIEdit
{
public abstract class TeamTypeOption
{
protected string tag, name;
public string Tag { get { return tag; } }
public string Name { get { return name; } }
public TeamTypeOption(string tag, string name)
{
this.tag = tag;
this.name = name;
}
public abstract IList List { get; }
public abstract TeamTypeEntry DefaultValue { get; }
public abstract int SortOrder { get; }
public abstract TeamTypeEntry Parse(OrderedDictionary section);
public abstract void Write(StreamWriter stream, object value);
}
public class TeamTypeOptionBool : TeamTypeOptionStringList
{
private static List<AITypeListEntry> bools = new List<AITypeListEntry>()
{
new AITypeListEntry(0, "no"),
new AITypeListEntry(1, "yes"),
};
public override int SortOrder { get { return 3; } }
public TeamTypeOptionBool(string tag, string name) : base(tag, name, bools)
{
}
}
public class TeamTypeOptionNumber : TeamTypeOption
{
public TeamTypeOptionNumber(string tag, string name) : base(tag, name)
{
}
public override IList List { get { return null; } }
public override TeamTypeEntry DefaultValue { get { return new TeamTypeEntry(this, (object)0); } }
public override int SortOrder { get { return 2; } }
public override TeamTypeEntry Parse(OrderedDictionary section)
{
if (!section.Contains(tag)) return DefaultValue;
return new TeamTypeEntry(this, section.GetInt(tag));
}
public override void Write(StreamWriter stream, object value)
{
stream.WriteLine(tag + "=" + ((int)value));
}
}
public class TeamTypeOptionList : TeamTypeOption
{
protected IList dataList;
public TeamTypeOptionList(string tag, string name, IList dataList)
: base(tag, name)
{
this.dataList = dataList;
}
public override IList List { get { return dataList; } }
public override TeamTypeEntry DefaultValue { get { return new TeamTypeEntry(this, dataList[0]); } }
public override int SortOrder { get { return 1; } }
public override TeamTypeEntry Parse(OrderedDictionary section)
{
if (!section.Contains(tag)) return DefaultValue;
int val = section.GetInt(tag);
foreach (AITypeListEntry listitem in dataList)
{
if (listitem.Index == val) return new TeamTypeEntry(this, listitem);
}
return new TeamTypeEntry(this, dataList[0]);
}
public override void Write(StreamWriter stream, object value)
{
int v = (value as AITypeListEntry).Index;
stream.WriteLine(tag + "=" + v);
}
}
public class TeamTypeOptionStringList : TeamTypeOptionList
{
public TeamTypeOptionStringList(string tag, string name, IList dataList) : base(tag, name, dataList)
{
}
public override int SortOrder { get { return 1; } }
public override TeamTypeEntry Parse(OrderedDictionary section)
{
if (!section.Contains(tag)) return DefaultValue;
string val = section.GetString(tag);
foreach (AITypeListEntry listitem in dataList)
{
if (listitem.Name == val) return new TeamTypeEntry(this, listitem);
}
return new TeamTypeEntry(this, dataList[0]);
}
public override void Write(StreamWriter stream, object value)
{
string v = (value as AITypeListEntry).Name;
stream.WriteLine(tag + "=" + v);
}
}
public class TeamTypeOptionAIObject : TeamTypeOptionList
{
public TeamTypeOptionAIObject(string tag, string name, IList dataList) : base(tag, name, dataList)
{
}
public override int SortOrder { get { return 0; } }
public override TeamTypeEntry Parse(OrderedDictionary section)
{
if (!section.Contains(tag)) return DefaultValue;
string id = section.GetString(tag);
foreach(IAIObject aiobj in dataList)
{
if (aiobj.ID == id) return new TeamTypeEntry(this, aiobj);
}
return new TeamTypeEntry(this, dataList[0]);
}
public override void Write(StreamWriter stream, object value)
{
string v = (value as IAIObject).ID;
stream.WriteLine(tag + "=" + v);
}
}
public class TeamTypeEntry
{
private TeamTypeOption option;
private object value;
public string Name { get { return option.Name; } }
public TeamTypeOption Option { get { return option; } }
public int SortOrder { get { return option.SortOrder; } }
public object Value
{
get
{
return value;
}
set
{
if (this.value is IAIObject) (this.value as IAIObject).DecUses();
if (value is IAIObject) (value as IAIObject).IncUses();
this.value = value;
}
}
public void Reset()
{
if (this.value is IAIObject) (this.value as IAIObject).DecUses();
this.value = null;
}
public TeamTypeEntry(TeamTypeOption option, object value)
{
this.option = option;
this.Value = value;
}
public TeamTypeEntry(TeamTypeEntry other)
{
this.option = other.option;
this.Value = other.Value;
}
public void Write(StreamWriter stream)
{
option.Write(stream, value);
}
}
public class TeamType : IAIObject, IEnumerable<TeamTypeEntry>, IParamListEntry
{
private string name, id;
private List<TeamTypeEntry> entries;
private int uses;
public string IsBaseDefense { get { return (entries.First(x => x.Option.Tag == "IsBaseDefense").Value as AITypeListEntry).Index == 0 ? "N" : "Y"; } }
public int Max { get { return (int)entries.First(x => x.Option.Tag == "Max").Value; } }
public int Priority { get { return (int)entries.First(x => x.Option.Tag == "Priority").Value; } }
public string House { get { return entries.First(x => x.Option.Tag == "House").Value.ToString(); } }
public uint ParamListIndex { get { return 0; } }
public string Name { get { return name; } set { name = value.Trim(); } }
public string ID { get { return id; } }
public int Uses { get { return uses; } }
public void IncUses() { uses++; }
public void DecUses() { uses--; }
public IEnumerator<TeamTypeEntry> GetEnumerator()
{
return entries.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool HasObject(object obj)
{
return entries.FirstOrDefault(e => e.Value == obj) != null;
}
public TeamType(string id, string name, List<TeamTypeEntry> entries)
{
this.id = id;
this.Name = name;
this.entries = (entries != null) ? entries : new List<TeamTypeEntry>();
}
public IAIObject Copy(string newid, string newname)
{
List<TeamTypeEntry> newentries = new List<TeamTypeEntry>();
foreach (TeamTypeEntry e in this.entries) newentries.Add(new TeamTypeEntry(e));
return new TeamType(newid, newname, newentries);
}
public override string ToString()
{
return name;
}
public void Reset()
{
foreach(TeamTypeEntry e in this.entries) e.Reset();
entries.Clear();
}
public void Write(StreamWriter stream)
{
stream.WriteLine("[" + id + "]");
stream.WriteLine("Name=" + name);
foreach (TeamTypeEntry entry in entries) entry.Write(stream);
stream.WriteLine();
}
public static TeamType Parse(string id, OrderedDictionary section, List<TeamTypeOption> options)
{
List<TeamTypeEntry> entries = new List<TeamTypeEntry>();
string name = section.GetString("Name");
if (name == null) name = id;
try
{
foreach(TeamTypeOption option in options)
{
entries.Add(option.Parse(section));
}
}
catch (Exception )
{
string msg = "Error occured at TeamType: [" + id + "]" + "\nPlease verify its format. Application will now close.";
MessageBox.Show(msg, "Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
Application.Exit();
}
return new TeamType(id, name, entries);
}
}
}