| 1 | /* |
| 2 | CrossFire, A Multiplayer game for X-windows |
| 3 | |
| 4 | Copyright (C) 2008 Crossfire Development Team |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | |
| 20 | The authors can be reached via e-mail at [email protected] |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * @file |
| 25 | * The implementation of @ref page_type_41 "teleporter" objects. |
| 26 | */ |
| 27 | |
| 28 | #include <global.h> |
| 29 | #include <ob_methods.h> |
| 30 | #include <ob_types.h> |
| 31 | #include <sounds.h> |
| 32 | #include <sproto.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | static method_ret teleporter_type_process(ob_methods *context, object *op); |
| 36 | static method_ret teleporter_type_trigger(ob_methods *context, object *op, object *cause, int state); |
| 37 | |
| 38 | /** |
| 39 | * Initializer for the @ref page_type_41 "teleporter" object type. |
| 40 | */ |
| 41 | void init_type_teleporter(void) { |
| 42 | register_process(TELEPORTER, teleporter_type_process); |
| 43 | register_trigger(TELEPORTER, teleporter_type_trigger); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Move function for @ref page_type_41 "teleporter" objects. |
| 48 | * |
| 49 | * @param op |
| 50 | * teleporter. |
| 51 | */ |
| 52 | static void move_teleporter(object *op) { |
| 53 | object *tmp, *head; |
| 54 | |
| 55 | /* if this is a multipart teleporter, handle the other parts |
| 56 | * The check for speed isn't strictly needed - basically, if |
| 57 | * there is an old multipart teleporter in which the other parts |
| 58 | * have speed, we don't really want to call it twice for the same |
| 59 | * function - in fact, as written below, part N would get called |
| 60 | * N times without the speed check. |
| 61 | */ |
| 62 | if (op->more && FABS(op->more->speed) < MIN_ACTIVE_SPEED) |
| 63 | move_teleporter(op->more); |
| 64 | |
| 65 | head = HEAD(op); |
| 66 | for (tmp = op->above; tmp != NULL; tmp = tmp->above) { |
| 67 | if (QUERY_FLAG(tmp, FLAG_IS_FLOOR)) continue; /* Ignore floors */ |
| 68 | if (op->other_arch && strcmp(op->other_arch->name,tmp->name) != 0 ) continue; /* Ignore if other_arch doesn't match */ |
| 69 | if (QUERY_FLAG(tmp, FLAG_WIZPASS)) continue; /* Don't teleport the DM */ |
| 70 | /* |
| 71 | * Use the subtype as a bitmask to specify types of objects to *ignore*: |
| 72 | * 1 -- non-player |
| 73 | * 2 -- players |
| 74 | * 4 -- non-monster |
| 75 | * 8 -- monsters |
| 76 | * 16 -- no-pick objects |
| 77 | * ... -- add more as needed |
| 78 | * |
| 79 | * For most purposes, restricting players/non-players is the big win here. |
| 80 | * For objects, use the other_arch field to restrict it to the particular object. |
| 81 | */ |
| 82 | if ( (op->subtype & 1) && tmp->type != PLAYER ) continue; /* If flagged for player only */ |
| 83 | if ( (op->subtype & 2) && tmp->type == PLAYER ) continue; /* If flagged for non-player only */ |
| 84 | if ( (op->subtype & 4) && !QUERY_FLAG(tmp, FLAG_MONSTER) ) continue; /* If flagged for monster only */ |
| 85 | if ( (op->subtype & 8) && QUERY_FLAG(tmp, FLAG_MONSTER) ) continue; /* If flagged for non-monster only */ |
| 86 | if ( (op->subtype & 16) && QUERY_FLAG(tmp, FLAG_NO_PICK) ) continue; /* If flagged skip non-pickupable objects */ |
| 87 | /* ... */ |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | /* If nothing above us to move, nothing to do */ |
| 92 | if (!tmp) return; |
| 93 | |
| 94 | if (EXIT_PATH(head)) { |
| 95 | if (tmp->type == PLAYER) { |
| 96 | /* Lauwenmark: Handle for plugin TRIGGER event */ |
| 97 | if (execute_event(op, EVENT_TRIGGER, tmp, NULL, NULL, SCRIPT_FIX_ALL) != 0) |
| 98 | return; |
| 99 | enter_exit(tmp, head); |
| 100 | } |
| 101 | else |
| 102 | /* Currently only players can transfer maps */ |
| 103 | return; |
| 104 | } else if (EXIT_X(head) || EXIT_Y(head)) { |
| 105 | if (out_of_map(head->map, EXIT_X(head), EXIT_Y(head))) { |
| 106 | LOG(llevError, "Removed illegal teleporter.\n"); |
| 107 | object_remove(head); |
| 108 | object_free_drop_inventory(head); |
| 109 | return; |
| 110 | } |
| 111 | /* Lauwenmark: Handle for plugin TRIGGER event */ |
| 112 | if (execute_event(op, EVENT_TRIGGER, tmp, NULL, NULL, SCRIPT_FIX_ALL) != 0) |
| 113 | return; |
| 114 | transfer_ob(tmp, EXIT_X(head), EXIT_Y(head), 0, head); |
| 115 | } else { |
| 116 | /* Random teleporter */ |
| 117 | /* Lauwenmark: Handle for plugin TRIGGER event */ |
| 118 | if (execute_event(op, EVENT_TRIGGER, tmp, NULL, NULL, SCRIPT_FIX_ALL) != 0) |
| 119 | return; |
| 120 | teleport(head, TELEPORTER, tmp); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Processes a @ref page_type_41 "teleporter". |
| 126 | * @param context The method context |
| 127 | * @param op The teleporter to process |
| 128 | * @retval METHOD_OK |
| 129 | */ |
| 130 | static method_ret teleporter_type_process(ob_methods *context, object *op) { |
| 131 | move_teleporter(op); |
| 132 | return METHOD_OK; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * A @ref page_type_41 "teleporter" is triggered. |
| 137 | * @param context Ignored. |
| 138 | * @param op The object being triggered |
| 139 | * @param cause Ignored. |
| 140 | * @param state Ignored. |
| 141 | * @retval METHOD_OK |
| 142 | */ |
| 143 | static method_ret teleporter_type_trigger(ob_methods *context, object *op, object *cause, int state) { |
| 144 | move_teleporter(op); |
| 145 | return METHOD_OK; |
| 146 | } |