Crossfire Server, Trunk  1.75.0
ob_methods.cpp
Go to the documentation of this file.
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 <crossfire@metalforge.org>.
12  */
13 
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 
44 method_ret ob_apply(object *op, object *applier, int aflags) {
45  method_ret ret;
46  ob_methods *methods;
47 
48  if (events_execute_object_event(op, EVENT_APPLY, applier, NULL, NULL, SCRIPT_FIX_ALL) != 0)
49  return METHOD_OK;
50 
51  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
52  if (methods->apply) {
53  ret = methods->apply(op, applier, aflags);
54  if (ret != METHOD_UNHANDLED)
55  return ret;
56  }
57  }
58  return METHOD_UNHANDLED;
59 }
60 
67 method_ret ob_process(object *op) {
68  method_ret ret;
69  ob_methods *methods;
70 
71  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
72  if (methods->process) {
73  ret = methods->process(op);
74  if (ret != METHOD_UNHANDLED)
75  return ret;
76  }
77  }
78  return METHOD_UNHANDLED;
79 }
80 
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(op, observer, use_media_tags, buf, size);
98  return buf;
99  }
100  }
101  buf[0] = '\0';
102  return buf;
103 }
104 
113 method_ret ob_examine(const object *op, const object *observer, int use_media_tags, char *buf, size_t size) {
114  method_ret ret;
115  ob_methods *methods;
116 
117  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
118  if (methods->examine) {
119  ret = methods->examine(op, observer, use_media_tags, buf, size);
120  if (ret != METHOD_UNHANDLED)
121  return ret;
122  }
123  }
124  return METHOD_UNHANDLED;
125 }
126 
134 method_ret ob_move_on(object *op, object *victim, object *originator) {
135  method_ret ret;
136  ob_methods *methods;
137 
138  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
139  if (methods->move_on) {
140  ret = methods->move_on(op, victim, originator);
141  if (ret != METHOD_UNHANDLED)
142  return ret;
143  }
144  }
145  return METHOD_UNHANDLED;
146 }
147 
156 method_ret ob_trigger(object *op, object *cause, int state) {
157  method_ret ret;
158  ob_methods *methods;
159 
160  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
161  if (methods->trigger) {
162  ret = methods->trigger(op, cause, state);
163  if (ret != METHOD_UNHANDLED)
164  return ret;
165  }
166  }
167  return METHOD_UNHANDLED;
168 }
global.h
EVENT_APPLY
#define EVENT_APPLY
Object applied-unapplied.
Definition: events.h:28
ob_examine
method_ret ob_examine(const object *op, const object *observer, int use_media_tags, char *buf, size_t size)
Get examine text for OP as seen by OBSERVER.
Definition: ob_methods.cpp:113
ob_trigger
method_ret ob_trigger(object *op, object *cause, int state)
An object is triggered by another one.
Definition: ob_methods.cpp:156
ob_methods::move_on
move_on_func move_on
The move_on method.
Definition: ob_methods.h:49
METHOD_OK
#define METHOD_OK
Definition: ob_methods.h:15
type_methods
ob_methods type_methods[OBJECT_TYPE_MAX]
Registered method handlers.
Definition: ob_types.cpp:25
SCRIPT_FIX_ALL
#define SCRIPT_FIX_ALL
Definition: global.h:380
events_execute_object_event
int events_execute_object_event(object *op, int eventcode, object *activator, object *third, const char *message, int fix)
Execute an event on the specified object.
Definition: events.cpp:309
buf
StringBuffer * buf
Definition: readable.cpp:1565
ob_move_on
method_ret ob_move_on(object *op, object *victim, object *originator)
Makes an object move on top of another one.
Definition: ob_methods.cpp:134
ob_methods::fallback
ob_methods * fallback
ob_method structure to fallback to
Definition: ob_methods.h:51
METHOD_UNHANDLED
#define METHOD_UNHANDLED
Definition: ob_methods.h:16
ob_methods::examine
examine_func examine
Definition: ob_methods.h:48
ob_methods::apply
apply_func apply
The apply method.
Definition: ob_methods.h:45
ob_methods
This struct stores function pointers for actions that can be done to objects.
Definition: ob_methods.h:44
ob_describe
char * ob_describe(const object *op, const object *observer, int use_media_tags, char *buf, size_t size)
Returns the description (short item name) of an object, as seen by the given observer.
Definition: ob_methods.cpp:92
object::type
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:348
sproto.h
ob_methods::describe
describe_func describe
The describe method.
Definition: ob_methods.h:47
method_ret
char method_ret
Define some standard return values for callbacks which don't need to return any other results.
Definition: ob_methods.h:14
ob_types.h
ob_methods::process
process_func process
The process method.
Definition: ob_methods.h:46
ob_methods::trigger
trigger_func trigger
When something is triggered via a button.
Definition: ob_methods.h:50
ob_process
method_ret ob_process(object *op)
Processes an object, giving it the opportunity to move or react.
Definition: ob_methods.cpp:67
ob_apply
method_ret ob_apply(object *op, object *applier, int aflags)
Apply an object by running an event hook or an object method.
Definition: ob_methods.cpp:44
ob_methods.h