Crossfire Server, Trunk  R20513
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;
63  current_context = current_context->down;
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 
141 CF_PLUGIN void *globalEventListener(int *type, ...) {
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 }
One player.
Definition: player.h:92
PyObject * third
Definition: cfpython.h:118
#define EVENT_REMOVE
A Player character has been removed.
Definition: plugin.h:93
#define EVENT_SHOUT
A player &#39;shout&#39; something.
Definition: plugin.h:94
CFPContext * current_context
int event_code
Definition: cfpython.h:122
#define EVENT_KICK
A player was Kicked by a DM.
Definition: plugin.h:97
#define EVENT_LOGIN
Player login.
Definition: plugin.h:88
PyObject * activator
Definition: cfpython.h:117
#define EVENT_MAPLEAVE
A player left a map.
Definition: plugin.h:91
char options[1024]
Definition: cfpython.h:124
#define EVENT_LOGOUT
Player logout.
Definition: plugin.h:89
CF_PLUGIN void * eventListener(int *type,...)
Handles an object-related event.
char message[1024]
Definition: cfpython.h:120
#define EVENT_TELL
A player &#39;tell&#39; something.
Definition: plugin.h:95
#define EVENT_CRASH
Triggered when the server crashes.
Definition: plugin.h:85
CF_PLUGIN int postInitPlugin(void)
Plugin was initialized, now to finish.
CF_PLUGIN int initPlugin(const char *iversion, f_plug_api gethooksptr)
Plugin initialisation function.
CF_PLUGIN int runPluginCommand(object *op, char *params)
void cf_log(LogLevel logLevel, const char *format,...)
Wrapper for LOG().
#define EVENT_MAPENTER
A player entered a map.
Definition: plugin.h:90
int cf_init_plugin(f_plug_api getHooks)
Structure used to build up dialog information when a player says something.
Definition: dialog.h:51
#define snprintf
Definition: win32.h:46
PyObject * event
Definition: cfpython.h:119
#define PLUGIN_NAME
Definition: cfanim.h:32
CFPContext * context_stack
CF_PLUGIN int closePlugin(void)
Close the plugin.
static int current_command
void pushContext(CFPContext *context)
PyObject * who
Definition: cfpython.h:116
#define EVENT_MAPRESET
A map is resetting.
Definition: plugin.h:92
#define EVENT_GKILL
Triggered when anything got killed by anyone.
Definition: plugin.h:87
object * ob
The object representing the player.
Definition: player.h:158
#define EVENT_CLOCK
Global time event.
Definition: plugin.h:84
CF_PLUGIN void * globalEventListener(int *type,...)
Represents one command.
Definition: commands.h:37
#define CF_PLUGIN
Definition: plugin_common.h:38
Only for debugging purposes.
Definition: logger.h:13
CFPContext * popContext(void)
void initContextStack(void)
CF_PLUGIN void * getPluginProperty(int *type,...)
Gets a plugin property.
struct _cfpcontext * down
Definition: cfpython.h:115
#define EVENT_PLAYER_DEATH
Global Death event.
Definition: plugin.h:86
int returnvalue
Definition: cfpython.h:125
#define EVENT_BORN
A new character has been created.
Definition: plugin.h:83
void(* f_plug_api)(int *type,...)
General API function.
Definition: plugin.h:125
#define EVENT_MUZZLE
A player was Muzzled (no_shout set).
Definition: plugin.h:96
#define PLUGIN_VERSION
Definition: cfanim.h:33