1 | /* |
1 | /* |
2 | * static char *rcsid_map_c = |
2 | * static char *rcsid_map_c = |
3 | * "$Id: map.C,v 1.4 2006/08/15 18:12:20 elmex Exp $"; |
3 | * "$Id: map.C,v 1.8 2006/08/26 08:44:04 root Exp $"; |
4 | */ |
4 | */ |
5 | |
5 | |
6 | /* |
6 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
7 | CrossFire, A Multiplayer game for X-windows |
8 | |
8 | |
… | |
… | |
37 | |
37 | |
38 | #include "path.h" |
38 | #include "path.h" |
39 | |
39 | |
40 | |
40 | |
41 | extern int nrofallocobjects,nroffreeobjects; |
41 | extern int nrofallocobjects,nroffreeobjects; |
42 | |
|
|
43 | void (*load_original_map_callback)(mapstruct *map); |
|
|
44 | void (*load_temporary_map_callback)(mapstruct *map); |
|
|
45 | void (*clean_temporary_map_callback)(mapstruct *map); |
|
|
46 | |
42 | |
47 | /* |
43 | /* |
48 | * Returns the mapstruct which has a name matching the given argument. |
44 | * Returns the mapstruct which has a name matching the given argument. |
49 | * return NULL if no match is found. |
45 | * return NULL if no match is found. |
50 | */ |
46 | */ |
… | |
… | |
1055 | close_and_delete(fp, comp); |
1051 | close_and_delete(fp, comp); |
1056 | m->in_memory=MAP_IN_MEMORY; |
1052 | m->in_memory=MAP_IN_MEMORY; |
1057 | if (!MAP_DIFFICULTY(m)) |
1053 | if (!MAP_DIFFICULTY(m)) |
1058 | MAP_DIFFICULTY(m)=calculate_difficulty(m); |
1054 | MAP_DIFFICULTY(m)=calculate_difficulty(m); |
1059 | set_map_reset_time(m); |
1055 | set_map_reset_time(m); |
1060 | if (load_original_map_callback) |
1056 | INVOKE_MAP (LOAD, m); |
1061 | load_original_map_callback(m); |
|
|
1062 | return (m); |
1057 | return (m); |
1063 | } |
1058 | } |
1064 | |
1059 | |
1065 | /* |
1060 | /* |
1066 | * Loads a map, which has been loaded earlier, from file. |
1061 | * Loads a map, which has been loaded earlier, from file. |
… | |
… | |
1106 | |
1101 | |
1107 | m->in_memory=MAP_LOADING; |
1102 | m->in_memory=MAP_LOADING; |
1108 | load_objects (m, fp, 0); |
1103 | load_objects (m, fp, 0); |
1109 | close_and_delete(fp, comp); |
1104 | close_and_delete(fp, comp); |
1110 | m->in_memory=MAP_IN_MEMORY; |
1105 | m->in_memory=MAP_IN_MEMORY; |
1111 | if (load_temporary_map_callback) |
1106 | INVOKE_MAP (SWAPIN, m); |
1112 | load_temporary_map_callback(m); |
|
|
1113 | return m; |
1107 | return m; |
1114 | } |
1108 | } |
1115 | |
1109 | |
1116 | /* |
1110 | /* |
1117 | * Loads a map, which has been loaded earlier, from file. |
1111 | * Loads a map, which has been loaded earlier, from file. |
… | |
… | |
1220 | * (this should have been updated when first loaded) |
1214 | * (this should have been updated when first loaded) |
1221 | */ |
1215 | */ |
1222 | |
1216 | |
1223 | int new_save_map(mapstruct *m, int flag) { |
1217 | int new_save_map(mapstruct *m, int flag) { |
1224 | FILE *fp, *fp2; |
1218 | FILE *fp, *fp2; |
1225 | char filename[MAX_BUF],buf[MAX_BUF], shop[MAX_BUF]; |
1219 | char filename[MAX_BUF],buf[MAX_BUF], buf_s[MAX_BUF], |
|
|
1220 | shop[MAX_BUF], filename_s[MAX_BUF]; |
1226 | int i; |
1221 | int i; |
1227 | |
1222 | |
1228 | if (flag && !*m->path) { |
1223 | if (flag && !*m->path) { |
1229 | LOG(llevError,"Tried to save map without path.\n"); |
1224 | LOG(llevError,"Tried to save map without path.\n"); |
1230 | return -1; |
1225 | return -1; |
… | |
… | |
1251 | } else { |
1246 | } else { |
1252 | if (!m->tmpname) |
1247 | if (!m->tmpname) |
1253 | m->tmpname = tempnam_local(settings.tmpdir,NULL); |
1248 | m->tmpname = tempnam_local(settings.tmpdir,NULL); |
1254 | strcpy(filename, m->tmpname); |
1249 | strcpy(filename, m->tmpname); |
1255 | } |
1250 | } |
1256 | LOG(llevDebug,"Saving map %s\n",m->path); |
1251 | LOG(llevDebug,"Saving map %s to %s\n", m->path, filename); |
1257 | m->in_memory = MAP_SAVING; |
1252 | m->in_memory = MAP_SAVING; |
1258 | |
1253 | |
1259 | unlink (filename); // do not overwrite backups if done via hardlinks |
1254 | sprintf (filename_s, "%s~", filename); |
1260 | |
1255 | |
1261 | /* Compress if it isn't a temporary save. Do compress if unique */ |
1256 | /* Compress if it isn't a temporary save. Do compress if unique */ |
1262 | if (m->compressed && (m->unique || m->templatemap || flag)) { |
1257 | if (m->compressed && (m->unique || m->templatemap || flag)) { |
1263 | char buf[MAX_BUF]; |
1258 | char buf[MAX_BUF]; |
1264 | strcpy(buf, uncomp[m->compressed][2]); |
1259 | strcpy(buf, uncomp[m->compressed][2]); |
1265 | strcat(buf, " > "); |
1260 | strcat(buf, " > "); |
1266 | strcat(buf, filename); |
1261 | strcat(buf, filename_s); |
1267 | fp = popen(buf, "w"); |
1262 | fp = popen(buf, "w"); |
1268 | } else |
1263 | } else |
1269 | fp = fopen(filename, "w"); |
1264 | fp = fopen(filename_s, "w"); |
1270 | |
1265 | |
1271 | if(fp == NULL) { |
1266 | if(fp == NULL) { |
1272 | LOG(llevError, "Cannot write %s: %s\n", filename, strerror_local(errno)); |
1267 | LOG(llevError, "Cannot write %s: %s\n", filename_s, strerror_local(errno)); |
1273 | return -1; |
1268 | return -1; |
1274 | } |
1269 | } |
1275 | |
1270 | |
1276 | /* legacy */ |
1271 | /* legacy */ |
1277 | fprintf(fp,"arch map\n"); |
1272 | fprintf(fp,"arch map\n"); |
… | |
… | |
1327 | /* In the game save unique items in the different file, but |
1322 | /* In the game save unique items in the different file, but |
1328 | * in the editor save them to the normal map file. |
1323 | * in the editor save them to the normal map file. |
1329 | * If unique map, save files in the proper destination (set by |
1324 | * If unique map, save files in the proper destination (set by |
1330 | * player) |
1325 | * player) |
1331 | */ |
1326 | */ |
1332 | fp2 = fp; /* save unique items into fp2 */ |
|
|
1333 | if ((flag == 0 || flag == 2) && !m->unique && !m->templatemap) { |
1327 | if ((flag == 0 || flag == 2) && !m->unique && !m->templatemap) { |
1334 | sprintf (buf,"%s.v00",create_items_path (m->path)); |
1328 | sprintf (buf,"%s.v00",create_items_path (m->path)); |
|
|
1329 | sprintf (buf_s, "%s~", buf); |
1335 | if ((fp2 = fopen (buf, "w")) == NULL) { |
1330 | if ((fp2 = fopen (buf_s, "w")) == NULL) { |
1336 | LOG(llevError, "Can't open unique items file %s\n", buf); |
1331 | LOG(llevError, "Can't open unique items file %s\n", buf_s); |
1337 | } |
1332 | } |
1338 | if (flag == 2) |
1333 | if (flag == 2) |
1339 | save_objects(m, fp, fp2, 2); |
1334 | save_objects(m, fp, fp2, 2); |
1340 | else |
1335 | else |
1341 | save_objects (m, fp, fp2, 0); |
1336 | save_objects (m, fp, fp2, 0); |
1342 | if (fp2 != NULL) { |
1337 | if (fp2 != NULL) { |
1343 | if (ftell (fp2) == 0) { |
1338 | if (ftell (fp2) == 0) { |
1344 | fclose (fp2); |
1339 | fclose (fp2); |
|
|
1340 | rename (buf_s, buf); |
1345 | unlink (buf); |
1341 | unlink (buf); |
1346 | } else { |
1342 | } else { |
1347 | fclose (fp2); |
1343 | fclose (fp2); |
|
|
1344 | rename (buf_s, buf); |
1348 | chmod (buf, SAVE_MODE); |
1345 | chmod (buf, SAVE_MODE); |
1349 | } |
1346 | } |
1350 | } |
1347 | } |
1351 | } else { /* save same file when not playing, like in editor */ |
1348 | } else { /* save same file when not playing, like in editor */ |
1352 | save_objects(m, fp, fp, 0); |
1349 | save_objects(m, fp, fp, 0); |
… | |
… | |
1354 | |
1351 | |
1355 | if (m->compressed && (m->unique || m->templatemap || flag)) |
1352 | if (m->compressed && (m->unique || m->templatemap || flag)) |
1356 | pclose(fp); |
1353 | pclose(fp); |
1357 | else |
1354 | else |
1358 | fclose(fp); |
1355 | fclose(fp); |
|
|
1356 | |
|
|
1357 | rename (filename_s, filename); |
1359 | |
1358 | |
1360 | chmod (filename, SAVE_MODE); |
1359 | chmod (filename, SAVE_MODE); |
1361 | return 0; |
1360 | return 0; |
1362 | } |
1361 | } |
1363 | |
1362 | |
… | |
… | |
1676 | } |
1675 | } |
1677 | |
1676 | |
1678 | void clean_tmp_map(mapstruct *m) { |
1677 | void clean_tmp_map(mapstruct *m) { |
1679 | if(m->tmpname == NULL) |
1678 | if(m->tmpname == NULL) |
1680 | return; |
1679 | return; |
1681 | if (clean_temporary_map_callback) |
1680 | INVOKE_MAP (CLEAN, m); |
1682 | clean_temporary_map_callback (m); |
|
|
1683 | (void) unlink(m->tmpname); |
1681 | (void) unlink(m->tmpname); |
1684 | } |
1682 | } |
1685 | |
1683 | |
1686 | void free_all_maps(void) |
1684 | void free_all_maps(void) |
1687 | { |
1685 | { |
… | |
… | |
1827 | if (QUERY_FLAG(tmp,FLAG_NO_MAGIC)) |
1825 | if (QUERY_FLAG(tmp,FLAG_NO_MAGIC)) |
1828 | flags |= P_NO_MAGIC; |
1826 | flags |= P_NO_MAGIC; |
1829 | if (QUERY_FLAG(tmp,FLAG_DAMNED)) |
1827 | if (QUERY_FLAG(tmp,FLAG_DAMNED)) |
1830 | flags |= P_NO_CLERIC; |
1828 | flags |= P_NO_CLERIC; |
1831 | if (tmp->type == SAFE_GROUND) |
1829 | if (tmp->type == SAFE_GROUND) |
1832 | flags |= P_SAFE; |
1830 | flags |= P_SAFE | P_NO_CLERIC | P_NO_MAGIC; |
1833 | |
1831 | |
1834 | if (QUERY_FLAG(tmp,FLAG_BLOCKSVIEW)) |
1832 | if (QUERY_FLAG(tmp,FLAG_BLOCKSVIEW)) |
1835 | flags |= P_BLOCKSVIEW; |
1833 | flags |= P_BLOCKSVIEW; |
1836 | } /* for stack of objects */ |
1834 | } /* for stack of objects */ |
1837 | |
1835 | |