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 
91 char *ob_describe(const object *op, const object *observer, int use_media_tags, char *buf, size_t size) {
92  ob_methods *methods;
93 
94  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
95  if (methods->describe) {
96  methods->describe(op, observer, use_media_tags, buf, size);
97  return buf;
98  }
99  }
100  buf[0] = '\0';
101  return buf;
102 }
103 
111 method_ret ob_move_on(object *op, object *victim, object *originator) {
112  method_ret ret;
113  ob_methods *methods;
114 
115  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
116  if (methods->move_on) {
117  ret = methods->move_on(op, victim, originator);
118  if (ret != METHOD_UNHANDLED)
119  return ret;
120  }
121  }
122  return METHOD_UNHANDLED;
123 }
124 
133 method_ret ob_trigger(object *op, object *cause, int state) {
134  method_ret ret;
135  ob_methods *methods;
136 
137  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
138  if (methods->trigger) {
139  ret = methods->trigger(op, cause, state);
140  if (ret != METHOD_UNHANDLED)
141  return ret;
142  }
143  }
144  return METHOD_UNHANDLED;
145 }
global.h
EVENT_APPLY
#define EVENT_APPLY
Object applied-unapplied.
Definition: events.h:28
ob_trigger
method_ret ob_trigger(object *op, object *cause, int state)
An object is triggered by another one.
Definition: ob_methods.cpp:133
ob_methods::move_on
move_on_func move_on
The move_on method.
Definition: ob_methods.h:47
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
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:111
ob_methods::fallback
ob_methods * fallback
ob_method structure to fallback to
Definition: ob_methods.h:49
METHOD_UNHANDLED
#define METHOD_UNHANDLED
Definition: ob_methods.h:16
ob_methods::apply
apply_func apply
The apply method.
Definition: ob_methods.h:44
ob_methods
This struct stores function pointers for actions that can be done to objects.
Definition: ob_methods.h:43
SCRIPT_FIX_ALL
#define SCRIPT_FIX_ALL
Definition: global.h:380
ob_describe
char * ob_describe(const object *op, const object *observer, int use_media_tags, char *buf, size_t size)
Returns the description of an object, as seen by the given observer.
Definition: ob_methods.cpp:91
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:46
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:45
ob_methods::trigger
trigger_func trigger
When something is triggered via a button.
Definition: ob_methods.h:48
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