1 | /* |
1 | /* |
2 | * static char *rcsid_map_c = |
2 | * static char *rcsid_map_c = |
3 | * "$Id: map.C,v 1.5 2006/08/20 20:57:34 elmex Exp $"; |
3 | * "$Id: map.C,v 1.6 2006/08/21 02:55:10 elmex 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 | |
… | |
… | |
1220 | * (this should have been updated when first loaded) |
1220 | * (this should have been updated when first loaded) |
1221 | */ |
1221 | */ |
1222 | |
1222 | |
1223 | int new_save_map(mapstruct *m, int flag) { |
1223 | int new_save_map(mapstruct *m, int flag) { |
1224 | FILE *fp, *fp2; |
1224 | FILE *fp, *fp2; |
1225 | char filename[MAX_BUF],buf[MAX_BUF], shop[MAX_BUF]; |
1225 | char filename[MAX_BUF],buf[MAX_BUF], buf_s[MAX_BUF], |
|
|
1226 | shop[MAX_BUF], filename_s[MAX_BUF]; |
1226 | int i; |
1227 | int i; |
1227 | |
1228 | |
1228 | if (flag && !*m->path) { |
1229 | if (flag && !*m->path) { |
1229 | LOG(llevError,"Tried to save map without path.\n"); |
1230 | LOG(llevError,"Tried to save map without path.\n"); |
1230 | return -1; |
1231 | return -1; |
… | |
… | |
1251 | } else { |
1252 | } else { |
1252 | if (!m->tmpname) |
1253 | if (!m->tmpname) |
1253 | m->tmpname = tempnam_local(settings.tmpdir,NULL); |
1254 | m->tmpname = tempnam_local(settings.tmpdir,NULL); |
1254 | strcpy(filename, m->tmpname); |
1255 | strcpy(filename, m->tmpname); |
1255 | } |
1256 | } |
1256 | LOG(llevDebug,"Saving map %s\n",m->path); |
1257 | LOG(llevDebug,"Saving map %s to %s\n", m->path, filename); |
1257 | m->in_memory = MAP_SAVING; |
1258 | m->in_memory = MAP_SAVING; |
1258 | |
1259 | |
1259 | unlink (filename); // do not overwrite backups if done via hardlinks |
1260 | sprintf (filename_s, "%s~", filename); |
1260 | |
1261 | |
1261 | /* Compress if it isn't a temporary save. Do compress if unique */ |
1262 | /* Compress if it isn't a temporary save. Do compress if unique */ |
1262 | if (m->compressed && (m->unique || m->templatemap || flag)) { |
1263 | if (m->compressed && (m->unique || m->templatemap || flag)) { |
1263 | char buf[MAX_BUF]; |
1264 | char buf[MAX_BUF]; |
1264 | strcpy(buf, uncomp[m->compressed][2]); |
1265 | strcpy(buf, uncomp[m->compressed][2]); |
1265 | strcat(buf, " > "); |
1266 | strcat(buf, " > "); |
1266 | strcat(buf, filename); |
1267 | strcat(buf, filename_s); |
1267 | fp = popen(buf, "w"); |
1268 | fp = popen(buf, "w"); |
1268 | } else |
1269 | } else |
1269 | fp = fopen(filename, "w"); |
1270 | fp = fopen(filename_s, "w"); |
1270 | |
1271 | |
1271 | if(fp == NULL) { |
1272 | if(fp == NULL) { |
1272 | LOG(llevError, "Cannot write %s: %s\n", filename, strerror_local(errno)); |
1273 | LOG(llevError, "Cannot write %s: %s\n", filename_s, strerror_local(errno)); |
1273 | return -1; |
1274 | return -1; |
1274 | } |
1275 | } |
1275 | |
1276 | |
1276 | /* legacy */ |
1277 | /* legacy */ |
1277 | fprintf(fp,"arch map\n"); |
1278 | fprintf(fp,"arch map\n"); |
… | |
… | |
1327 | /* In the game save unique items in the different file, but |
1328 | /* In the game save unique items in the different file, but |
1328 | * in the editor save them to the normal map file. |
1329 | * in the editor save them to the normal map file. |
1329 | * If unique map, save files in the proper destination (set by |
1330 | * If unique map, save files in the proper destination (set by |
1330 | * player) |
1331 | * player) |
1331 | */ |
1332 | */ |
1332 | fp2 = fp; /* save unique items into fp2 */ |
|
|
1333 | if ((flag == 0 || flag == 2) && !m->unique && !m->templatemap) { |
1333 | if ((flag == 0 || flag == 2) && !m->unique && !m->templatemap) { |
1334 | sprintf (buf,"%s.v00",create_items_path (m->path)); |
1334 | sprintf (buf,"%s.v00",create_items_path (m->path)); |
1335 | unlink (buf); // unlink here too, so that we have backups when using hard links |
1335 | LOG(llevDebug,"Saving unique items of map %s to %s\n", m->path, buf); |
|
|
1336 | sprintf (buf_s, "%s~", buf); |
1336 | if ((fp2 = fopen (buf, "w")) == NULL) { |
1337 | if ((fp2 = fopen (buf_s, "w")) == NULL) { |
1337 | LOG(llevError, "Can't open unique items file %s\n", buf); |
1338 | LOG(llevError, "Can't open unique items file %s\n", buf_s); |
1338 | } |
1339 | } |
1339 | if (flag == 2) |
1340 | if (flag == 2) |
1340 | save_objects(m, fp, fp2, 2); |
1341 | save_objects(m, fp, fp2, 2); |
1341 | else |
1342 | else |
1342 | save_objects (m, fp, fp2, 0); |
1343 | save_objects (m, fp, fp2, 0); |
1343 | if (fp2 != NULL) { |
1344 | if (fp2 != NULL) { |
1344 | if (ftell (fp2) == 0) { |
1345 | if (ftell (fp2) == 0) { |
1345 | fclose (fp2); |
1346 | fclose (fp2); |
|
|
1347 | rename (buf_s, buf); |
1346 | unlink (buf); |
1348 | unlink (buf); |
1347 | } else { |
1349 | } else { |
1348 | fclose (fp2); |
1350 | fclose (fp2); |
|
|
1351 | rename (buf_s, buf); |
1349 | chmod (buf, SAVE_MODE); |
1352 | chmod (buf, SAVE_MODE); |
1350 | } |
1353 | } |
1351 | } |
1354 | } |
1352 | } else { /* save same file when not playing, like in editor */ |
1355 | } else { /* save same file when not playing, like in editor */ |
1353 | save_objects(m, fp, fp, 0); |
1356 | save_objects(m, fp, fp, 0); |
… | |
… | |
1355 | |
1358 | |
1356 | if (m->compressed && (m->unique || m->templatemap || flag)) |
1359 | if (m->compressed && (m->unique || m->templatemap || flag)) |
1357 | pclose(fp); |
1360 | pclose(fp); |
1358 | else |
1361 | else |
1359 | fclose(fp); |
1362 | fclose(fp); |
|
|
1363 | |
|
|
1364 | rename (filename_s, filename); |
1360 | |
1365 | |
1361 | chmod (filename, SAVE_MODE); |
1366 | chmod (filename, SAVE_MODE); |
1362 | return 0; |
1367 | return 0; |
1363 | } |
1368 | } |
1364 | |
1369 | |
… | |
… | |
1828 | if (QUERY_FLAG(tmp,FLAG_NO_MAGIC)) |
1833 | if (QUERY_FLAG(tmp,FLAG_NO_MAGIC)) |
1829 | flags |= P_NO_MAGIC; |
1834 | flags |= P_NO_MAGIC; |
1830 | if (QUERY_FLAG(tmp,FLAG_DAMNED)) |
1835 | if (QUERY_FLAG(tmp,FLAG_DAMNED)) |
1831 | flags |= P_NO_CLERIC; |
1836 | flags |= P_NO_CLERIC; |
1832 | if (tmp->type == SAFE_GROUND) |
1837 | if (tmp->type == SAFE_GROUND) |
1833 | flags |= P_SAFE; |
1838 | flags |= P_SAFE | P_NO_CLERIC | P_NO_MAGIC; |
1834 | |
1839 | |
1835 | if (QUERY_FLAG(tmp,FLAG_BLOCKSVIEW)) |
1840 | if (QUERY_FLAG(tmp,FLAG_BLOCKSVIEW)) |
1836 | flags |= P_BLOCKSVIEW; |
1841 | flags |= P_BLOCKSVIEW; |
1837 | } /* for stack of objects */ |
1842 | } /* for stack of objects */ |
1838 | |
1843 | |