-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathNpc.hpp
406 lines (356 loc) · 9.53 KB
/
Npc.hpp
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#ifndef NPC_HPP_INCLUDED
#define NPC_HPP_INCLUDED
#include <string>
#include <list>
#include <map>
#include "wx/string.h"
#include "wx/arrstr.h"
#include "wx/stdpaths.h"
#include "wx/filename.h"
#include "wx/textfile.h"
#include "wx/cmdproc.h"
#include "AsfDecode.hpp"
#include "ListDefHelper.h"
struct NpcItem
{
std::map<wxString, wxVariant> KeyValues;
AsfDecode *NpcStand; //Point to AsfImgList item, don't free
long MapX()
{
if(KeyValues.count(wxT("MapX")) > 0)
{
return KeyValues[wxT("MapX")].GetInteger();
}
return 0;
}
long MapY()
{
if(KeyValues.count(wxT("MapY")) > 0)
{
return KeyValues[wxT("MapY")].GetInteger();
}
return 0;
}
void SetMapX(long x)
{
KeyValues[wxT("MapX")] = x;
}
void SetMapY(long y)
{
KeyValues[wxT("MapY")] = y;
}
wxString GetString(const wxString& key)
{
if(KeyValues.count(key) > 0)
{
return KeyValues[key].GetString();
}
return wxT("");
}
long GetInteger(const wxString& key)
{
if(KeyValues.count(key) > 0)
{
return KeyValues[key].GetInteger();
}
return 0;
}
void SetValue(const wxString&key, const wxVariant& value)
{
KeyValues[key] = value;
}
NpcItem() {NpcStand = NULL;}
void CopyFrom(const NpcItem *item);
};
struct ObjItem
{
std::map<wxString, wxVariant> KeyValues;
AsfDecode *ObjCommon; //Point to AsfObjImgList item, don't free
long MapX()
{
if(KeyValues.count(wxT("MapX")) > 0)
{
return KeyValues[wxT("MapX")].GetInteger();
}
return 0;
}
long MapY()
{
if(KeyValues.count(wxT("MapY")) > 0)
{
return KeyValues[wxT("MapY")].GetInteger();
}
return 0;
}
void SetMapX(long x)
{
KeyValues[wxT("MapX")] = x;
}
void SetMapY(long y)
{
KeyValues[wxT("MapY")] = y;
}
long GetOffX()
{
long offx = GetInteger(wxT("OffX"));
return offx == -1 ? 0 : offx;
}
long GetOffY()
{
long offy = GetInteger(wxT("OffY"));
return offy == -1 ? 0 : offy;
}
void SetOffX(long x)
{
KeyValues[wxT("OffX")] = x;
}
void SetOffY(long y)
{
KeyValues[wxT("OffY")] = y;
}
wxString GetString(const wxString& key)
{
if(KeyValues.count(key) > 0)
{
return KeyValues[key].GetString();
}
return wxT("");
}
long GetInteger(const wxString& key)
{
if(KeyValues.count(key) > 0)
{
return KeyValues[key].GetInteger();
}
return 0;
}
void SetValue(const wxString&key, const wxVariant& value)
{
KeyValues[key] = value;
}
ObjItem(){ObjCommon = NULL;}
void CopyFrom(const ObjItem *item);
};
struct AsfImg
{
wxString path;
AsfDecode *asfdec;
};
typedef std::list<AsfImg*> AsfImgList;
typedef std::list<AsfImg*>::iterator AsfImgListIterator;
// if in , outasf is the asf data
bool IsAsfFileIn(wxString path, AsfImgList *list, AsfDecode **outasf);
// Add img to list
AsfImg* AddImg(AsfImgList &list, const wxString &asfFilePath);
//delete list item AsfImg*, AsfImg::asfdec, and clear list
void FreeAsfImgList(AsfImgList *list);
// find [stand] asf file in npcres ini file
std::string FindAsfInIni(const std::string FilePath, const std::string match);
//Find asfdecoder in NpcItem or ObjItem or both ini res file
void FindAndBufferAsf(const wxString &exepath, NpcItem *npcitem, ObjItem *objitem, AsfImgList *asflist);
// find [stand] asf and buffer its data
//exepath : the end contain path seprator
//asfdec : out , get a point of AsfDecode data
//asflist : AsfDecode manager list
bool FindAndBufferAsf(const wxString &exepath,
const wxString &inifilename,
const wxString &match,
AsfDecode **asfdec,
AsfImgList *asflist = NULL);
// init NpcItem to default vaule
void InitNpcItem(NpcItem *item);
void InitObjItem(ObjItem *item);
// Read a npc ini file and initializing item
//exepath : the end contain path seprator
//list : if not null, npc's asfdata is add to list
bool ReadIni(const wxString &exepath,
const wxString &filePath,
NpcItem *npcitem = NULL,
ObjItem *objitem = NULL,
AsfImgList *list = NULL);
bool SaveIni(const wxString &filePath, NpcItem *npcitem = NULL, ObjItem *objitem = NULL, ListDefHelper *listDefHelper = NULL);
void SaveNpcItem(wxTextFile &file, NpcItem *item, ListDefHelper *listDefHelper);
void SaveObjItem(wxTextFile &file, ObjItem *item, ListDefHelper *listDefHelper);
void AssignNpcItem(const wxString &name, const wxString &value, long n_value, NpcItem *item);
void AssignObjItem(const wxString &name, const wxString &value, long n_value, ObjItem *item);
bool GetNameValue(const wxString &line, wxString &name, wxString &value, long *n_value);
template<class T>
class ItemList
{
public:
typedef typename std::list<T>::iterator iterator;
ItemList()
{
exepath = wxStandardPaths::Get().GetExecutablePath();
exepath = wxFileName::FileName(exepath).GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
}
~ItemList()
{
FreeData();
}
T GetItem(int index)
{
if(index < 0 || index >= (int)m_list.size()) return T(NULL);
typename std::list<T>::iterator it = m_list.begin();
for(int i = 0; i < index; i++, it++);
return *it;
}
int GetIndex(T item)
{
int index = 0;
for(typename std::list<T>::iterator it = m_list.begin(); it != m_list.end(); ++it, index++)
{
if(*it == item) return index;
}
return -1;
}
//Returned array must deleted use delete[]
T* GetAllItem()
{
size_t num = m_list.size();
T *items = new T[num + 1];
items[num] = NULL;
long pos = 0;
for(typename std::list<T>::iterator it = m_list.begin(); it != m_list.end(); ++it)
{
items[pos++] = *it;
}
return items;
}
T GetItem(long mapx, long mapy, long *index = NULL)
{
long pos = 0;
for(typename std::list<T>::iterator it = m_list.begin(); it != m_list.end(); ++it, pos++)
{
if((*it)->MapX() == mapx && (*it)->MapY() == mapy)
{
if(index != NULL) (*index) = pos;
return *it;
}
}
return T(NULL);
}
void InsertItem(int pos, T item)
{
if(pos == 0)
{
m_list.insert(m_list.begin(), item);
}
else
{
int i = 0;
bool inserted = false;
auto it = m_list.begin();
for(; it != m_list.end();)
{
if(i == pos)
{
m_list.insert(it, item);
inserted = true;
break;
}
i++;
it++;
}
if(!inserted)
{
//insert at end
m_list.insert(it, item);
}
}
}
void AddItem(T item)
{
if(item == NULL) return;
m_list.push_back(item);
}
void DeleteBack()
{
if(m_list.size() > 0)
{
delete m_list.back();
m_list.pop_back();
}
}
void DeleteItem(long mapx, long mapy)
{
for(typename std::list<T>::iterator it = m_list.begin(); it != m_list.end(); ++it)
{
if((*it)->MapX() == mapx && (*it)->MapY() == mapy)
{
delete *it;
m_list.erase(it);
return;
}
}
}
void DeleteItem(int index)
{
if(index < 0 || index >= (int)m_list.size()) return;
typename std::list<T>::iterator it = m_list.begin();
for(int i = 0; i < index; i++, it++);
delete *it;
m_list.erase(it);
}
int getCounts()
{
return m_list.size();
}
bool HasItem(long mapx, long mapy)
{
for(typename std::list<T>::iterator it = m_list.begin(); it != m_list.end(); ++it)
{
if((*it)->MapX() == mapx && (*it)->MapY() == mapy)
{
return true;
}
}
return false;
}
iterator begin()
{
return m_list.begin();
}
iterator end()
{
return m_list.end();
}
void Clear()
{
FreeData();
}
//clear list without deleting items in list.
void ClearWithoutFreeData()
{
m_list.clear();
}
void HandoverOwnship(ItemList<T>& newOwner)
{
for(typename std::list<T>::iterator it = m_list.begin();
it != m_list.end(); ++it)
{
newOwner.AddItem(*it);
}
m_list.clear();
}
private:
void FreeData()
{
if(m_list.empty()) return;
for(typename std::list<T>::iterator it = m_list.begin();
it != m_list.end(); ++it)
{
delete *it;
}
m_list.clear();
}
std::list<T> m_list;
wxString exepath;
};
typedef ItemList<NpcItem*> NpcList;
bool NpcListImport(const wxString &exepath, const wxString &path, NpcList *list, AsfImgList *asflist, wxCommandProcessor *cmdProc);
bool NpcListSave(const wxString path, const wxString mapName, NpcList *list, ListDefHelper *listDefHelper);
typedef ItemList<ObjItem*> ObjList;
bool ObjListImport(const wxString &exepath, const wxString &path, ObjList *list, AsfImgList *asflist, wxCommandProcessor *cmdProc);
bool ObjListSave(const wxString path, const wxString mapName, ObjList *list, ListDefHelper *listDefHelper);
#endif // NPC_HPP_INCLUDED