-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.cpp
More file actions
467 lines (398 loc) · 11.5 KB
/
Copy patheditor.cpp
File metadata and controls
467 lines (398 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include "mastrix.hpp"
#include <algorithm>
static void writeLevel(FILE *fout);
ClientConsoleVar<bool> gridEnabled("grid_enabled", false);
ClientConsoleVar<float> gridSize("grid_size", 64);
// Get the mouse X-position and translate it into game coordinates.
float getEditMouseX()
{
Client *cl = (Client*)currentNode;
return (getMouseX() - cl->getViewport()->center_x)/zoom + cl->getCamera().getX();
}
// Get the mouse Y-position and translate it into game coordinates.
float getEditMouseY()
{
Client *cl = (Client*)currentNode;
return (getMouseY() - cl->getViewport()->center_y)/zoom + cl->getCamera().getY();
}
CLIENT_CONSOLE_COMMAND(select_click)
{
((Client*)currentNode) -> select();
}
CLIENT_CONSOLE_COMMAND(cursor)
{
if(argc<1) {
console.printf("Usage: cursor [command] [arguments]");
return;
}
std::string args;
for(int ii=1; ii<argc; ii++)
args += std::string(" {") + argv[ii] + "}";
float x = getEditMouseX(),
y = getEditMouseY();
if(gridEnabled) {
x = gridSize * floor((x+gridSize/2)/gridSize);
y = gridSize * floor((y+gridSize/2)/gridSize);
}
console.command( retprintf(
"%s %f %f %s",
argv[0],
x, y,
args.c_str()) );
}
SERVER_CONSOLE_COMMAND(selected)
{
if(argc<1) {
console.printf("Usage: selected [command] [arguments]");
return;
}
std::string args;
for(int ii=1; ii<argc; ii++)
args += std::string(" {") + argv[ii] + "}";
console.command( retprintf(
"%s %i %s",
argv[0],
server->selectedEntity,
args.c_str()) );
}
void Client::select(void)
{
for(CLEntityPool::iterator ii=entpool.begin(); ii!=entpool.end(); ii++)
{
CLEntity *ent = ii->second;
Position &pos(ii->second->getPosition());
float radius = ii->second->sprite->getWidth() / 2 * ii->second->scale;
float click_x = getEditMouseX(), click_y = getEditMouseY();
float dx = click_x - pos.getX(),
dy = click_y - pos.getY();
float distSq = dx*dx + dy*dy;
if(distSq < radius*radius) {
console.command( retprintf("select_ent %i", ii->first) );
break;
}
}
}
void Client::select(int entID)
{
selectedEntId = entID;
}
SERVER_CONSOLE_COMMAND(addregion)
{
if(argc != 5) {
console.printf("Usage: addregion name min_x min_y max_x max_y");
return;
}
float min_x = atof(argv[1]),
min_y = atof(argv[2]),
max_x = atof(argv[3]),
max_y = atof(argv[4]);
if(min_x > max_x) std::swap(min_x, max_x);
if(min_y > max_y) std::swap(min_y, max_y);
Server::Region addition = {argv[0], min_x, max_x, min_y, max_y};
server->regions.push_back(addition);
server->refreshRegionDisplay();
}
/* Remove all ents inside the region */
SERVER_CONSOLE_COMMAND(clear_region)
{
if(argc != 4) {
console.printf("Usage: clear_region min_x min_y max_x max_y");
return;
}
float min_x = atof(argv[0]),
min_y = atof(argv[1]),
max_x = atof(argv[2]),
max_y = atof(argv[3]);
if(min_x > max_x) std::swap(min_x, max_x);
if(min_y > max_y) std::swap(min_y, max_y);
for(Server::Entpool::iterator ii=server->entities.begin(); ii!=server->entities.end(); ii++)
{
Entity *ent = ii->second;
float x = ent->getPosition().getX(),
y = ent->getPosition().getY();
if(x>min_x && x<max_x && y>min_y && y<max_y)
ent->deleteMe();
}
}
SERVER_CONSOLE_COMMAND(select_ent)
{
if(argc != 1) {
console.printf("Usage: select_ent [entID]");
return;
}
int entID = atoi(argv[0]);
if (!server->entities[entID]->isVisible())
return;
server->selectedEntity = entID;
SendMessage msg(message_selection_box);
msg.putInt(entID);
msg.sendToEveryone();
}
SERVER_CONSOLE_COMMAND(move_ent_to)
{
if(argc < 2) {
console.printf("Usage: move_ent_to x y [entid]");
return;
}
int entid = server->selectedEntity;
if(argc == 3)
entid = atoi(argv[2]);
if(entid < 0) return;
Entity *ent = server->entities[entid];
Position newPos = Position(atof(argv[0]), atof(argv[1]), ent->getPosition().getR());
ent->setPosition( newPos );
//if the entity is a waypoint, recalculate all of its edge weights
if (ent->isWaypointMarker())
((WaypointMarker *)ent)->calculateEdgeWeights();
}
SERVER_CONSOLE_COMMAND(set_rvel)
{
if(argc < 1) {
console.printf("Usage: set_rvel rvel [entid]");
return;
}
int entid = server->selectedEntity;
if(argc == 2)
entid = atoi(argv[1]);
if(entid < 0) return;
Entity *ent = server->entities[entid];
Position pos = ent->getPosition();
pos.setR_vel( atof(argv[0]) );
ent->setPosition(pos);
}
SERVER_CONSOLE_COMMAND(rescale_ent)
{
if(argc < 1) {
console.printf("Usage: rescale size [entid]");
return;
}
int entid = server->selectedEntity;
if(argc == 2)
entid = atoi(argv[1]);
if(entid < 0) return;
Entity *ent = server->entities[entid];
ent->rescale( atof(argv[0]) );
}
SERVER_CONSOLE_COMMAND(rotate_ent_towards)
{
if(argc < 2) {
console.printf("Usage: rotate_ent_towards x y [entid]");
return;
}
int entid = server->selectedEntity;
if(argc == 3)
entid = atoi(argv[2]);
if(entid < 0) return;
Entity *ent = server->entities[entid];
float x=atof(argv[0]), y=atof(argv[1]);
float angle = atan2( ent->getPosition().getY()-y, x-ent->getPosition().getX() );
Position newPos = ent->getPosition();
newPos.setR(angle);
ent->setPosition(newPos);
}
SERVER_CONSOLE_COMMAND(delete_ent)
{
int entid = server->selectedEntity;
if(argc == 1)
entid = atoi(argv[0]);
if(entid < 0) return;
Entity *ent = server->entities[server->selectedEntity];
ent->die(-1);
}
SERVER_CONSOLE_COMMAND(entflag)
{
if(argc < 1) {
printfToClient(who, "Usage: entflag flag [entid]");
return;
}
int entid = server->selectedEntity;
if(argc == 2)
entid = atoi(argv[1]);
if(entid < 0) return;
Entity *ent = server->entities[entid];
EnvironmentEntity *cast = (EnvironmentEntity*)ent;
if(!cast) return;
int flag = EnvironmentEntity::flagFromString(argv[0]);
if(!flag)
printfToClient(who, "%s is not a valid flag.", argv[0]);
else
cast->addFlag(flag);
}
SERVER_CONSOLE_COMMAND(kick_ent_towards)
{
if(argc < 3) {
console.printf("Usage: kick_ent_towards x y strength [entid]");
return;
}
int entid = server->selectedEntity;
if(argc == 4)
entid = atoi(argv[3]);
if(entid < 0) return;
float x=atof(argv[0]), y=atof(argv[1]);
float strength = atof(argv[2]);
Entity *ent = server->entities[entid];
//ignore this command if it's a waypoint (too damn expensive to constantly
//recalculate the edge weights for every step it moves)
if (ent->isWaypointMarker())
return;
Position newPos = ent->getPosition();
float xvel = x-newPos.getX(),
yvel = y-newPos.getY();
float norm = sqrt(xvel*xvel + yvel*yvel);
xvel *= strength/norm;
yvel *= strength/norm;
newPos.setX_vel(xvel);
newPos.setY_vel(yvel);
ent->setPosition(newPos);
}
SERVER_CONSOLE_COMMAND(save_level)
{
if(argc != 1) {
printfToClient(who, "Usage: save_level name");
return;
}
// Check for bad chars and generate filename
for(const char *pos=argv[0]; *pos; pos++) {
if(*pos == '.' || *pos == '/' || *pos == '\\') {
printfToClient(who, "Cannot use special characters in filename. (The extension and path are handled automatically.");
}
}
std::string filename = retprintf("maps/%s.map", argv[0]);
// First, read the file that will be overwritten and extract all non-auto-generated parts.
char buf[512];
std::string prefix(""), suffix("");
std::string *read_target = &prefix;
int read_state = 0; // 0:prefix, 1:generated, 2:suffix
FILE *fin = fopen(filename.c_str(), "r");
if(fin)
{
while(!feof(fin))
{
if(!fgets(buf, 512, fin))
break;
if(!strcmp(buf, "# BEGIN GENERATED CODE\n") ) {
read_state = 1;
read_target = NULL;
}
else if(!strcmp(buf, "# END GENERATED CODE\n") ) {
read_state = 2;
read_target = &suffix;
}
else if(read_target)
(*read_target) += buf;
}
fclose(fin);
}
// Open output file
FILE *fout = fopen(filename.c_str(), "w");
if(!fout) {
printfToClient(who, "Couldn't open output file.");
return;
}
fputs(prefix.c_str(), fout);
fputs("# BEGIN GENERATED CODE\n", fout);
writeLevel(fout);
fputs("# END GENERATED CODE\n", fout);
fputs(suffix.c_str(), fout);
fclose(fout);
}
static void writeLevel(FILE *fout)
{
// Write addent ... lines
for(Server::Entpool::iterator ii=server->entities.begin(); ii!=server->entities.end(); ii++)
fprintf(fout, "%s", ii->second->creationString().c_str());
// Write addspawn ... lines
for(Server::SpawnPool::iterator ii=server->spawns.begin(); ii!=server->spawns.end(); ii++)
{
fprintf(
fout,
"addspawn %f %f %f %i\n",
(*ii)->getPosition().getX(),
(*ii)->getPosition().getY(),
(*ii)->getPosition().getR(),
(*ii)->team);
}
// Write addregion ... lines
for(Server::RegionPool::iterator ii=server->regions.begin(); ii!=server->regions.end(); ii++)
{
fprintf(fout,
"addregion %s %f %f %f %f\n",
ii->name.c_str(), (float)ii->min_x, (float)ii->min_y, (float)ii->max_x, (float)ii->max_y);
}
// Write addwaypoint ... lines
for (Server::WaypointPool::iterator ii = server->waypoints.begin(); ii != server->waypoints.end(); ++ii)
{
WaypointMarker *currentWaypoint = (*ii);
fprintf(
fout,
"addwaypoint %f %f\n",
currentWaypoint->getPosition().getX(),
currentWaypoint->getPosition().getY());
}
// Write the waypoint edges (taking 2nd pass because not all waypoints might
// have already been read)
server->unvisitAllWaypoints();
for (Server::WaypointPool::iterator ii = server->waypoints.begin(); ii != server->waypoints.end(); ++ii)
{
WaypointMarker *currentWaypoint = (*ii);
if (currentWaypoint->getVisitStatus() == status_visited)
continue;
currentWaypoint->setVisitStatus(status_visited);
WaypointMarker::EdgePool::iterator edgeIter;
WaypointMarker::EdgePool ¤tEdges = currentWaypoint->getEdges();
for (edgeIter = currentEdges.begin(); edgeIter != currentEdges.end(); ++edgeIter)
{
if (edgeIter->target->getVisitStatus() == status_visited)
continue;
float destX = edgeIter->target->getPosition().getX();
float destY = edgeIter->target->getPosition().getY();
float sourceX = (*ii)->getPosition().getX();
float sourceY = (*ii)->getPosition().getY();
fprintf(
fout,
"addedge %f %f %f %f\n",
destX,
destY,
sourceX,
sourceY);
}
}
fprintf(fout,
"border_left %f\n"
"border_right %f\n"
"border_top %f\n"
"border_bottom %f\n",
(float)leftBorder, (float)rightBorder, (float)topBorder, (float)bottomBorder);
fprintf(fout, "spawn_players\n");
}
bool gameFrozen = false;double FrozenTime = 0;double TimeofFreeze = 0;double TimeofUnfreeze = 0;
SERVER_CONSOLE_COMMAND(freeze){ if(gameFrozen) return; gameFrozen = true; TimeofFreeze = (double)SDL_GetTicks() / 1000;}
SERVER_CONSOLE_COMMAND(unfreeze){ if(!gameFrozen) return; gameFrozen = false; TimeofUnfreeze = (double)SDL_GetTicks() / 1000; FrozenTime +=TimeofUnfreeze-TimeofFreeze;}SERVER_CONSOLE_COMMAND(pause){ if(!gameFrozen) { TimeofFreeze = (double)SDL_GetTicks() / 1000; } else { TimeofUnfreeze = (double)SDL_GetTicks() / 1000; FrozenTime += TimeofUnfreeze-TimeofFreeze; } gameFrozen = !gameFrozen;}
SERVER_CONSOLE_COMMAND(display_time){ printfToClient(who, "Time is (%f), dt is (%f)",getTime(),getDt());}