Crossfire Server, Trunk
plugin_template.c
Go to the documentation of this file.
1 /*****************************************************************************/
2 /* Template for version 2.0 plugins. */
3 /* Contact: yann.chachkoff@myrealbox.com */
4 /*****************************************************************************/
5 /* That code is placed under the GNU General Public Licence (GPL) */
6 /* (C)2001-2005 by Chachkoff Yann (Feel free to deliver your complaints) */
7 /*****************************************************************************/
8 /* CrossFire, A Multiplayer game for X-windows */
9 /* */
10 /* Copyright (C) 2000 Mark Wedel */
11 /* Copyright (C) 1992 Frank Tore Johansen */
12 /* */
13 /* This program is free software; you can redistribute it and/or modify */
14 /* it under the terms of the GNU General Public License as published by */
15 /* the Free Software Foundation; either version 2 of the License, or */
16 /* (at your option) any later version. */
17 /* */
18 /* This program is distributed in the hope that it will be useful, */
19 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
20 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
21 /* GNU General Public License for more details. */
22 /* */
23 /* You should have received a copy of the GNU General Public License */
24 /* along with this program; if not, write to the Free Software */
25 /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 /* */
27 /*****************************************************************************/
28 
29 /* First let's include the header file needed */
30 
31 #include <plugin_template.h>
32 #include <stdarg.h>
33 #include <plugin_template_proto.h>
34 
36 
38 
39 static int current_command = -999;
40 
41 void initContextStack(void) {
42  current_context = NULL;
43  context_stack = NULL;
44 }
45 
46 void pushContext(CFPContext *context) {
47  CFPContext *stack_context;
48 
49  if (current_context == NULL) {
50  context_stack = context;
51  context->down = NULL;
52  } else {
53  context->down = current_context;
54  }
55  current_context = context;
56 }
57 
59  CFPContext *oldcontext;
60 
61  if (current_context != NULL) {
62  oldcontext = current_context;
64  return oldcontext;
65  } else
66  return NULL;
67 }
68 
69 CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr) {
70  cf_init_plugin(gethooksptr);
71 
72  cf_log(llevDebug, PLUGIN_VERSION " init\n");
73 
74  /* Place your initialization code here */
75  return 0;
76 }
77 
78 CF_PLUGIN void *getPluginProperty(int *type, ...) {
79  va_list args;
80  const char *propname;
81  int i, size;
82  command_array_struct *rtn_cmd;
83  char *buf;
84 
85  va_start(args, type);
86  propname = va_arg(args, const char *);
87 
88  if (!strcmp(propname, "command?")) {
89  const char *cmdname;
90  cmdname = va_arg(args, const char *);
91  rtn_cmd = va_arg(args, command_array_struct *);
92  va_end(args);
93 
95  return NULL;
96  } else if (!strcmp(propname, "Identification")) {
97  buf = va_arg(args, char *);
98  size = va_arg(args, int);
99  va_end(args);
100  snprintf(buf, size, PLUGIN_NAME);
101  return NULL;
102  } else if (!strcmp(propname, "FullName")) {
103  buf = va_arg(args, char *);
104  size = va_arg(args, int);
105  va_end(args);
106  snprintf(buf, size, PLUGIN_VERSION);
107  return NULL;
108  }
109  va_end(args);
110  return NULL;
111 }
112 
113 CF_PLUGIN int runPluginCommand(object *op, char *params) {
114  return -1;
115 }
116 
118  cf_log(llevDebug, PLUGIN_VERSION" post init\n");
120  /* Pick the global events you want to monitor from this plugin */
121 /*
122  cf_system_register_global_event(EVENT_BORN, PLUGIN_NAME, globalEventListener);
123  cf_system_register_global_event(EVENT_CLOCK, PLUGIN_NAME, globalEventListener);
124  cf_system_register_global_event(EVENT_CRASH, PLUGIN_NAME, globalEventListener);
125  cf_system_register_global_event(EVENT_PLAYER_DEATH, PLUGIN_NAME, globalEventListener);
126  cf_system_register_global_event(EVENT_GKILL, PLUGIN_NAME, globalEventListener);
127  cf_system_register_global_event(EVENT_LOGIN, PLUGIN_NAME, globalEventListener);
128  cf_system_register_global_event(EVENT_LOGOUT, PLUGIN_NAME, globalEventListener);
129  cf_system_register_global_event(EVENT_MAPENTER, PLUGIN_NAME, globalEventListener);
130  cf_system_register_global_event(EVENT_MAPLEAVE, PLUGIN_NAME, globalEventListener);
131  cf_system_register_global_event(EVENT_MAPRESET, PLUGIN_NAME, globalEventListener);
132  cf_system_register_global_event(EVENT_REMOVE, PLUGIN_NAME, globalEventListener);
133  cf_system_register_global_event(EVENT_SHOUT, PLUGIN_NAME, globalEventListener);
134  cf_system_register_global_event(EVENT_TELL, PLUGIN_NAME, globalEventListener);
135  cf_system_register_global_event(EVENT_MUZZLE, PLUGIN_NAME, globalEventListener);
136  cf_system_register_global_event(EVENT_KICK, PLUGIN_NAME, globalEventListener);
137 */
138  return 0;
139 }
140 
142  va_list args;
143  static int rv = 0;
144  CFPContext *context;
145  context = malloc(sizeof(CFPContext));
146  char *buf;
147  player *pl;
148  object *op;
149 
150  va_start(args, type);
151  context->event_code = va_arg(args, int);
152  printf("****** Global event listener called ***********\n");
153  printf("- Event code: %d\n", context->event_code);
154 
155  context->message[0] = 0;
156 
157  context->who = NULL;
158  context->activator = NULL;
159  context->third = NULL;
160  context->event = NULL;
161  rv = context->returnvalue = 0;
162  switch (context->event_code) {
163  case EVENT_CRASH:
164  printf("Unimplemented for now\n");
165  break;
166 
167  case EVENT_BORN:
168  context->activator = va_arg(args, object *);
169  break;
170 
171  case EVENT_PLAYER_DEATH:
172  context->who = va_arg(args, object *);
173  context->activator = va_arg(args, object *);
174  break;
175 
176  case EVENT_GKILL:
177  context->who = va_arg(args, object *);
178  context->activator = va_arg(args, object *);
179  break;
180 
181  case EVENT_LOGIN:
182  pl = va_arg(args, player *);
183  context->activator = pl->ob;
184  buf = va_arg(args, char *);
185  if (buf != 0)
186  strcpy(context->message, buf);
187  break;
188 
189  case EVENT_LOGOUT:
190  pl = va_arg(args, player *);
191  context->activator = pl->ob;
192  buf = va_arg(args, char *);
193  if (buf != 0)
194  strcpy(context->message, buf);
195  break;
196 
197  case EVENT_REMOVE:
198  context->activator = va_arg(args, object *);
199  break;
200 
201  case EVENT_SHOUT:
202  context->activator = va_arg(args, object *);
203  buf = va_arg(args, char *);
204  if (buf != 0)
205  strcpy(context->message, buf);
206  break;
207 
208  case EVENT_MUZZLE:
209  context->activator = va_arg(args, object *);
210  buf = va_arg(args, char *);
211  if (buf != 0)
212  strcpy(context->message, buf);
213  break;
214 
215  case EVENT_KICK:
216  context->activator = va_arg(args, object *);
217  buf = va_arg(args, char *);
218  if (buf != 0)
219  strcpy(context->message, buf);
220  break;
221 
222  case EVENT_MAPENTER:
223  context->activator = va_arg(args, object *);
224  break;
225 
226  case EVENT_MAPLEAVE:
227  context->activator = va_arg(args, object *);
228  break;
229 
230  case EVENT_CLOCK:
231  break;
232 
233  case EVENT_MAPRESET:
234  buf = va_arg(args, char *);
235  if (buf != 0)
236  strcpy(context->message, buf);
237  break;
238 
239  case EVENT_TELL:
240  context->activator = va_arg(args, object *);
241  buf = va_arg(args, char *);
242  if (buf != 0)
243  strcpy(context->message, buf);
244  context->third = va_arg(args, object *);
245  break;
246  }
247  va_end(args);
248  context->returnvalue = 0;
249 
250  pushContext(context);
251  /* Put your plugin action(s) here */
252  context = popContext();
253  rv = context->returnvalue;
254  free(context);
255  cf_log(llevDebug, "*********** Execution complete ****************\n");
256 
257  return &rv;
258 }
259 
260 CF_PLUGIN void *eventListener(int *type, ...) {
261  static int rv = 0;
262  va_list args;
263  char *buf;
264  CFPContext *context;
265 
266  context = malloc(sizeof(CFPContext));
267 
268  context->message[0] = 0;
269 
270  va_start(args, type);
271 
272  context->who = va_arg(args, object *);
273  context->activator = va_arg(args, object *);
274  context->third = va_arg(args, object *);
275  buf = va_arg(args, char *);
276  if (buf != 0)
277  strcpy(context->message, buf);
278  context->fix = va_arg(args, int);
279  context->event = va_arg(args, object *);
280  context->event_code = context->event->subtype;
281  strncpy(context->options, context->event->name, sizeof(context->options));
282  context->returnvalue = 0;
283  va_arg(args, talk_info *); /* ignored for now */
284  va_end(args);
285 
286  pushContext(context);
287  /* Put your plugin action(s) here */
288  context = popContext();
289  rv = context->returnvalue;
290  free(context);
291  cf_log(llevDebug, "Execution complete");
292  return &rv;
293 }
294 
296  cf_log(llevDebug, PLUGIN_VERSION " closing\n");
297  return 0;
298 }
_cfpcontext::message
char message[1024]
Definition: cfpython.h:97
cf_log
void cf_log(LogLevel logLevel, const char *format,...)
Definition: plugin_common.c:1512
_cfpcontext::third
PyObject * third
Definition: cfpython.h:95
initPlugin
CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr)
Definition: plugin_template.c:69
_cfpcontext::event_code
int event_code
Definition: cfpython.h:98
pl
Definition: player.h:105
current_command
static int current_command
Definition: plugin_template.c:39
pl::ob
object * ob
Definition: player.h:176
getPluginProperty
CF_PLUGIN void * getPluginProperty(int *type,...)
Definition: plugin_template.c:78
EVENT_LOGOUT
#define EVENT_LOGOUT
Definition: events.h:44
_cfpcontext::returnvalue
int returnvalue
Definition: cfpython.h:101
_cfpcontext::event
PyObject * event
Definition: cfpython.h:96
closePlugin
CF_PLUGIN int closePlugin(void)
Definition: plugin_template.c:295
plugin_template.h
EVENT_LOGIN
#define EVENT_LOGIN
Definition: events.h:43
popContext
CFPContext * popContext(void)
Definition: plugin_template.c:58
EVENT_CLOCK
#define EVENT_CLOCK
Definition: events.h:39
make_face_from_files.args
args
Definition: make_face_from_files.py:31
EVENT_CRASH
#define EVENT_CRASH
Definition: events.h:40
CF_PLUGIN
#define CF_PLUGIN
Definition: plugin_common.h:38
_cfpcontext::down
struct _cfpcontext * down
Definition: cfpython.h:92
_cfpcontext::who
PyObject * who
Definition: cfpython.h:93
f_plug_api
void(* f_plug_api)(int *type,...)
Definition: plugin.h:81
EVENT_MAPENTER
#define EVENT_MAPENTER
Definition: events.h:45
cf_init_plugin
int cf_init_plugin(f_plug_api getHooks)
Definition: plugin_common.c:141
eventListener
CF_PLUGIN void * eventListener(int *type,...)
Definition: plugin_template.c:260
PLUGIN_NAME
#define PLUGIN_NAME
Definition: cfanim.h:32
EVENT_MAPRESET
#define EVENT_MAPRESET
Definition: events.h:49
EVENT_BORN
#define EVENT_BORN
Definition: events.h:38
postInitPlugin
CF_PLUGIN int postInitPlugin(void)
Definition: plugin_template.c:117
EVENT_SHOUT
#define EVENT_SHOUT
Definition: events.h:54
EVENT_PLAYER_DEATH
#define EVENT_PLAYER_DEATH
Definition: events.h:52
PLUGIN_VERSION
#define PLUGIN_VERSION
Definition: cfanim.h:33
current_context
CFPContext * current_context
Definition: plugin_template.c:37
EVENT_MUZZLE
#define EVENT_MUZZLE
Definition: events.h:51
EVENT_TELL
#define EVENT_TELL
Definition: events.h:55
_cfpcontext::fix
int fix
Definition: plugin_template.h:61
give.op
op
Definition: give.py:33
_cfpcontext::activator
PyObject * activator
Definition: cfpython.h:94
EVENT_MAPLEAVE
#define EVENT_MAPLEAVE
Definition: events.h:46
roll-o-matic.params
params
Definition: roll-o-matic.py:193
buf
StringBuffer * buf
Definition: readable.c:1610
talk_info
Definition: dialog.h:51
EVENT_REMOVE
#define EVENT_REMOVE
Definition: events.h:53
_cfpcontext
Definition: cfpython.h:91
globalEventListener
CF_PLUGIN void * globalEventListener(int *type,...)
Definition: plugin_template.c:141
pushContext
void pushContext(CFPContext *context)
Definition: plugin_template.c:46
initContextStack
void initContextStack(void)
Definition: plugin_template.c:41
context_stack
CFPContext * context_stack
Definition: plugin_template.c:35
EVENT_GKILL
#define EVENT_GKILL
Definition: events.h:41
altar_valkyrie.pl
pl
Definition: altar_valkyrie.py:28
runPluginCommand
CF_PLUGIN int runPluginCommand(object *op, char *params)
Definition: plugin_template.c:113
EVENT_KICK
#define EVENT_KICK
Definition: events.h:42
_cfpcontext::options
char options[1024]
Definition: cfpython.h:100
llevDebug
@ llevDebug
Definition: logger.h:13
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25