… | |
… | |
329 | * carrying. |
329 | * carrying. |
330 | */ |
330 | */ |
331 | sum_weight (container); |
331 | sum_weight (container); |
332 | } |
332 | } |
333 | |
333 | |
|
|
334 | void |
|
|
335 | maptile::set_object_flag (int flag, int value) |
|
|
336 | { |
|
|
337 | if (!spaces) |
|
|
338 | return; |
|
|
339 | |
|
|
340 | for (mapspace *ms = spaces + size (); ms-- > spaces; ) |
|
|
341 | for (object *tmp = ms->bot; tmp; tmp = tmp->above) |
|
|
342 | tmp->flag [flag] = value; |
|
|
343 | } |
|
|
344 | |
334 | /* link_multipart_objects go through all the objects on the map looking |
345 | /* link_multipart_objects go through all the objects on the map looking |
335 | * for objects whose arch says they are multipart yet according to the |
346 | * for objects whose arch says they are multipart yet according to the |
336 | * info we have, they only have the head (as would be expected when |
347 | * info we have, they only have the head (as would be expected when |
337 | * they are saved). We do have to look for the old maps that did save |
348 | * they are saved). We do have to look for the old maps that did save |
338 | * the more sections and not re-add sections for them. |
349 | * the more sections and not re-add sections for them. |
… | |
… | |
476 | } |
487 | } |
477 | |
488 | |
478 | bool |
489 | bool |
479 | maptile::save_objects (object_freezer &freezer, int flags) |
490 | maptile::save_objects (object_freezer &freezer, int flags) |
480 | { |
491 | { |
|
|
492 | static int cede_count = 0; |
|
|
493 | |
481 | if (flags & IO_HEADER) |
494 | if (flags & IO_HEADER) |
482 | save_header (freezer); |
495 | save_header (freezer); |
483 | |
496 | |
484 | if (!spaces) |
497 | if (!spaces) |
485 | return false; |
498 | return false; |
486 | |
499 | |
487 | for (int i = 0; i < size (); ++i) |
500 | for (int i = 0; i < size (); ++i) |
488 | { |
501 | { |
|
|
502 | if (cede_count >= 500) |
|
|
503 | { |
|
|
504 | cede_count = 0; |
|
|
505 | coroapi::cede (); |
|
|
506 | } |
|
|
507 | |
489 | int unique = 0; |
508 | int unique = 0; |
490 | for (object *op = spaces [i].bot; op; op = op->above) |
509 | for (object *op = spaces [i].bot; op; op = op->above) |
491 | { |
510 | { |
|
|
511 | // count per-object, but cede only when modification-safe |
|
|
512 | cede_count++; |
|
|
513 | |
492 | if (op->flag [FLAG_UNIQUE] && op->flag [FLAG_IS_FLOOR]) |
514 | if (op->flag [FLAG_UNIQUE] && op->flag [FLAG_IS_FLOOR]) |
493 | unique = 1; |
515 | unique = 1; |
494 | |
516 | |
495 | if (!op->can_map_save ()) |
517 | if (!op->can_map_save ()) |
496 | continue; |
518 | continue; |
… | |
… | |
1215 | |
1237 | |
1216 | /* If two top faces are already set, quit processing */ |
1238 | /* If two top faces are already set, quit processing */ |
1217 | if ((top != blank_face) && (middle != blank_face)) |
1239 | if ((top != blank_face) && (middle != blank_face)) |
1218 | break; |
1240 | break; |
1219 | |
1241 | |
1220 | /* Only show visible faces, unless its the editor - show all */ |
1242 | /* Only show visible faces */ |
1221 | if (!tmp->invisible || editor) |
1243 | if (!tmp->invisible) |
1222 | { |
1244 | { |
1223 | /* Fill in top if needed */ |
1245 | /* Fill in top if needed */ |
1224 | if (top == blank_face) |
1246 | if (top == blank_face) |
1225 | { |
1247 | { |
1226 | top = tmp->face; |
1248 | top = tmp->face; |
… | |
… | |
1256 | faces [0] = top; faces_obj [0] = top != blank_face ? top_obj : 0; |
1278 | faces [0] = top; faces_obj [0] = top != blank_face ? top_obj : 0; |
1257 | faces [1] = middle; faces_obj [1] = middle != blank_face ? middle_obj : 0; |
1279 | faces [1] = middle; faces_obj [1] = middle != blank_face ? middle_obj : 0; |
1258 | faces [2] = floor; faces_obj [2] = floor != blank_face ? floor_obj : 0; |
1280 | faces [2] = floor; faces_obj [2] = floor != blank_face ? floor_obj : 0; |
1259 | } |
1281 | } |
1260 | |
1282 | |
1261 | /* this updates the orig_map->tile_map[tile_num] value after loading |
1283 | /* this updates the orig_map->tile_map[tile_num] value after finding |
1262 | * the map. It also takes care of linking back the freshly loaded |
1284 | * the map. It also takes care of linking back the freshly found |
1263 | * maps tile_map values if it tiles back to this one. It returns |
1285 | * maps tile_map values if it tiles back to this one. It returns |
1264 | * the value of orig_map->tile_map[tile_num]. It really only does this |
1286 | * the value of orig_map->tile_map[tile_num]. |
1265 | * so that it is easier for calling functions to verify success. |
|
|
1266 | */ |
1287 | */ |
1267 | static maptile * |
1288 | static inline maptile * |
1268 | load_and_link_tiled_map (maptile *orig_map, int tile_num) |
1289 | find_and_link (maptile *orig_map, int tile_num) |
1269 | { |
1290 | { |
1270 | maptile *mp = orig_map->load_map_sync (orig_map->tile_path [tile_num], orig_map); |
1291 | maptile *mp = orig_map->find_sync (orig_map->tile_path [tile_num], orig_map); |
1271 | |
1292 | |
1272 | if (!mp || mp->in_memory != MAP_IN_MEMORY) |
1293 | if (!mp) |
1273 | { |
1294 | { |
1274 | // emergency mode, manufacture a dummy map, this creates a memleak, but thats fine |
1295 | // emergency mode, manufacture a dummy map, this creates a memleak, but thats fine |
1275 | LOG (llevError, "FATAL: cannot load tiled map %s from %s, leaking memory and worse!\n", |
1296 | LOG (llevError, "FATAL: cannot load tiled map %s from %s, leaking memory and worse!\n", |
1276 | &orig_map->tile_path[tile_num], &orig_map->path); |
1297 | &orig_map->tile_path[tile_num], &orig_map->path); |
1277 | mp = new maptile (1, 1); |
1298 | mp = new maptile (1, 1); |
… | |
… | |
1287 | //TODO: non-euclidean maps MUST GO |
1308 | //TODO: non-euclidean maps MUST GO |
1288 | if (orig_map->tile_map[tile_num]->tile_path[dest_tile] == orig_map->path) |
1309 | if (orig_map->tile_map[tile_num]->tile_path[dest_tile] == orig_map->path) |
1289 | orig_map->tile_map[tile_num]->tile_map[dest_tile] = orig_map; |
1310 | orig_map->tile_map[tile_num]->tile_map[dest_tile] = orig_map; |
1290 | |
1311 | |
1291 | return mp; |
1312 | return mp; |
|
|
1313 | } |
|
|
1314 | |
|
|
1315 | static inline void |
|
|
1316 | load_and_link (maptile *orig_map, int tile_num) |
|
|
1317 | { |
|
|
1318 | find_and_link (orig_map, tile_num)->load_sync (); |
1292 | } |
1319 | } |
1293 | |
1320 | |
1294 | /* this returns TRUE if the coordinates (x,y) are out of |
1321 | /* this returns TRUE if the coordinates (x,y) are out of |
1295 | * map m. This function also takes into account any |
1322 | * map m. This function also takes into account any |
1296 | * tiling considerations, loading adjacant maps as needed. |
1323 | * tiling considerations, loading adjacant maps as needed. |
… | |
… | |
1313 | { |
1340 | { |
1314 | if (!m->tile_path[3]) |
1341 | if (!m->tile_path[3]) |
1315 | return 1; |
1342 | return 1; |
1316 | |
1343 | |
1317 | if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY) |
1344 | if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY) |
1318 | load_and_link_tiled_map (m, 3); |
1345 | find_and_link (m, 3); |
1319 | |
1346 | |
1320 | return out_of_map (m->tile_map[3], x + m->tile_map[3]->width, y); |
1347 | return out_of_map (m->tile_map[3], x + m->tile_map[3]->width, y); |
1321 | } |
1348 | } |
1322 | |
1349 | |
1323 | if (x >= m->width) |
1350 | if (x >= m->width) |
1324 | { |
1351 | { |
1325 | if (!m->tile_path[1]) |
1352 | if (!m->tile_path[1]) |
1326 | return 1; |
1353 | return 1; |
1327 | |
1354 | |
1328 | if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY) |
1355 | if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY) |
1329 | load_and_link_tiled_map (m, 1); |
1356 | find_and_link (m, 1); |
1330 | |
1357 | |
1331 | return out_of_map (m->tile_map[1], x - m->width, y); |
1358 | return out_of_map (m->tile_map[1], x - m->width, y); |
1332 | } |
1359 | } |
1333 | |
1360 | |
1334 | if (y < 0) |
1361 | if (y < 0) |
1335 | { |
1362 | { |
1336 | if (!m->tile_path[0]) |
1363 | if (!m->tile_path[0]) |
1337 | return 1; |
1364 | return 1; |
1338 | |
1365 | |
1339 | if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY) |
1366 | if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY) |
1340 | load_and_link_tiled_map (m, 0); |
1367 | find_and_link (m, 0); |
1341 | |
1368 | |
1342 | return out_of_map (m->tile_map[0], x, y + m->tile_map[0]->height); |
1369 | return out_of_map (m->tile_map[0], x, y + m->tile_map[0]->height); |
1343 | } |
1370 | } |
1344 | |
1371 | |
1345 | if (y >= m->height) |
1372 | if (y >= m->height) |
1346 | { |
1373 | { |
1347 | if (!m->tile_path[2]) |
1374 | if (!m->tile_path[2]) |
1348 | return 1; |
1375 | return 1; |
1349 | |
1376 | |
1350 | if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY) |
1377 | if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY) |
1351 | load_and_link_tiled_map (m, 2); |
1378 | find_and_link (m, 2); |
1352 | |
1379 | |
1353 | return out_of_map (m->tile_map[2], x, y - m->height); |
1380 | return out_of_map (m->tile_map[2], x, y - m->height); |
1354 | } |
1381 | } |
1355 | |
1382 | |
1356 | /* Simple case - coordinates are within this local |
1383 | /* Simple case - coordinates are within this local |
… | |
… | |
1361 | |
1388 | |
1362 | /* This is basically the same as out_of_map above, but |
1389 | /* This is basically the same as out_of_map above, but |
1363 | * instead we return NULL if no map is valid (coordinates |
1390 | * instead we return NULL if no map is valid (coordinates |
1364 | * out of bounds and no tiled map), otherwise it returns |
1391 | * out of bounds and no tiled map), otherwise it returns |
1365 | * the map as that the coordinates are really on, and |
1392 | * the map as that the coordinates are really on, and |
1366 | * updates x and y to be the localized coordinates. |
1393 | * updates x and y to be the localised coordinates. |
1367 | * Using this is more efficient of calling out_of_map |
1394 | * Using this is more efficient of calling out_of_map |
1368 | * and then figuring out what the real map is |
1395 | * and then figuring out what the real map is |
1369 | */ |
1396 | */ |
1370 | maptile * |
1397 | maptile * |
1371 | get_map_from_coord (maptile *m, sint16 *x, sint16 *y) |
1398 | maptile::xy_find (sint16 &x, sint16 &y) |
1372 | { |
1399 | { |
1373 | if (*x < 0) |
1400 | if (x < 0) |
1374 | { |
1401 | { |
1375 | if (!m->tile_path[3]) |
1402 | if (!tile_path[3]) |
1376 | return 0; |
1403 | return 0; |
1377 | |
1404 | |
1378 | if (!m->tile_map[3] || m->tile_map[3]->in_memory != MAP_IN_MEMORY) |
1405 | find_and_link (this, 3); |
1379 | load_and_link_tiled_map (m, 3); |
|
|
1380 | |
|
|
1381 | *x += m->tile_map[3]->width; |
1406 | x += tile_map[3]->width; |
1382 | return (get_map_from_coord (m->tile_map[3], x, y)); |
1407 | return tile_map[3]->xy_find (x, y); |
1383 | } |
1408 | } |
1384 | |
1409 | |
1385 | if (*x >= m->width) |
1410 | if (x >= width) |
1386 | { |
1411 | { |
1387 | if (!m->tile_path[1]) |
1412 | if (!tile_path[1]) |
1388 | return 0; |
1413 | return 0; |
1389 | |
1414 | |
1390 | if (!m->tile_map[1] || m->tile_map[1]->in_memory != MAP_IN_MEMORY) |
1415 | find_and_link (this, 1); |
1391 | load_and_link_tiled_map (m, 1); |
|
|
1392 | |
|
|
1393 | *x -= m->width; |
1416 | x -= width; |
1394 | return (get_map_from_coord (m->tile_map[1], x, y)); |
1417 | return tile_map[1]->xy_find (x, y); |
1395 | } |
1418 | } |
1396 | |
1419 | |
1397 | if (*y < 0) |
1420 | if (y < 0) |
1398 | { |
1421 | { |
1399 | if (!m->tile_path[0]) |
1422 | if (!tile_path[0]) |
1400 | return 0; |
1423 | return 0; |
1401 | |
1424 | |
1402 | if (!m->tile_map[0] || m->tile_map[0]->in_memory != MAP_IN_MEMORY) |
1425 | find_and_link (this, 0); |
1403 | load_and_link_tiled_map (m, 0); |
|
|
1404 | |
|
|
1405 | *y += m->tile_map[0]->height; |
1426 | y += tile_map[0]->height; |
1406 | return (get_map_from_coord (m->tile_map[0], x, y)); |
1427 | return tile_map[0]->xy_find (x, y); |
1407 | } |
1428 | } |
1408 | |
1429 | |
1409 | if (*y >= m->height) |
1430 | if (y >= height) |
1410 | { |
1431 | { |
1411 | if (!m->tile_path[2]) |
1432 | if (!tile_path[2]) |
1412 | return 0; |
1433 | return 0; |
1413 | |
1434 | |
1414 | if (!m->tile_map[2] || m->tile_map[2]->in_memory != MAP_IN_MEMORY) |
1435 | find_and_link (this, 2); |
1415 | load_and_link_tiled_map (m, 2); |
|
|
1416 | |
|
|
1417 | *y -= m->height; |
1436 | y -= height; |
1418 | return (get_map_from_coord (m->tile_map[2], x, y)); |
1437 | return tile_map[2]->xy_find (x, y); |
1419 | } |
1438 | } |
1420 | |
1439 | |
1421 | /* Simple case - coordinates are within this local |
1440 | /* Simple case - coordinates are within this local |
1422 | * map. |
1441 | * map. |
1423 | */ |
1442 | */ |
1424 | return m; |
1443 | return this; |
1425 | } |
1444 | } |
1426 | |
1445 | |
1427 | /** |
1446 | /** |
1428 | * Return whether map2 is adjacent to map1. If so, store the distance from |
1447 | * Return whether map2 is adjacent to map1. If so, store the distance from |
1429 | * map1 to map2 in dx/dy. |
1448 | * map1 to map2 in dx/dy. |
… | |
… | |
1432 | adjacent_map (const maptile *map1, const maptile *map2, int *dx, int *dy) |
1451 | adjacent_map (const maptile *map1, const maptile *map2, int *dx, int *dy) |
1433 | { |
1452 | { |
1434 | if (!map1 || !map2) |
1453 | if (!map1 || !map2) |
1435 | return 0; |
1454 | return 0; |
1436 | |
1455 | |
|
|
1456 | //TODO: this doesn't actually check corretcly when intermediate maps are not loaded |
|
|
1457 | //fix: compare paths instead (this is likely faster, too!) |
1437 | if (map1 == map2) |
1458 | if (map1 == map2) |
1438 | { |
1459 | { |
1439 | *dx = 0; |
1460 | *dx = 0; |
1440 | *dy = 0; |
1461 | *dy = 0; |
1441 | } |
1462 | } |
… | |
… | |
1501 | } |
1522 | } |
1502 | else |
1523 | else |
1503 | return 0; |
1524 | return 0; |
1504 | |
1525 | |
1505 | return 1; |
1526 | return 1; |
|
|
1527 | } |
|
|
1528 | |
|
|
1529 | maptile * |
|
|
1530 | maptile::xy_load (sint16 &x, sint16 &y) |
|
|
1531 | { |
|
|
1532 | maptile *map = xy_find (x, y); |
|
|
1533 | |
|
|
1534 | if (map) |
|
|
1535 | map->load_sync (); |
|
|
1536 | |
|
|
1537 | return map; |
|
|
1538 | } |
|
|
1539 | |
|
|
1540 | maptile * |
|
|
1541 | get_map_from_coord (maptile *m, sint16 *x, sint16 *y) |
|
|
1542 | { |
|
|
1543 | return m->xy_load (*x, *y); |
1506 | } |
1544 | } |
1507 | |
1545 | |
1508 | /* From map.c |
1546 | /* From map.c |
1509 | * This is used by get_player to determine where the other |
1547 | * This is used by get_player to determine where the other |
1510 | * creature is. get_rangevector takes into account map tiling, |
1548 | * creature is. get_rangevector takes into account map tiling, |