forked from InfiniteRasa/Game-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataInterface_Creature.cpp
More file actions
355 lines (340 loc) · 11.5 KB
/
DataInterface_Creature.cpp
File metadata and controls
355 lines (340 loc) · 11.5 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
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
#include<winsock2.h>
#include<stdio.h>
#include"DataInterface.h"
void cb_DataInterface_Creature_getCreatureActionList(MYSQL *dbCon, diJob_creatureAction_t *job, void *cb, void *param)
{
sint8 queryText[1024];
sprintf(queryText, "SELECT "
"id,description,actionId,actionArgId,"
"rangeMin,rangeMax,"
"recoverTime,recoverTimeGlobal,windupTime,minDamage,maxDamage"
" FROM creature_action");
// execute query
if( mysql_query(dbCon, queryText) )
{
printf("Error in query\n");
while(1) Sleep(1000);
}
MYSQL_RES *dbResult = mysql_store_result(dbCon);
MYSQL_ROW dbRow;
// parse rows
sint32 rowCount = mysql_num_rows(dbResult);
while((dbRow = mysql_fetch_row(dbResult)))
{
//"id,description,actionId,actionArgId,"
//"recoverTime,recoverTimeGlobal,windupTime,"
sint32 idx = 0;
sscanf(dbRow[idx], "%d", &job->id); idx++;
strcpy(job->description, dbRow[idx]); idx++;
sscanf(dbRow[idx], "%d", &job->actionId); idx++;
sscanf(dbRow[idx], "%d", &job->actionArgId); idx++;
sscanf(dbRow[idx], "%f", &job->rangeMin); idx++;
sscanf(dbRow[idx], "%f", &job->rangeMax); idx++;
sscanf(dbRow[idx], "%d", &job->recoverTime); idx++;
sscanf(dbRow[idx], "%d", &job->recoverTimeGlobal); idx++;
sscanf(dbRow[idx], "%d", &job->windupTime); idx++;
sscanf(dbRow[idx], "%d", &job->minDamage); idx++;
sscanf(dbRow[idx], "%d", &job->maxDamage); idx++;
// do callback param1: mapchannel param2: list of missile type data
((void (*)(void*,void*))cb)(param, job);
}
mysql_free_result(dbResult);
DataInterface_freeJob(job);
}
void DataInterface_Creature_getCreatureActionList(void (*cb)(void *param, diJob_creatureAction_t *jobData), void *param)
{
diJob_creatureAction_t *job = (diJob_creatureAction_t*)DataInterface_allocJob(sizeof(diJob_creatureAction_t));
DataInterface_queueJob(job, cb_DataInterface_Creature_getCreatureActionList, cb, param);
}
void cb_DataInterface_Creature_getCreatureTypeList_LootTable(MYSQL *dbCon, diJob_creatureType_t *job)
{
sint8 queryText[1024];
sprintf(queryText, "SELECT "
"itemTemplateId,chance,stacksizeMin,stacksizeMax"
" FROM creature_type_loot WHERE creatureTypeId = %d", job->id);
// execute query
if( mysql_query(dbCon, queryText) )
{
printf("Error in query\n");
while(1) Sleep(1000);
}
MYSQL_RES *dbResult = mysql_store_result(dbCon);
MYSQL_ROW dbRow;
// parse rows
sint32 rowCount = mysql_num_rows(dbResult);
// allocate table
if( rowCount > 0 )
{
job->numLootEntries = rowCount;
job->lootTable = (diData_lootTable_t*)malloc(sizeof(diData_lootTable_t) * rowCount);
memset(job->lootTable, 0x00, sizeof(diData_lootTable_t) * rowCount);
}
else
{
job->numLootEntries = 0;
job->lootTable = NULL;
}
sint32 lootIndex = 0;
while((dbRow = mysql_fetch_row(dbResult)))
{
//"itemTemplateId,chance,stacksizeMin,stacksizeMax"
sint32 loot_itemTemplateId = 0;
float loot_chance = 0;
sint32 loot_stacksizeMin = 0;
sint32 loot_stacksizeMax = 0;
sscanf(dbRow[0], "%d", &loot_itemTemplateId);
sscanf(dbRow[1], "%f", &loot_chance);
sscanf(dbRow[2], "%d", &loot_stacksizeMin);
sscanf(dbRow[3], "%d", &loot_stacksizeMax);
job->lootTable[lootIndex].itemTemplateId = loot_itemTemplateId;
job->lootTable[lootIndex].chance = loot_chance;
job->lootTable[lootIndex].stacksizeMin = loot_stacksizeMin;
job->lootTable[lootIndex].stacksizeMax = max(loot_stacksizeMin, loot_stacksizeMax);
// next
lootIndex++;
}
mysql_free_result(dbResult);
}
void cb_DataInterface_Creature_getCreatureTypeList(MYSQL *dbCon, diJob_creatureType_t *job, void *cb, void *param)
{
sint8 queryText[1024];
sprintf(queryText, "SELECT "
"id,name,nameId,classId,faction,"
"walkspeed,runspeed,hitpoints,"
"action1,action2,action3,action4,action5,action6,action7,action8,"
"wanderDistance"
" FROM creature_type");
// execute query
if( mysql_query(dbCon, queryText) )
{
printf("Error in query\n");
while(1) Sleep(1000);
}
MYSQL_RES *dbResult = mysql_store_result(dbCon);
MYSQL_ROW dbRow;
// parse rows
sint32 rowCount = mysql_num_rows(dbResult);
while((dbRow = mysql_fetch_row(dbResult)))
{
//"id,name,classId,faction,"
//"walkspeed,runspeed,hitpoints,"
//"melee_damageMin,melee_damageMax,melee_attackSpeed,melee_attackRange,"
//"range_damageMin,range_damageMax,range_missile,range_attackSpeed,range_attackRange"
sint32 idx = 0;
sscanf(dbRow[idx], "%d", &job->id); idx++;
strcpy(job->name, dbRow[idx]); idx++;
sscanf(dbRow[idx], "%d", &job->nameId); idx++;
sscanf(dbRow[idx], "%d", &job->classId); idx++;
sscanf(dbRow[idx], "%d", &job->faction); idx++;
sscanf(dbRow[idx], "%f", &job->walkspeed); idx++;
sscanf(dbRow[idx], "%f", &job->runspeed); idx++;
sscanf(dbRow[idx], "%d", &job->hitpoints); idx++;
for(sint32 i=0; i<8; i++)
{
if( dbRow[idx] == NULL )
{
job->missile[i] = 0;
idx++;
}
else
{
sscanf(dbRow[idx], "%d", &job->missile[i]); idx++;
}
}
sscanf(dbRow[idx], "%f", &job->wanderDistance); idx++;
// load the loot table for this creature
cb_DataInterface_Creature_getCreatureTypeList_LootTable(dbCon, job);
// do callback param1: mapchannel param2: list of creature type data
((void (*)(void*,void*))cb)(param, job);
// free temp data
if( job->lootTable )
free(job->lootTable);
}
mysql_free_result(dbResult);
DataInterface_freeJob(job);
}
/*
* Queries the database creature_type table
*/
void DataInterface_Creature_getCreatureTypeList(void (*cb)(void *param, diJob_creatureType_t *jobData), void *param)
{
diJob_creatureType_t *job = (diJob_creatureType_t*)DataInterface_allocJob(sizeof(diJob_creatureType_t));
DataInterface_queueJob(job, cb_DataInterface_Creature_getCreatureTypeList, cb, param);
}
void cb_DataInterface_Creature_savePath(MYSQL *dbCon, diJob_path_t *job, void *cb, void *param)
{
diData_path_t* pathData = job->pathData;
sint8 queryText[1024];
sprintf(queryText, "SELECT "
"max(pathId)"
" FROM creature_ai_path");
// execute query
if( mysql_query(dbCon, queryText) )
{
printf("Error in query\n");
while(1) Sleep(1000);
}
MYSQL_RES *dbResult = mysql_store_result(dbCon);
MYSQL_ROW dbRow;
// parse rows
sint32 rowCount = mysql_num_rows(dbResult);
if( rowCount == NULL )
__debugbreak();
dbRow = mysql_fetch_row(dbResult);
sint32 newPathId = 0;
if( dbRow[0] )
newPathId = atoi(dbRow[0]);
mysql_free_result(dbResult);
newPathId++;
pathData->pathId = newPathId;
// now that we have a valid pathId -> insert path
sprintf(queryText, "INSERT INTO creature_ai_path (`pathId`,`contextId`,`mode`,`spawnpool`) VALUES('%d','%d','%d','%d')", pathData->pathId, pathData->contextId, pathData->mode, pathData->spawnpool);
// execute query
if( mysql_query(dbCon, queryText) )
{
printf("Error in query\n");
while(1) Sleep(1000);
}
// add all the path nodes
for(sint32 i=0; i<pathData->numberOfNodes; i++)
{
sprintf(queryText, "INSERT INTO creature_ai_pathnodes (`pathId`,`nodeIndex`,`x`,`y`,`z`) VALUES('%d','%d','%f','%f','%f')", pathData->pathId, i+1, pathData->pathnodes[i].pos[0], pathData->pathnodes[i].pos[1], pathData->pathnodes[i].pos[2]);
// execute query
if( mysql_query(dbCon, queryText) )
{
printf("Error in query\n");
while(1) Sleep(1000);
}
}
// free job and leave
pathData->_isFinished = true;
DataInterface_freeJob(job);
}
/*
* SYNCHRONOUSLY adding a path to the db (table creature_ai_path and creature_ai_pathnodes)
* This method is dangerous,
*/
void DataInterface_Creature_savePath(diData_path_t* pathData)
{
diJob_path_t *job = (diJob_path_t*)DataInterface_allocJob(sizeof(diJob_path_t));
job->pathData = pathData;
pathData->_isFinished = false;
DataInterface_queueJob(job, cb_DataInterface_Creature_savePath, NULL, NULL);
while( pathData->_isFinished == false ) Sleep(1);
}
/*
* Queries data from creature_ai_pathnodes table for a specific path
*/
void _cb_DataInterface_Creature_getPathNodes(MYSQL *dbCon, diData_path_t* pathData)
{
sint8 queryText[1024];
wsprintf(queryText, "SELECT "
"x,y,z"
" FROM creature_ai_pathnodes WHERE pathId = %d ORDER BY nodeIndex ASC", pathData->pathId);
// execute query
if( mysql_query(dbCon, queryText) )
{
printf("Error in query\n");
while(1) Sleep(1000);
}
MYSQL_RES *dbResult = mysql_store_result(dbCon);
MYSQL_ROW dbRow;
// allocate path nodes
sint32 pathNodeCount = mysql_num_rows(dbResult);
diData_pathNodes_t *pathNodeList = (diData_pathNodes_t*)malloc(sizeof(diData_pathNodes_t) * pathNodeCount);
// parse mysql data
diData_pathNodes_t* pathNodeData = pathNodeList;
while((dbRow = mysql_fetch_row(dbResult)))
{
float pathnode_x = 0.0f;
float pathnode_y = 0.0f;
float pathnode_z = 0.0f;
sscanf(dbRow[0], "%f", &pathnode_x);
sscanf(dbRow[1], "%f", &pathnode_y);
sscanf(dbRow[2], "%f", &pathnode_z);;
pathNodeData->pos[0] = pathnode_x;
pathNodeData->pos[1] = pathnode_y;
pathNodeData->pos[2] = pathnode_z;
// next
pathNodeData++;
}
mysql_free_result(dbResult);
pathData->pathnodes = pathNodeList;
pathData->numberOfNodes = pathNodeCount;
}
/*
* Queries data from creature_ai_path table for all paths
* Also, queries each paths's node list
*/
void cb_DataInterface_Vendor_getVendorList(MYSQL *dbCon, diJob_path_t *job, void *cb, void *param)
{
sint8 queryText[1024];
wsprintf(queryText, "SELECT "
"pathId,contextId,mode,spawnpool,nodeOffsetRandomization"
" FROM creature_ai_path");
// execute query
if( mysql_query(dbCon, queryText) )
{
printf("Error in query\n");
while(1) Sleep(1000);
}
MYSQL_RES *dbResult = mysql_store_result(dbCon);
MYSQL_ROW dbRow;
// allocate path data
sint32 pathCount = mysql_num_rows(dbResult);
diData_path_t *pathDataList = (diData_path_t*)malloc(sizeof(diData_path_t) * pathCount);
// parse mysql data
diData_path_t* path = pathDataList;
while((dbRow = mysql_fetch_row(dbResult)))
{
// "pathId,contextId,mode"
sint32 path_pathId = 0;
sint32 path_contextId = 0;
sint32 path_mode = 0;
sint32 path_spawnpool = 0;
float path_nodeOffsetRandomization = 0.0f;
sscanf(dbRow[0], "%d", &path_pathId);
sscanf(dbRow[1], "%d", &path_contextId);
sscanf(dbRow[2], "%d", &path_mode);
if( dbRow[3] )
sscanf(dbRow[3], "%d", &path_spawnpool);
else
path_spawnpool = 0;
if( dbRow[4] )
sscanf(dbRow[4], "%f", &path_nodeOffsetRandomization);
else
path_nodeOffsetRandomization = 0.0f;
path->pathId = path_pathId;
path->contextId = path_contextId;
path->mode = path_mode;
path->spawnpool = path_spawnpool;
path->nodeOffsetRandomization = path_nodeOffsetRandomization;
// for each vendor, query item data
_cb_DataInterface_Creature_getPathNodes(dbCon, path);
// next
path++;
}
mysql_free_result(dbResult);
job->pathList = pathDataList;
job->pathCount = pathCount;
// do callback param1: mapchannel param2: list of job path data
((void (*)(void*,void*))cb)(param, job);
// free all path node lists
for(uint32 i=0; i<pathCount; i++)
{
if( pathDataList[i].pathnodes )
free(pathDataList[i].pathnodes);
}
// free path list
free(pathDataList);
// free job
DataInterface_freeJob(job);
}
/*
* Queries all ai paths and their associates nodes from the db
*/
void DataInterface_Creature_getPathList(void (*cb)(void *param, diJob_path_t *jobData), void *param)
{
diJob_path_t *job = (diJob_path_t*)DataInterface_allocJob(sizeof(diJob_path_t));
DataInterface_queueJob(job, cb_DataInterface_Vendor_getVendorList, cb, param);
}