1 | /* |
1 | /* |
2 | CrossFire, A Multiplayer game for X-windows |
2 | CrossFire, A Multiplayer game for X-windows |
3 | |
3 | |
|
|
4 | Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
5 | Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
5 | Copyright (C) 1992 Frank Tore Johansen |
6 | Copyright (C) 1992 Frank Tore Johansen |
6 | |
7 | |
7 | This program is free software; you can redistribute it and/or modify |
8 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
9 | it under the terms of the GNU General Public License as published by |
… | |
… | |
276 | |
277 | |
277 | /* Note sure why the following is the case - either the object has to |
278 | /* Note sure why the following is the case - either the object has to |
278 | * be animated or have a very low speed. Is this an attempted monster |
279 | * be animated or have a very low speed. Is this an attempted monster |
279 | * check? |
280 | * check? |
280 | */ |
281 | */ |
281 | if (!QUERY_FLAG (ob1, FLAG_ANIMATE) && FABS ((ob1)->speed) > MIN_ACTIVE_SPEED) |
282 | if (!QUERY_FLAG (ob1, FLAG_ANIMATE) && ob1->has_active_speed ()) |
282 | return 0; |
283 | return 0; |
283 | |
284 | |
284 | switch (ob1->type) |
285 | switch (ob1->type) |
285 | { |
286 | { |
286 | case SCROLL: |
287 | case SCROLL: |
… | |
… | |
563 | speed = 0; |
564 | speed = 0; |
564 | } |
565 | } |
565 | |
566 | |
566 | this->speed = speed; |
567 | this->speed = speed; |
567 | |
568 | |
568 | if (FABS (speed) > MIN_ACTIVE_SPEED) |
569 | if (has_active_speed ()) |
569 | { |
570 | activate (); |
570 | /* If already on active list, don't do anything */ |
|
|
571 | if (active_next || active_prev || this == active_objects) |
|
|
572 | return; |
|
|
573 | |
|
|
574 | /* process_events() expects us to insert the object at the beginning |
|
|
575 | * of the list. */ |
|
|
576 | active_next = active_objects; |
|
|
577 | |
|
|
578 | if (active_next) |
|
|
579 | active_next->active_prev = this; |
|
|
580 | |
|
|
581 | active_objects = this; |
|
|
582 | } |
|
|
583 | else |
571 | else |
584 | { |
572 | deactivate (); |
585 | /* If not on the active list, nothing needs to be done */ |
|
|
586 | if (!active_next && !active_prev && this != active_objects) |
|
|
587 | return; |
|
|
588 | |
|
|
589 | if (!active_prev) |
|
|
590 | { |
|
|
591 | active_objects = active_next; |
|
|
592 | |
|
|
593 | if (active_next) |
|
|
594 | active_next->active_prev = 0; |
|
|
595 | } |
|
|
596 | else |
|
|
597 | { |
|
|
598 | active_prev->active_next = active_next; |
|
|
599 | |
|
|
600 | if (active_next) |
|
|
601 | active_next->active_prev = active_prev; |
|
|
602 | } |
|
|
603 | |
|
|
604 | active_next = 0; |
|
|
605 | active_prev = 0; |
|
|
606 | } |
|
|
607 | } |
573 | } |
608 | |
574 | |
609 | /* |
575 | /* |
610 | * update_object() updates the the map. |
576 | * update_object() updates the the map. |
611 | * It takes into account invisible objects (and represent squares covered |
577 | * It takes into account invisible objects (and represent squares covered |
… | |
… | |
659 | return; |
625 | return; |
660 | } |
626 | } |
661 | |
627 | |
662 | mapspace &m = op->ms (); |
628 | mapspace &m = op->ms (); |
663 | |
629 | |
664 | if (m.flags_ & P_NEED_UPDATE) |
630 | if (!(m.flags_ & P_UPTODATE)) |
665 | /* nop */; |
631 | /* nop */; |
666 | else if (action == UP_OBJ_INSERT) |
632 | else if (action == UP_OBJ_INSERT) |
667 | { |
633 | { |
668 | // this is likely overkill, TODO: revisit (schmorp) |
634 | // this is likely overkill, TODO: revisit (schmorp) |
669 | if ((QUERY_FLAG (op, FLAG_BLOCKSVIEW) && !(m.flags_ & P_BLOCKSVIEW)) |
635 | if ((QUERY_FLAG (op, FLAG_BLOCKSVIEW) && !(m.flags_ & P_BLOCKSVIEW)) |
… | |
… | |
678 | /* This isn't perfect, but I don't expect a lot of objects to |
644 | /* This isn't perfect, but I don't expect a lot of objects to |
679 | * to have move_allow right now. |
645 | * to have move_allow right now. |
680 | */ |
646 | */ |
681 | || ((m.move_block | op->move_block) & ~op->move_allow) != m.move_block |
647 | || ((m.move_block | op->move_block) & ~op->move_allow) != m.move_block |
682 | || 1) // the above is not strong enough a test to skip updating. los maybe? TODO (Schmorp) |
648 | || 1) // the above is not strong enough a test to skip updating. los maybe? TODO (Schmorp) |
683 | m.flags_ = P_NEED_UPDATE; |
649 | m.flags_ = 0; |
684 | } |
650 | } |
685 | /* if the object is being removed, we can't make intelligent |
651 | /* if the object is being removed, we can't make intelligent |
686 | * decisions, because remove_ob can't really pass the object |
652 | * decisions, because remove_ob can't really pass the object |
687 | * that is being removed. |
653 | * that is being removed. |
688 | */ |
654 | */ |
689 | else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE) |
655 | else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE) |
690 | m.flags_ = P_NEED_UPDATE; |
656 | m.flags_ = 0; |
691 | else if (action == UP_OBJ_FACE) |
657 | else if (action == UP_OBJ_FACE) |
692 | /* Nothing to do for that case */ ; |
658 | /* Nothing to do for that case */ ; |
693 | else |
659 | else |
694 | LOG (llevError, "update_object called with invalid action: %d\n", action); |
660 | LOG (llevError, "update_object called with invalid action: %d\n", action); |
695 | |
661 | |
696 | if (op->more) |
662 | if (op->more) |
697 | update_object (op->more, action); |
663 | update_object (op->more, action); |
698 | } |
664 | } |
699 | |
665 | |
700 | object::vector object::objects; // not yet used |
|
|
701 | object *object::first; |
666 | object *object::first; |
702 | |
667 | |
703 | object::object () |
668 | object::object () |
704 | { |
669 | { |
705 | SET_FLAG (this, FLAG_REMOVED); |
670 | SET_FLAG (this, FLAG_REMOVED); |
… | |
… | |
738 | |
703 | |
739 | prev = 0; |
704 | prev = 0; |
740 | next = 0; |
705 | next = 0; |
741 | } |
706 | } |
742 | |
707 | |
|
|
708 | bool |
|
|
709 | object::active () const |
|
|
710 | { |
|
|
711 | return active_next || active_prev || this == active_objects; |
|
|
712 | } |
|
|
713 | |
743 | void |
714 | void |
|
|
715 | object::activate () |
|
|
716 | { |
|
|
717 | /* If already on active list, don't do anything */ |
|
|
718 | if (active ()) |
|
|
719 | return; |
|
|
720 | |
|
|
721 | if (has_active_speed ()) |
|
|
722 | { |
|
|
723 | /* process_events() expects us to insert the object at the beginning |
|
|
724 | * of the list. */ |
|
|
725 | active_next = active_objects; |
|
|
726 | |
|
|
727 | if (active_next) |
|
|
728 | active_next->active_prev = this; |
|
|
729 | |
|
|
730 | active_objects = this; |
|
|
731 | } |
|
|
732 | } |
|
|
733 | |
|
|
734 | void |
744 | object::activate (bool recursive) |
735 | object::activate_recursive () |
745 | { |
736 | { |
746 | // uh, hack |
737 | activate (); |
747 | set_speed (speed); |
|
|
748 | |
738 | |
749 | if (recursive) |
|
|
750 | for (object *op = inv; op; op = op->above) |
739 | for (object *op = inv; op; op = op->below) |
751 | op->activate (1); |
740 | op->activate_recursive (); |
752 | } |
741 | } |
753 | |
742 | |
754 | /* This function removes object 'op' from the list of active |
743 | /* This function removes object 'op' from the list of active |
755 | * objects. |
744 | * objects. |
756 | * This should only be used for style maps or other such |
745 | * This should only be used for style maps or other such |
… | |
… | |
758 | * in play chewing up cpu time getting processed. |
747 | * in play chewing up cpu time getting processed. |
759 | * The reverse of this is to call update_ob_speed, which |
748 | * The reverse of this is to call update_ob_speed, which |
760 | * will do the right thing based on the speed of the object. |
749 | * will do the right thing based on the speed of the object. |
761 | */ |
750 | */ |
762 | void |
751 | void |
763 | object::deactivate (bool recursive) |
752 | object::deactivate () |
764 | { |
753 | { |
765 | /* If not on the active list, nothing needs to be done */ |
754 | /* If not on the active list, nothing needs to be done */ |
766 | if (!active_next && !active_prev && this != active_objects) |
755 | if (!active ()) |
767 | return; |
756 | return; |
768 | |
757 | |
769 | if (active_prev == 0) |
758 | if (active_prev == 0) |
770 | { |
759 | { |
771 | active_objects = active_next; |
760 | active_objects = active_next; |
… | |
… | |
779 | active_next->active_prev = active_prev; |
768 | active_next->active_prev = active_prev; |
780 | } |
769 | } |
781 | |
770 | |
782 | active_next = 0; |
771 | active_next = 0; |
783 | active_prev = 0; |
772 | active_prev = 0; |
|
|
773 | } |
784 | |
774 | |
785 | if (recursive) |
775 | void |
|
|
776 | object::deactivate_recursive () |
|
|
777 | { |
786 | for (object *op = inv; op; op = op->above) |
778 | for (object *op = inv; op; op = op->below) |
|
|
779 | op->deactivate_recursive (); |
|
|
780 | |
787 | op->deactivate (1); |
781 | deactivate (); |
|
|
782 | } |
|
|
783 | |
|
|
784 | void |
|
|
785 | object::set_flag_inv (int flag, int value) |
|
|
786 | { |
|
|
787 | for (object *op = inv; op; op = op->below) |
|
|
788 | { |
|
|
789 | op->flag [flag] = value; |
|
|
790 | op->set_flag_inv (flag, value); |
|
|
791 | } |
788 | } |
792 | } |
789 | |
793 | |
790 | /* |
794 | /* |
791 | * Remove and free all objects in the inventory of the given object. |
795 | * Remove and free all objects in the inventory of the given object. |
792 | * object.c ? |
796 | * object.c ? |
… | |
… | |
877 | freed_map->name = "/internal/freed_objects_map"; |
881 | freed_map->name = "/internal/freed_objects_map"; |
878 | freed_map->width = 3; |
882 | freed_map->width = 3; |
879 | freed_map->height = 3; |
883 | freed_map->height = 3; |
880 | |
884 | |
881 | freed_map->alloc (); |
885 | freed_map->alloc (); |
|
|
886 | freed_map->in_memory = MAP_IN_MEMORY; |
882 | } |
887 | } |
883 | |
888 | |
884 | map = freed_map; |
889 | map = freed_map; |
885 | x = 1; |
890 | x = 1; |
886 | y = 1; |
891 | y = 1; |
… | |
… | |
993 | else if (map) |
998 | else if (map) |
994 | { |
999 | { |
995 | if (type == PLAYER) |
1000 | if (type == PLAYER) |
996 | { |
1001 | { |
997 | --map->players; |
1002 | --map->players; |
998 | map->last_access = runtime; |
1003 | map->touch (); |
999 | } |
1004 | } |
1000 | |
1005 | |
|
|
1006 | map->dirty = true; |
1001 | |
1007 | |
1002 | /* link the object above us */ |
1008 | /* link the object above us */ |
1003 | if (above) |
1009 | if (above) |
1004 | above->below = below; |
1010 | above->below = below; |
1005 | else |
1011 | else |
… | |
… | |
1078 | } |
1084 | } |
1079 | |
1085 | |
1080 | /* last == NULL if there are no objects on this space */ |
1086 | /* last == NULL if there are no objects on this space */ |
1081 | //TODO: this makes little sense, why only update the topmost object? |
1087 | //TODO: this makes little sense, why only update the topmost object? |
1082 | if (!last) |
1088 | if (!last) |
1083 | map->at (x, y).flags_ = P_NEED_UPDATE; |
1089 | map->at (x, y).flags_ = 0; |
1084 | else |
1090 | else |
1085 | update_object (last, UP_OBJ_REMOVE); |
1091 | update_object (last, UP_OBJ_REMOVE); |
1086 | |
1092 | |
1087 | if (flag [FLAG_BLOCKSVIEW] || glow_radius) |
1093 | if (flag [FLAG_BLOCKSVIEW] || glow_radius) |
1088 | update_all_los (map, x, y); |
1094 | update_all_los (map, x, y); |
… | |
… | |
1209 | { |
1215 | { |
1210 | /* The part may be on a different map. */ |
1216 | /* The part may be on a different map. */ |
1211 | |
1217 | |
1212 | object *more = op->more; |
1218 | object *more = op->more; |
1213 | |
1219 | |
1214 | /* We really need the caller to normalize coordinates - if |
1220 | /* We really need the caller to normalise coordinates - if |
1215 | * we set the map, that doesn't work if the location is within |
1221 | * we set the map, that doesn't work if the location is within |
1216 | * a map and this is straddling an edge. So only if coordinate |
1222 | * a map and this is straddling an edge. So only if coordinate |
1217 | * is clear wrong do we normalize it. |
1223 | * is clear wrong do we normalise it. |
1218 | */ |
1224 | */ |
1219 | if (OUT_OF_REAL_MAP (more->map, more->x, more->y)) |
1225 | if (OUT_OF_REAL_MAP (more->map, more->x, more->y)) |
1220 | more->map = get_map_from_coord (m, &more->x, &more->y); |
1226 | more->map = get_map_from_coord (m, &more->x, &more->y); |
1221 | else if (!more->map) |
1227 | else if (!more->map) |
1222 | { |
1228 | { |
… | |
… | |
1382 | |
1388 | |
1383 | if (op->type == PLAYER) |
1389 | if (op->type == PLAYER) |
1384 | { |
1390 | { |
1385 | op->contr->do_los = 1; |
1391 | op->contr->do_los = 1; |
1386 | ++op->map->players; |
1392 | ++op->map->players; |
1387 | op->map->last_access = runtime; |
1393 | op->map->touch (); |
1388 | } |
1394 | } |
|
|
1395 | |
|
|
1396 | op->map->dirty = true; |
1389 | |
1397 | |
1390 | /* If we have a floor, we know the player, if any, will be above |
1398 | /* If we have a floor, we know the player, if any, will be above |
1391 | * it, so save a few ticks and start from there. |
1399 | * it, so save a few ticks and start from there. |
1392 | */ |
1400 | */ |
1393 | if (!(flag & INS_MAP_LOAD)) |
1401 | if (!(flag & INS_MAP_LOAD)) |
… | |
… | |
1399 | * visible to others on this map. But update_all_los is really |
1407 | * visible to others on this map. But update_all_los is really |
1400 | * an inefficient way to do this, as it means los for all players |
1408 | * an inefficient way to do this, as it means los for all players |
1401 | * on the map will get recalculated. The players could very well |
1409 | * on the map will get recalculated. The players could very well |
1402 | * be far away from this change and not affected in any way - |
1410 | * be far away from this change and not affected in any way - |
1403 | * this should get redone to only look for players within range, |
1411 | * this should get redone to only look for players within range, |
1404 | * or just updating the P_NEED_UPDATE for spaces within this area |
1412 | * or just updating the P_UPTODATE for spaces within this area |
1405 | * of effect may be sufficient. |
1413 | * of effect may be sufficient. |
1406 | */ |
1414 | */ |
1407 | if (op->map->darkness && (op->glow_radius != 0)) |
1415 | if (op->map->darkness && (op->glow_radius != 0)) |
1408 | update_all_los (op->map, op->x, op->y); |
1416 | update_all_los (op->map, op->x, op->y); |
1409 | |
1417 | |
… | |
… | |
1447 | { |
1455 | { |
1448 | object *tmp, *tmp1; |
1456 | object *tmp, *tmp1; |
1449 | |
1457 | |
1450 | /* first search for itself and remove any old instances */ |
1458 | /* first search for itself and remove any old instances */ |
1451 | |
1459 | |
1452 | for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp != NULL; tmp = tmp->above) |
1460 | for (tmp = op->ms ().bot; tmp; tmp = tmp->above) |
1453 | if (!strcmp (tmp->arch->name, arch_string)) /* same archetype */ |
1461 | if (!strcmp (tmp->arch->name, arch_string)) /* same archetype */ |
1454 | tmp->destroy (); |
1462 | tmp->destroy (); |
1455 | |
1463 | |
1456 | tmp1 = arch_to_object (archetype::find (arch_string)); |
1464 | tmp1 = arch_to_object (archetype::find (arch_string)); |
1457 | |
1465 | |
… | |
… | |
1780 | |
1788 | |
1781 | /* The objects have to be checked from top to bottom. |
1789 | /* The objects have to be checked from top to bottom. |
1782 | * Hence, we first go to the top: |
1790 | * Hence, we first go to the top: |
1783 | */ |
1791 | */ |
1784 | |
1792 | |
1785 | for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp && tmp->above; tmp = tmp->above) |
1793 | for (tmp = op->ms ().bot; tmp && tmp->above; tmp = tmp->above) |
1786 | { |
1794 | { |
1787 | /* Trim the search when we find the first other spell effect |
1795 | /* Trim the search when we find the first other spell effect |
1788 | * this helps performance so that if a space has 50 spell objects, |
1796 | * this helps performance so that if a space has 50 spell objects, |
1789 | * we don't need to check all of them. |
1797 | * we don't need to check all of them. |
1790 | */ |
1798 | */ |
… | |
… | |
1848 | * The first matching object is returned, or NULL if none. |
1856 | * The first matching object is returned, or NULL if none. |
1849 | */ |
1857 | */ |
1850 | object * |
1858 | object * |
1851 | present_arch (const archetype *at, maptile *m, int x, int y) |
1859 | present_arch (const archetype *at, maptile *m, int x, int y) |
1852 | { |
1860 | { |
1853 | if (m == NULL || out_of_map (m, x, y)) |
1861 | if (!m || out_of_map (m, x, y)) |
1854 | { |
1862 | { |
1855 | LOG (llevError, "Present_arch called outside map.\n"); |
1863 | LOG (llevError, "Present_arch called outside map.\n"); |
1856 | return NULL; |
1864 | return NULL; |
1857 | } |
1865 | } |
1858 | |
1866 | |
1859 | for (object *tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) |
1867 | for (object *tmp = m->at (x, y).bot; tmp; tmp = tmp->above) |
1860 | if (tmp->arch == at) |
1868 | if (tmp->arch == at) |
1861 | return tmp; |
1869 | return tmp; |
1862 | |
1870 | |
1863 | return NULL; |
1871 | return NULL; |
1864 | } |
1872 | } |
… | |
… | |
1875 | { |
1883 | { |
1876 | LOG (llevError, "Present called outside map.\n"); |
1884 | LOG (llevError, "Present called outside map.\n"); |
1877 | return NULL; |
1885 | return NULL; |
1878 | } |
1886 | } |
1879 | |
1887 | |
1880 | for (object *tmp = GET_MAP_OB (m, x, y); tmp != NULL; tmp = tmp->above) |
1888 | for (object *tmp = m->at (x, y).bot; tmp; tmp = tmp->above) |
1881 | if (tmp->type == type) |
1889 | if (tmp->type == type) |
1882 | return tmp; |
1890 | return tmp; |
1883 | |
1891 | |
1884 | return NULL; |
1892 | return NULL; |
1885 | } |
1893 | } |
… | |
… | |
2610 | } |
2618 | } |
2611 | else |
2619 | else |
2612 | item = item->env; |
2620 | item = item->env; |
2613 | } |
2621 | } |
2614 | |
2622 | |
|
|
2623 | |
|
|
2624 | const char * |
|
|
2625 | object::flag_desc (char *desc, int len) const |
|
|
2626 | { |
|
|
2627 | char *p = desc; |
|
|
2628 | bool first = true; |
|
|
2629 | |
|
|
2630 | *p = 0; |
|
|
2631 | |
|
|
2632 | for (int i = 0; i < NUM_FLAGS; i++) |
|
|
2633 | { |
|
|
2634 | if (len <= 10) // magic constant! |
|
|
2635 | { |
|
|
2636 | snprintf (p, len, ",..."); |
|
|
2637 | break; |
|
|
2638 | } |
|
|
2639 | |
|
|
2640 | if (flag [i]) |
|
|
2641 | { |
|
|
2642 | int cnt = snprintf (p, len, "%s%d", first ? "" : ",", i); |
|
|
2643 | len -= cnt; |
|
|
2644 | p += cnt; |
|
|
2645 | first = false; |
|
|
2646 | } |
|
|
2647 | } |
|
|
2648 | |
|
|
2649 | return desc; |
|
|
2650 | } |
|
|
2651 | |
2615 | // return a suitable string describing an objetc in enough detail to find it |
2652 | // return a suitable string describing an object in enough detail to find it |
2616 | const char * |
2653 | const char * |
2617 | object::debug_desc (char *info) const |
2654 | object::debug_desc (char *info) const |
2618 | { |
2655 | { |
|
|
2656 | char flagdesc[512]; |
2619 | char info2[256 * 3]; |
2657 | char info2[256 * 4]; |
2620 | char *p = info; |
2658 | char *p = info; |
2621 | |
2659 | |
2622 | p += snprintf (p, 256, "%d=\"%s%s%s\"", |
2660 | p += snprintf (p, 512, "{cnt:%d,uuid:<1,%" PRIx64 ">,name:\"%s%s%s\",flags:[%s],type:%d}", |
2623 | count, |
2661 | count, uuid.seq, |
2624 | &name, |
2662 | &name, |
2625 | title ? " " : "", |
2663 | title ? "\",title:" : "", |
2626 | title ? (const char *)title : ""); |
2664 | title ? (const char *)title : "", |
|
|
2665 | flag_desc (flagdesc, 512), type); |
2627 | |
2666 | |
2628 | if (env) |
2667 | if (env) |
2629 | p += snprintf (p, 256, "(in %s)", env->debug_desc (info2)); |
2668 | p += snprintf (p, 256, "(in %s)", env->debug_desc (info2)); |
2630 | |
2669 | |
2631 | if (map) |
2670 | if (map) |
… | |
… | |
2635 | } |
2674 | } |
2636 | |
2675 | |
2637 | const char * |
2676 | const char * |
2638 | object::debug_desc () const |
2677 | object::debug_desc () const |
2639 | { |
2678 | { |
2640 | static char info[256 * 3]; |
2679 | static char info[256 * 4]; |
2641 | return debug_desc (info); |
2680 | return debug_desc (info); |
2642 | } |
2681 | } |
2643 | |
2682 | |