Repository navigation
Expand file tree
/
Copy pathMapSaver.as
More file actions
493 lines (389 loc) · 13.8 KB
/
Copy pathMapSaver.as
File metadata and controls
493 lines (389 loc) · 13.8 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// SonantDread & Gingerbeard @ November 14th 2024
/*
Map Saving
This tool saves the entire map so that it can be played at a later time.
No longer will you have to worry about all your progress being lost due to crashes or network outages,
as you can just simply save the game and return later.
For an example of a complete implementation, visit Zombies Reborn- which is the mod this tool was originally created for.
https://discord.gg/V29BBeba3C
https://github.com/Gingerbeard5773/Zombies_Reborn
Errors from this script typically mean that a save file is corrupted or outdated!
*/
#include "MapSaverCommon.as"
const string SaveFile = "MapSave_"; //MODIFY THIS TO ENSURE NO OTHER MODS OVERWRITE
// store tiles using run line encoding
string SerializeTileData(CMap@ map)
{
string map_data = "";
u16 last_type = map.getTile(0).type;
u32 tile_count = 1;
const u32 tilemapsize = map.tilemapheight * map.tilemapwidth;
for (u32 i = 1; i < tilemapsize; i++)
{
const u16 type = map.getTile(i).type;
if (type == last_type)
{
tile_count++;
}
else
{
map_data += last_type + " " + tile_count + ";";
last_type = type;
tile_count = 1;
}
}
map_data += last_type + " " + tile_count + ";";
return map_data;
}
// store dirt background using run line encoding
string SerializeDirtData(CMap@ map)
{
string map_data = "";
bool was_dirt = map.getTile(0).dirt == 80;
u32 tile_count = 1;
const u32 tilemapsize = map.tilemapheight * map.tilemapwidth;
for (u32 i = 1; i < tilemapsize; i++)
{
const bool is_dirt = map.getTile(i).dirt == 80;
if (is_dirt == was_dirt)
{
tile_count++;
}
else
{
map_data += (was_dirt ? "1" : "0") + " " + tile_count + ";";
was_dirt = is_dirt;
tile_count = 1;
}
}
map_data += (was_dirt ? "1" : "0") + " " + tile_count + ";";
return map_data;
}
// store water using run line encoding
string SerializeWaterData(CMap@ map)
{
string map_data = "";
bool was_water = map.isInWater(map.getTileWorldPosition(0));
u32 tile_count = 1;
const u32 tilemapsize = map.tilemapheight * map.tilemapwidth;
for (u32 i = 1; i < tilemapsize; i++)
{
const bool has_water = map.isInWater(map.getTileWorldPosition(i));
if (was_water == has_water)
{
tile_count++;
}
else
{
map_data += (was_water ? "1" : "0") + " " + tile_count + ";";
was_water = has_water;
tile_count = 1;
}
}
map_data += (was_water ? "1" : "0") + " " + tile_count + ";";
return map_data;
}
string SerializeBlobData(u16[]@ saved_netids)
{
CBlob@[] blobs;
getBlobs(@blobs);
string blob_data = "";
for (int i = 0; i < blobs.length; i++)
{
CBlob@ blob = blobs[i];
if (!canSaveBlob(blob)) continue;
saved_netids.push_back(blob.getNetworkID());
BlobDataHandler@ handler = getBlobHandler(blob.getName());
const string data = handler.Serialize(blob);
if (!data.isEmpty())
{
blob_data += data + ";"; // extra semicolon to seperate each blob
}
}
return blob_data;
}
string SerializeInventoryData(u16[]@ saved_netids)
{
string inventory_data;
for (u16 i = 0; i < saved_netids.length; i++)
{
CBlob@ blob = getBlobByNetworkID(saved_netids[i]);
if (blob is null) continue;
CBlob@ parent = blob.getInventoryBlob();
if (parent is null) continue;
const int parent_index = saved_netids.find(parent.getNetworkID());
if (parent_index == -1) continue;
inventory_data += i + " " + parent_index + ";";
}
return inventory_data;
}
string SerializeAttachmentData(u16[]@ saved_netids)
{
string attachment_data;
for (u16 i = 0; i < saved_netids.length; i++)
{
CBlob@ blob = getBlobByNetworkID(saved_netids[i]);
if (blob is null) continue;
AttachmentPoint@[] aps;
if (!blob.getAttachmentPoints(@aps)) continue;
for (u16 a = 0; a < aps.length; a++)
{
AttachmentPoint@ ap = aps[a];
CBlob@ occupied = ap.getOccupied();
if (occupied is null) continue;
const int occupied_index = saved_netids.find(occupied.getNetworkID());
if (occupied_index == -1) continue;
attachment_data += i + " " + occupied_index + " " + a + ";";
}
}
return attachment_data;
}
string SerializeDamageOwnerPlayerData(u16[]@ saved_netids)
{
string owner_data;
string[] player_names;
u16[][] blob_indexes;
for (u16 i = 0; i < saved_netids.length; i++)
{
CBlob@ blob = getBlobByNetworkID(saved_netids[i]);
if (blob is null) continue;
string username = blob.exists("damage_owner") ? blob.get_string("damage_owner") : "";
CPlayer@ player = blob.getDamageOwnerPlayer();
if (player !is null && !player.isBot() && blob !is player.getBlob())
{
username = player.getUsername();
}
if (username.isEmpty()) continue;
int player_index = player_names.find(username);
if (player_index == -1)
{
player_names.push_back(username);
player_index = player_names.length - 1;
blob_indexes.set_length(player_names.length);
}
blob_indexes[player_index].push_back(i);
}
for (int i = 0; i < player_names.length; i++)
{
if (blob_indexes[i].length == 0) continue;
owner_data += player_names[i] + "{";
for (int b = 0; b < blob_indexes[i].length; b++)
{
owner_data += blob_indexes[i][b] + ";";
}
owner_data += "}";
}
return owner_data;
}
void SaveMap(CRules@ this, CMap@ map, const string&in save_slot = "AutoSave")
{
InitializeBlobHandlers();
ConfigFile@ config = ConfigFile();
// collect all map data
const string map_dimensions = map.tilemapwidth + ";" + map.tilemapheight;
const string map_data = SerializeTileData(map);
const string dirt_data = SerializeDirtData(map);
const string water_data = SerializeWaterData(map);
// collect all blob data
u16[] saved_netids;
const string blob_data = SerializeBlobData(@saved_netids);
const string inventory_data = SerializeInventoryData(@saved_netids);
const string attachment_data = SerializeAttachmentData(@saved_netids);
const string owner_data = SerializeDamageOwnerPlayerData(@saved_netids);
// collect rules data
CRules@ rules = getRules();
const f32 day_time = map.getDayTime();
// save data to config file
config.add_string("map_dimensions", map_dimensions);
config.add_string("map_data", map_data);
config.add_string("dirt_data", dirt_data);
config.add_string("water_data", water_data);
config.add_string("blob_data", blob_data);
config.add_string("inventory_data", inventory_data);
config.add_string("attachment_data", attachment_data);
config.add_string("owner_data", owner_data);
config.add_f32("day_time", day_time);
config.saveFile(SaveFile + save_slot);
blobHandlers.deleteAll();
}
/*
Loading is divided into two parts.
LoadSavedMap: called before rules scripts are initialized
LoadSavedRules: called after rules scripts are initialized- for overwriting variables set onRestart in rules scripts
*/
bool LoadSavedMap(CRules@ this, CMap@ map)
{
if (this.get_bool("loaded_saved_map")) return false;
if (!isServer()) return true;
const string save_slot = this.exists("mapsaver_save_slot") ? this.get_string("mapsaver_save_slot") : "AutoSave";
ConfigFile config = ConfigFile();
if (!config.loadFile("../Cache/" + SaveFile + save_slot)) return false;
if (!config.exists("map_dimensions")) return false;
const string[]@ map_dimensions = config.read_string("map_dimensions").split(";");
if (map_dimensions.length < 2) return false;
const int width = parseInt(map_dimensions[0]);
const int height = parseInt(map_dimensions[1]);
const string map_data = config.read_string("map_data", "");
const string water_data = config.read_string("water_data", "");
const string blob_data = config.read_string("blob_data", "");
const string inventory_data = config.read_string("inventory_data", "");
const string attachment_data = config.read_string("attachment_data", "");
const string owner_data = config.read_string("owner_data", "");
map.CreateTileMap(width, height, 8.0f, "Sprites/world.png");
InitializeBlobHandlers();
LoadTiles(map, map_data);
LoadWater(map, water_data);
CBlob@[] loaded_blobs;
LoadBlobs(map, blob_data, @loaded_blobs);
LoadInventories(map, inventory_data, @loaded_blobs);
LoadAttachments(map, attachment_data, @loaded_blobs);
LoadDamageOwnerPlayers(owner_data, @loaded_blobs);
blobHandlers.deleteAll();
return true;
}
bool LoadSavedRules(CRules@ this, CMap@ map)
{
if (this.get_bool("loaded_saved_map")) return false;
if (!isServer()) return true;
const string save_slot = this.exists("mapsaver_save_slot") ? this.get_string("mapsaver_save_slot") : "AutoSave";
ConfigFile config = ConfigFile();
if (!config.loadFile("../Cache/" + SaveFile + save_slot)) return false;
const string dirt_data = config.read_string("dirt_data", "");
const f32 day_time = config.read_f32("day_time", 0.2f);
map.SetDayTime(day_time);
//dirt data has to be loaded late because of an engine issue..
LoadDirt(map, dirt_data);
this.set_bool("loaded_saved_map", true);
return true;
}
void LoadTiles(CMap@ map, const string&in map_data)
{
const string[]@ tiles = map_data.split(";");
u32 current_index = 0;
for (int i = 0; i < tiles.length - 1; i++)
{
string[]@ data = tiles[i].split(" ");
if (data.length != 2) { error("MapSaver: Failed tile indices"); continue; }
const int tile_type = parseInt(data[0]);
const int tile_count = parseInt(data[1]);
for (int j = 0; j < tile_count; j++)
{
map.SetTile(current_index++, tile_type);
}
}
}
void LoadDirt(CMap@ map, const string&in map_data)
{
const string[]@ tiles = map_data.split(";");
u32 current_index = 0;
for (int i = 0; i < tiles.length - 1; i++)
{
string[]@ data = tiles[i].split(" ");
if (data.length != 2) { error("MapSaver: Failed dirt indices"); continue; }
const bool is_dirt = parseBool(data[0]);
const int tile_count = parseInt(data[1]);
for (int j = 0; j < tile_count; j++)
{
if (is_dirt)
{
map.RemoveTileFlag(current_index, Tile::LIGHT_SOURCE);
map.SetTileDirt(current_index, 80);
}
current_index++;
}
}
}
void LoadWater(CMap@ map, const string&in map_data)
{
const string[]@ tiles = map_data.split(";");
u32 current_index = 0;
for (int i = 0; i < tiles.length - 1; i++)
{
string[]@ data = tiles[i].split(" ");
if (data.length != 2) { error("MapSaver: Failed water indices"); continue; }
const bool has_water = parseBool(data[0]);
const int tile_count = parseInt(data[1]);
for (int j = 0; j < tile_count; j++)
{
map.server_setFloodWaterOffset(current_index++, has_water);
}
}
}
void LoadBlobs(CMap@ map, const string&in blob_data, CBlob@[]@ loaded_blobs)
{
// each blob is separated by 2x semicolon
const string[]@ blobs = blob_data.split(";;");
for (int i = 0; i < blobs.length; i++)
{
if (blobs[i].isEmpty()) continue;
string[]@ data = blobs[i].split(";");
if (data.length < 3) { error("MapSaver: Failed indexing for blob data"); continue; }
const string name = data[0];
const Vec2f pos(parseFloat(data[1]), parseFloat(data[2]));
BlobDataHandler@ handler = getBlobHandler(name);
CBlob@ blob = handler.CreateBlob(name, pos, data);
loaded_blobs.push_back(blob);
if (blob is null) { error("MapSaver: Failed to load blob '"+name+"'"); continue; }
handler.LoadBlobData(blob, data);
}
}
void LoadInventories(CMap@ map, const string&in inventory_data, CBlob@[]@ loaded_blobs)
{
const string[]@ pairs = inventory_data.split(";");
for (int i = 0; i < pairs.length - 1; i++)
{
const string[]@ indices = pairs[i].split(" ");
if (indices.length != 2) { error("MapSaver: Failed inventory indices"); continue; }
const int blob_index = parseInt(indices[0]);
const int parent_index = parseInt(indices[1]);
if (blob_index >= loaded_blobs.length || parent_index >= loaded_blobs.length) { error("MapSaver: Failed inventory indices [out of bounds]"); continue; }
CBlob@ blob = loaded_blobs[blob_index];
CBlob@ parent = loaded_blobs[parent_index];
if (blob is null || parent is null) continue;
parent.server_PutInInventory(blob);
}
}
void LoadAttachments(CMap@ map, const string&in attachment_data, CBlob@[]@ loaded_blobs)
{
const string[]@ pairs = attachment_data.split(";");
for (int i = 0; i < pairs.length - 1; i++)
{
const string[]@ indices = pairs[i].split(" ");
if (indices.length != 3) { error("MapSaver: Failed attachment indices"); continue; }
const int blob_index = parseInt(indices[0]);
const int parent_index = parseInt(indices[1]);
if (blob_index >= loaded_blobs.length || parent_index >= loaded_blobs.length) { error("MapSaver: Failed attachment indices [out of bounds]"); continue; }
CBlob@ blob = loaded_blobs[blob_index];
CBlob@ parent = loaded_blobs[parent_index];
if (blob is null || parent is null) continue;
const int ap_index = parseInt(indices[2]);
blob.server_AttachTo(parent, ap_index);
}
}
void LoadDamageOwnerPlayers(const string&in owner_data, CBlob@[]@ loaded_blobs)
{
const string[]@ players = owner_data.split("}");
for (int p = 0; p < players.length - 1; p++)
{
const string[]@ owner_compartments = players[p].split("{");
if (owner_compartments.length != 2) { error("MapSaver: Failed owner compartments"); continue; }
const string player_name = owner_compartments[0];
CPlayer@ player = getPlayerByUsername(player_name);
const string[]@ blob_indexes = owner_compartments[1].split(";");
for (int i = 0; i < blob_indexes.length - 1; i++)
{
const int blob_index = parseInt(blob_indexes[i]);
if (blob_index >= loaded_blobs.length) { error("MapSaver: Failed owner [out of bounds]"); continue; }
CBlob@ blob = loaded_blobs[blob_index];
if (blob is null) continue;
if (player !is null)
{
blob.SetDamageOwnerPlayer(player);
}
else
{
blob.set_string("damage_owner", player_name);
}
}
}
}