| 1 | /* |
| 2 | * Crossfire -- cooperative multi-player graphical RPG and adventure game |
| 3 | * |
| 4 | * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team |
| 5 | * Copyright (c) 1992 Frank Tore Johansen |
| 6 | * |
| 7 | * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are |
| 8 | * welcome to redistribute it under certain conditions. For details, please |
| 9 | * see COPYING and LICENSE. |
| 10 | * |
| 11 | * The authors can be reached via e-mail at <[email protected]>. |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * @file |
| 16 | * Those functions deal with the object/type system. |
| 17 | */ |
| 18 | |
| 19 | #include <global.h> |
| 20 | #include <ob_methods.h> |
| 21 | #include <ob_types.h> |
| 22 | #include <sproto.h> |
| 23 | |
| 24 | /* |
| 25 | * The following functions are meant for calling methods. No actual behavoir |
| 26 | * logic should be contained in this code. Code in the common/ directory should |
| 27 | * be used for logic common to all types, and should always be called by |
| 28 | * individual method code (i.e. all apply methods should call 'can_apply' from |
| 29 | * common/). Defaults for all types should not be put here either, as that code |
| 30 | * belongs in the common/ directory also, referenced to by base_type. |
| 31 | */ |
| 32 | |
| 33 | /** |
| 34 | * Apply an object by running an event hook or an object method. Consider |
| 35 | * using apply_manual() instead of this function if the applier should check |
| 36 | * for apply restrictions. |
| 37 | * @param op The object to apply |
| 38 | * @param applier The object that executes the apply action |
| 39 | * @param aflags Special (always apply/unapply) flags |
| 40 | * @retval 0 A player or monster can't apply objects of that type |
| 41 | * @retval 1 has been applied, or there was an error applying the object |
| 42 | * @retval 2 objects of that type can't be applied if not in inventory |
| 43 | */ |
| 44 | method_ret ob_apply(object *op, object *applier, int aflags) { |
| 45 | method_ret ret; |
| 46 | ob_methods *methods; |
| 47 | |
| 48 | /* Lauwenmark: Handle for plugin apply event */ |
| 49 | if (execute_event(op, EVENT_APPLY, applier, NULL, NULL, SCRIPT_FIX_ALL) != 0) |
| 50 | return METHOD_OK; |
| 51 | |
| 52 | for (methods = &type_methods[op->type]; methods; methods = methods->fallback) { |
| 53 | if (methods->apply) { |
| 54 | ret = methods->apply(methods, op, applier, aflags); |
| 55 | if (ret != METHOD_UNHANDLED) |
| 56 | return ret; |
| 57 | } |
| 58 | } |
| 59 | return METHOD_UNHANDLED; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Processes an object, giving it the opportunity to move or react. |
| 64 | * Note: The return value of ob_process doesn't seem to be used anymore. |
| 65 | * @param op The object to process |
| 66 | * @retval METHOD_UNHANDLED if the process method does not exist for that objec, |
| 67 | */ |
| 68 | method_ret ob_process(object *op) { |
| 69 | method_ret ret; |
| 70 | ob_methods *methods; |
| 71 | |
| 72 | for (methods = &type_methods[op->type]; methods; methods = methods->fallback) { |
| 73 | if (methods->process) { |
| 74 | ret = methods->process(methods, op); |
| 75 | if (ret != METHOD_UNHANDLED) |
| 76 | return ret; |
| 77 | } |
| 78 | } |
| 79 | return METHOD_UNHANDLED; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the description of an object, as seen by the given observer. |
| 84 | * @param op The object to describe |
| 85 | * @param observer The object to which the description is made |
| 86 | * @param use_media_tags whether to add media tags or not on the description |
| 87 | * @param buf Buffer that will contain the description |
| 88 | * @param size Size of buf |
| 89 | * @return |
| 90 | * buf. |
| 91 | */ |
| 92 | char *ob_describe(const object *op, const object *observer, int use_media_tags, char *buf, size_t size) { |
| 93 | ob_methods *methods; |
| 94 | |
| 95 | for (methods = &type_methods[op->type]; methods; methods = methods->fallback) { |
| 96 | if (methods->describe) { |
| 97 | methods->describe(methods, op, observer, use_media_tags, buf, size); |
| 98 | return buf; |
| 99 | } |
| 100 | } |
| 101 | buf[0] = '\0'; |
| 102 | return buf; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Makes an object move on top of another one. |
| 107 | * @param op The object over which to move |
| 108 | * @param victim The object moving over op |
| 109 | * @param originator The object that is the cause of the move |
| 110 | * @retval METHOD_UNHANDLED if the process method does not exist for that object |
| 111 | */ |
| 112 | method_ret ob_move_on(object *op, object *victim, object *originator) { |
| 113 | method_ret ret; |
| 114 | ob_methods *methods; |
| 115 | |
| 116 | for (methods = &type_methods[op->type]; methods; methods = methods->fallback) { |
| 117 | if (methods->move_on) { |
| 118 | ret = methods->move_on(methods, op, victim, originator); |
| 119 | if (ret != METHOD_UNHANDLED) |
| 120 | return ret; |
| 121 | } |
| 122 | } |
| 123 | return METHOD_UNHANDLED; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * An object is triggered by another one. |
| 128 | * @param op The object being triggered |
| 129 | * @param cause The object that is the cause of the trigger |
| 130 | * @param state trigger state, 0 for released, other for pushed |
| 131 | * @retval METHOD_UNHANDLED if the process method does not exist for that object |
| 132 | * @todo check the exact state values/meaning |
| 133 | */ |
| 134 | method_ret ob_trigger(object *op, object *cause, int state) { |
| 135 | method_ret ret; |
| 136 | ob_methods *methods; |
| 137 | |
| 138 | for (methods = &type_methods[op->type]; methods; methods = methods->fallback) { |
| 139 | if (methods->trigger) { |
| 140 | ret = methods->trigger(methods, op, cause, state); |
| 141 | if (ret != METHOD_UNHANDLED) |
| 142 | return ret; |
| 143 | } |
| 144 | } |
| 145 | return METHOD_UNHANDLED; |
| 146 | } |