forked from InfiniteRasa/Game-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgm.cpp
More file actions
223 lines (218 loc) · 7.23 KB
/
gm.cpp
File metadata and controls
223 lines (218 loc) · 7.23 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
#include"global.h"
/*
* Inits the data to create path
* Triggered by entering .createpath
*/
void gm_startPathCreation(mapChannelClient_t *cm)
{
if( cm->player->gmData == NULL )
return; // not a gm
if( cm->player->gmData->pathCreation )
{
// path creation is already active
communicator_systemMessage(cm, "Please complete the last path with .finishpath first, or use .discardpath to delete the path");
return;
}
cm->player->gmData->pathCreation = new gmPathCreation_t();
communicator_systemMessage(cm, "Starting path creation...");
}
/*
* Adds a new node to the path
* Triggered by entering .addnode
*/
void gm_addPathNode(mapChannelClient_t *cm)
{
if( cm->player->gmData == NULL )
return; // not a gm
if( cm->player->gmData->pathCreation == NULL )
{
// path creation is not active
communicator_systemMessage(cm, "Please start a path with .createpath first");
return;
}
// init node and add it to the list
gmPathNode_t gmPathNode;
gmPathNode.x = cm->player->actor->posX;
gmPathNode.y = cm->player->actor->posY;
gmPathNode.z = cm->player->actor->posZ;
// create an object only visible for the gm
gmPathNode.entityId_nodeDebugObject = 20000000 + rand()*6500 + rand(); // random entity (and we hope it is free)
// create object entity
pyMarshalString_t pms;
pym_init(&pms);
pym_tuple_begin(&pms);
pym_addInt(&pms, gmPathNode.entityId_nodeDebugObject); // entityID
pym_addInt(&pms, 26582); // classID
pym_addNoneStruct(&pms);
pym_tuple_end(&pms);
netMgr_pythonAddMethodCallRaw(cm->cgm, 5, METHODID_CREATEPYHSICALENTITY, pym_getData(&pms), pym_getLen(&pms));
// set position
pym_init(&pms);
pym_tuple_begin(&pms);
// position
pym_tuple_begin(&pms);
pym_addFloat(&pms, cm->player->actor->posX); // x
pym_addFloat(&pms, cm->player->actor->posY+0.1f); // y
pym_addFloat(&pms, cm->player->actor->posZ); // z
pym_tuple_end(&pms);
// rotation quaternion
pym_tuple_begin(&pms);
pym_addFloat(&pms, 0.0f);
pym_addFloat(&pms, 0.0f);
pym_addFloat(&pms, 0.0f);
pym_addFloat(&pms, 1.0f);
pym_tuple_end(&pms);
pym_tuple_end(&pms);
netMgr_pythonAddMethodCallRaw(cm->cgm, gmPathNode.entityId_nodeDebugObject, 243, pym_getData(&pms), pym_getLen(&pms));
communicator_systemMessage(cm, "Created object");
// add to node list
cm->player->gmData->pathCreation->pathNodes.push_back(gmPathNode);
// tell user we created the path
char textMsg[256];
sint32 pathNodeIndex = cm->player->gmData->pathCreation->pathNodes.size();
sprintf(textMsg, "Path node with index %d added (%f %f %f)\n", pathNodeIndex, cm->player->actor->posX, cm->player->actor->posY, cm->player->actor->posZ);
communicator_systemMessage(cm, textMsg);
}
/*
* Deletes the currently generated path
* Triggered by entering .discardpath
*/
void gm_discardPath(mapChannelClient_t *cm)
{
if( cm->player->gmData == NULL )
return; // not a gm
if( cm->player->gmData->pathCreation == NULL )
{
// path creation is not active
communicator_systemMessage(cm, "No path active");
return;
}
// delete all entities
gmPathNode_t *pathNodeList = &cm->player->gmData->pathCreation->pathNodes[0];
sint32 pathNodeCount = cm->player->gmData->pathCreation->pathNodes.size();
for(sint32 f=0; f<pathNodeCount; f++)
{
// send client destroy packet for each debug entity (node object)
pyMarshalString_t pms;
pym_init(&pms);
pym_tuple_begin(&pms);
pym_addInt(&pms, pathNodeList[f].entityId_nodeDebugObject); // entityID
pym_tuple_end(&pms);
netMgr_pythonAddMethodCallRaw(cm->cgm, 5, METHODID_DESTROYPHYSICALENTITY, pym_getData(&pms), pym_getLen(&pms));
}
// delete path
delete cm->player->gmData->pathCreation;
cm->player->gmData->pathCreation = NULL;
communicator_systemMessage(cm, "Active path discarded");
}
/*
* Analyzes, sets path mode and saves the path
* Triggered by entering .finishpath
*/
void gm_finishPath(mapChannelClient_t *cm, bool attachToLastSpawnpool)
{
if( cm->player->gmData == NULL )
return; // not a gm
if( cm->player->gmData->pathCreation == NULL )
{
// path creation is not active
communicator_systemMessage(cm, "No path active");
return;
}
// if attaching to spawnpool, test if there is a spawnpool
if( cm->player->gmData->lastCreatedSpawnpool == 0 )
{
// no spawnpool
communicator_systemMessage(cm, "You have not created a spawnpool in this session");
return;
}
// delete all entities
gmPathNode_t *pathNodeList = &cm->player->gmData->pathCreation->pathNodes[0];
sint32 pathNodeCount = cm->player->gmData->pathCreation->pathNodes.size();
for(sint32 f=0; f<pathNodeCount; f++)
{
// send client destroy packet for each debug entity (node object)
pyMarshalString_t pms;
pym_init(&pms);
pym_tuple_begin(&pms);
pym_addInt(&pms, pathNodeList[f].entityId_nodeDebugObject); // entityID
pym_tuple_end(&pms);
netMgr_pythonAddMethodCallRaw(cm->cgm, 5, METHODID_DESTROYPHYSICALENTITY, pym_getData(&pms), pym_getLen(&pms));
}
// analyze path to get mode (cyclic or one-shot)
float startLoc[3];
float endLoc[3];
startLoc[0] = pathNodeList[0].x;
startLoc[1] = pathNodeList[0].y;
startLoc[2] = pathNodeList[0].z;
endLoc[0] = pathNodeList[pathNodeCount-1].x;
endLoc[1] = pathNodeList[pathNodeCount-1].y;
endLoc[2] = pathNodeList[pathNodeCount-1].z;
float distX = startLoc[0] - endLoc[0];
float distY = startLoc[1] - endLoc[1];
float distZ = startLoc[2] - endLoc[2];
float dist = sqrt(distX*distX+distY*distY+distZ*distZ);
bool modeIsCyclic = false;
if( dist < 9.0f )
modeIsCyclic = true;
// save path
diData_path_t pathData = {0};
pathData.contextId = cm->mapChannel->mapInfo->contextId;
pathData.mode = modeIsCyclic?1:0;
pathData.pathId = 0;
pathData.numberOfNodes = pathNodeCount;
pathData.pathnodes = (diData_pathNodes_t*)malloc(sizeof(diData_pathNodes_t)*pathNodeCount);
for(sint32 f=0; f<pathNodeCount; f++)
{
pathData.pathnodes[f].pos[0] = pathNodeList[f].x;
pathData.pathnodes[f].pos[1] = pathNodeList[f].y;
pathData.pathnodes[f].pos[2] = pathNodeList[f].z;
}
if( attachToLastSpawnpool )
pathData.spawnpool = cm->player->gmData->lastCreatedSpawnpool;
DataInterface_Creature_savePath(&pathData);
free(pathData.pathnodes);
sint32 pathId = pathData.pathId;
// delete path
delete cm->player->gmData->pathCreation;
cm->player->gmData->pathCreation = NULL;
char msgText[256];
char* pathMode = modeIsCyclic?"cyclic":"one-shot";
sprintf(msgText, "Path saved. pathId: %d Mode: %s Nodes: %d Spawnpool: %d%s", pathId, pathMode, pathNodeCount, pathData.spawnpool, (pathData.spawnpool==0)?"(none)":"");
communicator_systemMessage(cm, msgText);
}
/*
* Called to parse gm commands (to keep communicator.cpp a bit more clean)
* Returns true if the message should be blocked and not written to the chat
*/
bool gm_parseGmCommands(mapChannelClient_t *cm, char *textMsg)
{
if( memcmp(textMsg, ".createpath", 11) == 0 )
{
gm_startPathCreation(cm);
// we also immediately create our first path node
gm_addPathNode(cm);
return true;
}
else if( memcmp(textMsg, ".addnode", 8) == 0 )
{
gm_addPathNode(cm);
return true;
}
else if( memcmp(textMsg, ".discardpath", 12) == 0 )
{
gm_discardPath(cm);
return true;
}
else if( memcmp(textMsg, ".finishpath", 11) == 0 )
{
gm_finishPath(cm, false);
return true;
}
else if( memcmp(textMsg, ".spawnpoolpath", 14) == 0 )
{
gm_finishPath(cm, true);
return true;
}
return false; // do not suppress message
}