| 1 | /* |
| 2 | CrossFire, A Multiplayer game for X-windows |
| 3 | |
| 4 | Copyright (C) 2007 Mark Wedel & Crossfire Development Team |
| 5 | Copyright (C) 1992 Frank Tore Johansen |
| 6 | |
| 7 | 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 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | |
| 21 | The authors can be reached via e-mail at [email protected] |
| 22 | */ |
| 23 | |
| 24 | /** @file |
| 25 | * The implementation of the Treasure class of objects. |
| 26 | */ |
| 27 | #include <global.h> |
| 28 | #include <ob_methods.h> |
| 29 | #include <ob_types.h> |
| 30 | #include <sounds.h> |
| 31 | #include <sproto.h> |
| 32 | |
| 33 | static method_ret treasure_type_apply(ob_methods *context, object *op, object *applier, int aflags); |
| 34 | |
| 35 | /** |
| 36 | * Initializer for the TREASURE object type. |
| 37 | */ |
| 38 | void init_type_treasure(void) { |
| 39 | register_apply(TREASURE, treasure_type_apply); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Attempts to apply treasure. |
| 44 | * @param context The method context |
| 45 | * @param op The treasure to apply |
| 46 | * @param applier The object attempting to apply the treasure. Ignored unless |
| 47 | * a player |
| 48 | * @param aflags Special flags (always apply/unapply) |
| 49 | * @return The return value is always METHOD_OK |
| 50 | */ |
| 51 | static method_ret treasure_type_apply(ob_methods *context, object *op, object *applier, int aflags) { |
| 52 | object *treas; |
| 53 | tag_t op_tag = op->count, applier_tag = applier->count; |
| 54 | char name[MAX_BUF], container_name[MAX_BUF]; |
| 55 | |
| 56 | if (applier->type == PLAYER) { |
| 57 | /* Nice side effect of new treasure creation method is that the |
| 58 | * treasure for the chest is done when the chest is created, |
| 59 | * and put into the chest inventory. So that when the chest |
| 60 | * burns up, the items still exist. Also prevents people from |
| 61 | * moving chests to more difficult maps to get better treasure |
| 62 | */ |
| 63 | |
| 64 | query_name(op, container_name, MAX_BUF); |
| 65 | |
| 66 | treas = op->inv; |
| 67 | if (treas == NULL) { |
| 68 | draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE, |
| 69 | "The chest was empty."); |
| 70 | object_decrease_nrof_by_one(op); |
| 71 | return METHOD_OK; |
| 72 | } |
| 73 | while (op->inv) { |
| 74 | treas = op->inv; |
| 75 | |
| 76 | object_remove(treas); |
| 77 | /* don't tell the player about invisible objects */ |
| 78 | if (!treas->invisible) { |
| 79 | query_name(treas, name, MAX_BUF); |
| 80 | draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS, |
| 81 | "You find %s in the %s.", |
| 82 | name, container_name); |
| 83 | } |
| 84 | |
| 85 | treas = object_insert_in_map_at(treas, applier->map, applier, INS_BELOW_ORIGINATOR, applier->x, applier->y); |
| 86 | |
| 87 | if (treas |
| 88 | && (treas->type == RUNE || treas->type == TRAP) |
| 89 | && treas->level && QUERY_FLAG(applier, FLAG_ALIVE)) |
| 90 | spring_trap(treas, applier); |
| 91 | /* If either player or container was destroyed, no need to do |
| 92 | * further processing. I think this should be enclused with |
| 93 | * spring trap above, as I don't think there is otherwise |
| 94 | * any way for the treasure chest or player to get killed |
| 95 | */ |
| 96 | if (object_was_destroyed(applier, applier_tag) || object_was_destroyed(op, op_tag)) |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | if (!object_was_destroyed(op, op_tag) && op->inv == NULL) |
| 101 | object_decrease_nrof_by_one(op); |
| 102 | } |
| 103 | return METHOD_OK; |
| 104 | } |