ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/map.C
(Generate patch)

Comparing deliantra/server/common/map.C (file contents):
Revision 1.49 by root, Mon Dec 25 14:54:44 2006 UTC vs.
Revision 1.53 by root, Wed Dec 27 09:28:01 2006 UTC

35 * return NULL if no match is found. 35 * return NULL if no match is found.
36 */ 36 */
37maptile * 37maptile *
38has_been_loaded (const char *name) 38has_been_loaded (const char *name)
39{ 39{
40 maptile *map;
41
42 if (!name || !*name) 40 if (!name || !*name)
43 return 0; 41 return 0;
44 42
45 for (map = first_map; map; map = map->next) 43 for_all_maps (map)
46 if (!strcmp (name, map->path)) 44 if (!strcmp (name, map->path))
47 break;
48
49 return (map); 45 return map;
46
47 return 0;
50} 48}
51 49
52/* 50/*
53 * This makes a path absolute outside the world of Crossfire. 51 * This makes a path absolute outside the world of Crossfire.
54 * In other words, it prepends LIBDIR/MAPDIR/ to the given path 52 * In other words, it prepends LIBDIR/MAPDIR/ to the given path
221 */ 219 */
222 220
223void 221void
224dump_all_maps (void) 222dump_all_maps (void)
225{ 223{
226 maptile *m; 224 for_all_maps (m)
227
228 for (m = first_map; m; m = m->next)
229 dump_map (m); 225 dump_map (m);
230} 226}
231 227
232/* This rolls up wall, blocks_magic, blocks_view, etc, all into 228/* This rolls up wall, blocks_magic, blocks_view, etc, all into
233 * one function that just returns a P_.. value (see map.h) 229 * one function that just returns a P_.. value (see map.h)
476 * for objects whose arch says they are multipart yet according to the 472 * for objects whose arch says they are multipart yet according to the
477 * info we have, they only have the head (as would be expected when 473 * info we have, they only have the head (as would be expected when
478 * they are saved). We do have to look for the old maps that did save 474 * they are saved). We do have to look for the old maps that did save
479 * the more sections and not re-add sections for them. 475 * the more sections and not re-add sections for them.
480 */ 476 */
481
482static void 477static void
483link_multipart_objects (maptile *m) 478link_multipart_objects (maptile *m)
484{ 479{
485 int x, y; 480 int x, y;
486 object *tmp, *op, *last, *above; 481 object *tmp, *op, *last, *above;
601 * Modified by MSW 2001-07-01 to do in a single pass - reduces code, 596 * Modified by MSW 2001-07-01 to do in a single pass - reduces code,
602 * and we only save the head of multi part objects - this is needed 597 * and we only save the head of multi part objects - this is needed
603 * in order to do map tiling properly. 598 * in order to do map tiling properly.
604 */ 599 */
605void 600void
606save_objects (maptile *m, object_freezer & fp, object_freezer & fp2, int flag) 601save_objects (maptile *m, object_freezer &fp, object_freezer &fp2, int flag)
607{ 602{
608 int i, j = 0, unique = 0; 603 int i, j = 0, unique = 0;
609 object *op; 604 object *op;
610 605
611 /* first pass - save one-part objects */ 606 /* first pass - save one-part objects */
612 for (i = 0; i < m->width; i++) 607 for (i = 0; i < m->width; i++)
613 for (j = 0; j < m->height; j++) 608 for (j = 0; j < m->height; j++)
614 { 609 {
615 unique = 0; 610 unique = 0;
611
616 for (op = m->at (i, j).bot; op; op = op->above) 612 for (op = m->at (i, j).bot; op; op = op->above)
617 { 613 {
618 if (op->flag [FLAG_IS_FLOOR] && op->flag [FLAG_UNIQUE]) 614 if (op->flag [FLAG_UNIQUE] && op->flag [FLAG_IS_FLOOR])
619 unique = 1; 615 unique = 1;
620 616
621 if (op->type == PLAYER) 617 if (op->type == PLAYER)
622 continue; 618 continue;
623 619
624 if (op->head || op->owner) 620 if (op->head || op->owner)
625 continue; 621 continue;
626 622
627 if (unique || op->flag [FLAG_UNIQUE]) 623 if (unique || op->flag [FLAG_UNIQUE])
628 save_object (fp2, op, 3); 624 save_object (fp2, op, 1);
629 else if (flag == 0 || (flag == 2 && (!op->flag [FLAG_OBJ_ORIGINAL] && !op->flag [FLAG_UNPAID]))) 625 else if (flag == 0 || (flag == 2 && (!op->flag [FLAG_OBJ_ORIGINAL] && !op->flag [FLAG_UNPAID])))
630 save_object (fp, op, 3); 626 save_object (fp, op, 1);
631 } 627 }
632 } 628 }
633} 629}
634 630
635maptile::maptile () 631maptile::maptile ()
636{ 632{
637 in_memory = MAP_SWAPPED; 633 in_memory = MAP_SWAPPED;
638 /* The maps used to pick up default x and y values from the 634 /* The maps used to pick up default x and y values from the
639 * map archetype. Mimic that behaviour. 635 * map archetype. Mimic that behaviour.
640 */ 636 */
641 this->width = 16; 637 width = 16;
642 this->height = 16; 638 height = 16;
643 this->reset_timeout = 0; 639 reset_timeout = 0;
644 this->timeout = 300; 640 timeout = 300;
645 this->enter_x = 0; 641 enter_x = 0;
646 this->enter_y = 0; 642 enter_y = 0;
647 /*set part to -1 indicating conversion to weather map not yet done */ 643 /*set part to -1 indicating conversion to weather map not yet done */
648 this->worldpartx = -1; 644 worldpartx = -1;
649 this->worldparty = -1; 645 worldparty = -1;
650} 646}
651 647
652/* 648/*
653 * Allocates, initialises, and returns a pointer to a maptile. 649 * Allocates, initialises, and returns a pointer to a maptile.
654 * Modified to no longer take a path option which was not being 650 * Modified to no longer take a path option which was not being
655 * used anyways. MSW 2001-07-01 651 * used anyways. MSW 2001-07-01
656 */ 652 */
657maptile * 653maptile *
658get_linked_map (void) 654get_linked_map (void)
659{ 655{
660 maptile *mp, *map = new maptile; 656 maptile *map = new maptile;
661 657
662 for (mp = first_map; mp && mp->next; mp = mp->next); 658 map->next = first_map;
663
664 if (mp == NULL)
665 first_map = map; 659 first_map = map;
666 else
667 mp->next = map;
668 660
669 return map; 661 return map;
670} 662}
671 663
672/* 664/*
684 * that is their poor assumption. 676 * that is their poor assumption.
685 */ 677 */
686 if (spaces) 678 if (spaces)
687 { 679 {
688 LOG (llevError, "allocate_map called with already allocated map (%s)\n", path); 680 LOG (llevError, "allocate_map called with already allocated map (%s)\n", path);
689 free (spaces); 681 sfree (spaces, size ());
690 } 682 }
691 683
692 spaces = (mapspace *) 684 spaces = salloc0<mapspace> (size ());
693 calloc (1, width * height * sizeof (mapspace));
694
695 if (!spaces)
696 fatal (OUT_OF_MEMORY);
697} 685}
698 686
699/* Create and returns a map of the specific size. Used 687/* Create and returns a map of the specific size. Used
700 * in random map code and the editor. 688 * in random map code and the editor.
701 */ 689 */
1260 } 1248 }
1261 } 1249 }
1262 } 1250 }
1263} 1251}
1264 1252
1265
1266/* 1253/*
1267 * Loads unique objects from file(s) into the map which is in memory 1254 * Loads unique objects from file(s) into the map which is in memory
1268 * m is the map to load unique items into. 1255 * m is the map to load unique items into.
1269 */ 1256 */
1270static void 1257static void
1289 return; 1276 return;
1290 1277
1291 m->in_memory = MAP_LOADING; 1278 m->in_memory = MAP_LOADING;
1292 if (m->tmpname == NULL) /* if we have loaded unique items from */ 1279 if (m->tmpname == NULL) /* if we have loaded unique items from */
1293 delete_unique_items (m); /* original map before, don't duplicate them */ 1280 delete_unique_items (m); /* original map before, don't duplicate them */
1281
1294 load_objects (m, thawer, 0); 1282 load_objects (m, thawer, 0);
1295 1283
1296 m->in_memory = MAP_IN_MEMORY; 1284 m->in_memory = MAP_IN_MEMORY;
1297} 1285}
1298
1299 1286
1300/* 1287/*
1301 * Saves a map to file. If flag is set, it is saved into the same 1288 * Saves a map to file. If flag is set, it is saved into the same
1302 * file it was (originally) loaded from. Otherwise a temporary 1289 * file it was (originally) loaded from. Otherwise a temporary
1303 * filename will be genarated, and the file will be stored there. 1290 * filename will be genarated, and the file will be stored there.
1304 * The temporary filename will be stored in the maptileure. 1291 * The temporary filename will be stored in the maptileure.
1305 * If the map is unique, we also save to the filename in the map 1292 * If the map is unique, we also save to the filename in the map
1306 * (this should have been updated when first loaded) 1293 * (this should have been updated when first loaded)
1307 */ 1294 */
1308
1309int 1295int
1310new_save_map (maptile *m, int flag) 1296new_save_map (maptile *m, int flag)
1311{ 1297{
1312 char filename[MAX_BUF], buf[MAX_BUF], shop[MAX_BUF]; 1298 char filename[MAX_BUF], buf[MAX_BUF], shop[MAX_BUF];
1313 int i; 1299 int i;
1477 * don't free tmpname - our caller is left to do that 1463 * don't free tmpname - our caller is left to do that
1478 */ 1464 */
1479void 1465void
1480free_map (maptile *m, int flag) 1466free_map (maptile *m, int flag)
1481{ 1467{
1482 if (m->in_memory != MAP_IN_MEMORY) 1468 if (!m->in_memory) //TODO: makes no sense to me?
1483 return; 1469 return;
1484 1470
1485 m->in_memory = MAP_SAVING; 1471 m->in_memory = MAP_SAVING;
1486 1472
1487 // TODO: use new/delete 1473 // TODO: use new/delete
1488#define FREE_AND_CLEAR(p) { free (p); p = NULL; }
1489 1474
1490 if (flag && m->spaces) 1475 if (flag && m->spaces)
1491 free_all_objects (m); 1476 free_all_objects (m);
1492 if (m->name)
1493 FREE_AND_CLEAR (m->name);
1494 if (m->spaces)
1495 FREE_AND_CLEAR (m->spaces);
1496 if (m->msg)
1497 FREE_AND_CLEAR (m->msg);
1498 if (m->maplore)
1499 FREE_AND_CLEAR (m->maplore);
1500 1477
1501 delete [] m->shopitems; 1478 sfree (m->spaces, m->size ()), m->spaces = 0;
1502 m->shopitems = 0;
1503 1479
1504 if (m->shoprace) 1480 free (m->name), m->name = 0;
1505 FREE_AND_CLEAR (m->shoprace); 1481 free (m->msg), m->msg = 0;
1482 free (m->maplore), m->maplore = 0;
1483 free (m->shoprace), m->shoprace = 0;
1484 delete [] m->shopitems, m->shopitems = 0;
1506 1485
1507 if (m->buttons) 1486 if (m->buttons)
1508 free_objectlinkpt (m->buttons); 1487 free_objectlinkpt (m->buttons), m->buttons = 0;
1509
1510 m->buttons = NULL;
1511 1488
1512 for (int i = 0; i < 4; i++) 1489 for (int i = 0; i < 4; i++)
1513 { 1490 free (m->tile_path[i]), m->tile_path[i] = 0;
1514 if (m->tile_path[i])
1515 FREE_AND_CLEAR (m->tile_path[i]);
1516
1517 m->tile_map[i] = 0;
1518 }
1519 1491
1520 m->in_memory = MAP_SWAPPED; 1492 m->in_memory = MAP_SWAPPED;
1521
1522#undef FREE_AND_CLEAR
1523} 1493}
1524 1494
1525maptile::~maptile () 1495maptile::~maptile ()
1526{ 1496{
1527 free_map (this, 1); 1497 assert (destroyed ());
1528 free (tmpname);
1529} 1498}
1530 1499
1531void 1500void
1532maptile::do_destroy () 1501maptile::do_destroy ()
1533{ 1502{
1534 attachable::do_destroy (); 1503 attachable::do_destroy ();
1535 1504
1536 free_all_objects (this); 1505 free_map (this, 1);
1506 free (tmpname), tmpname = 0;
1537 1507
1538 /* We need to look through all the maps and see if any maps 1508 /* We need to look through all the maps and see if any maps
1539 * are pointing at this one for tiling information. Since 1509 * are pointing at this one for tiling information. Since
1540 * tiling can be asymetric, we just can not look to see which 1510 * tiling can be asymetric, we just can not look to see which
1541 * maps this map tiles with and clears those. 1511 * maps this map tiles with and clears those.
1542 */ 1512 */
1543 //TODO: non-euclidean-tiling MUST GO 1513 //TODO: non-euclidean-tiling MUST GO
1544 for (maptile *m = first_map; m; m = m->next) 1514 for_all_maps (m)
1545 for (int i = 0; i < 4; i++) 1515 for (int i = 0; i < 4; i++)
1546 if (m->tile_map[i] == this) 1516 if (m->tile_map[i] == this)
1547 m->tile_map[i] = 0; 1517 m->tile_map[i] = 0;
1548 1518
1549 if (first_map == this) 1519 if (first_map == this)
1550 first_map = next; 1520 first_map = next;
1551 else 1521 else
1552 for (maptile *m = first_map; m; m = m->next) 1522 for_all_maps (m)
1553 if (m->next = this) 1523 if (m->next = this)
1554 { 1524 {
1555 m->next = next; 1525 m->next = next;
1556 break; 1526 break;
1557 } 1527 }
1759 if (first_map->in_memory == MAP_SAVING) 1729 if (first_map->in_memory == MAP_SAVING)
1760 first_map->in_memory = MAP_IN_MEMORY; 1730 first_map->in_memory = MAP_IN_MEMORY;
1761 delete_map (first_map); 1731 delete_map (first_map);
1762 real_maps++; 1732 real_maps++;
1763 } 1733 }
1734
1764 LOG (llevDebug, "free_all_maps: Freed %d maps\n", real_maps); 1735 LOG (llevDebug, "free_all_maps: Freed %d maps\n", real_maps);
1765} 1736}
1766 1737
1767/* change_map_light() - used to change map light level (darkness) 1738/* change_map_light() - used to change map light level (darkness)
1768 * up or down. Returns true if successful. It should now be 1739 * up or down. Returns true if successful. It should now be
2363{ 2334{
2364 int dx, dy; 2335 int dx, dy;
2365 2336
2366 return adjacent_map (op1->map, op2->map, &dx, &dy); 2337 return adjacent_map (op1->map, op2->map, &dx, &dy);
2367} 2338}
2339
2340object *
2341maptile::insert (object *op, int x, int y, object *originator, int flags)
2342{
2343 if (!op->flag [FLAG_REMOVED])
2344 op->remove ();
2345
2346 return insert_ob_in_map_at (op, this, originator, flags, x, y);
2347}
2348

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines