Crossfire Server, Trunk  R20513
ob_methods.c
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 
42 method_ret ob_apply(object *op, object *applier, int aflags) {
43  method_ret ret;
44  ob_methods *methods;
45 
46  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
47  if (methods->apply) {
48  ret = methods->apply(methods, op, applier, aflags);
49  if (ret != METHOD_UNHANDLED)
50  return ret;
51  }
52  }
53  return METHOD_UNHANDLED;
54 }
55 
62 method_ret ob_process(object *op) {
63  method_ret ret;
64  ob_methods *methods;
65 
66  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
67  if (methods->process) {
68  ret = methods->process(methods, op);
69  if (ret != METHOD_UNHANDLED)
70  return ret;
71  }
72  }
73  return METHOD_UNHANDLED;
74 }
75 
85 char *ob_describe(const object *op, const object *observer, char *buf, size_t size) {
86  ob_methods *methods;
87 
88  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
89  if (methods->describe) {
90  methods->describe(methods, op, observer, buf, size);
91  return buf;
92  }
93  }
94  buf[0] = '\0';
95  return buf;
96 }
97 
105 method_ret ob_move_on(object *op, object *victim, object *originator) {
106  method_ret ret;
107  ob_methods *methods;
108 
109  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
110  if (methods->move_on) {
111  ret = methods->move_on(methods, op, victim, originator);
112  if (ret != METHOD_UNHANDLED)
113  return ret;
114  }
115  }
116  return METHOD_UNHANDLED;
117 }
118 
127 method_ret ob_trigger(object *op, object *cause, int state) {
128  method_ret ret;
129  ob_methods *methods;
130 
131  for (methods = &type_methods[op->type]; methods; methods = methods->fallback) {
132  if (methods->trigger) {
133  ret = methods->trigger(methods, op, cause, state);
134  if (ret != METHOD_UNHANDLED)
135  return ret;
136  }
137  }
138  return METHOD_UNHANDLED;
139 }
ob_methods type_methods[OBJECT_TYPE_MAX]
Registered method handlers.
Definition: ob_types.c:25
Typedefs for ob_methods.
Definition: ob_methods.h:45
struct ob_methods * fallback
ob_method structure to fallback to
Definition: ob_methods.h:51
apply_func apply
The apply method.
Definition: ob_methods.h:46
method_ret ob_process(object *op)
Processes an object, giving it the opportunity to move or react.
Definition: ob_methods.c:62
method_ret ob_apply(object *op, object *applier, int aflags)
Applies an object.
Definition: ob_methods.c:42
Global type definitions and header inclusions.
char method_ret
Define some standard return values for callbacks which don&#39;t need to return any other results...
Definition: ob_methods.h:14
move_on_func move_on
The move_on method.
Definition: ob_methods.h:49
#define METHOD_UNHANDLED
Definition: ob_methods.h:16
char * ob_describe(const object *op, const object *observer, char *buf, size_t size)
Returns the description of an object, as seen by the given observer.
Definition: ob_methods.c:85
method_ret ob_move_on(object *op, object *victim, object *originator)
Makes an object move on top of another one.
Definition: ob_methods.c:105
trigger_func trigger
When something is triggered via a button.
Definition: ob_methods.h:50
Object type variables.
process_func process
The process method.
Definition: ob_methods.h:47
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:338
describe_func describe
The describe method.
Definition: ob_methods.h:48
method_ret ob_trigger(object *op, object *cause, int state)
An object is triggered by another one.
Definition: ob_methods.c:127
Object type functions and variables.