Crossfire Server, Branches 1.12  R18729
c_new.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_c_new_c =
3  * "$Id: c_new.c 11578 2009-02-23 22:02:27Z lalo $";
4  */
5 
6 /*
7  CrossFire, A Multiplayer game for X-windows
8 
9  Copyright (C) 2002-2006 Mark Wedel & Crossfire Development Team
10  Copyright (C) 1992 Frank Tore Johansen
11 
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 
26  The author can be reached via e-mail to crossfire-devel@real-time.com
27 */
28 
34 #include <global.h>
35 #include <commands.h>
36 #ifndef __CEXTRACT__
37 #include <sproto.h>
38 #endif
39 
40 #ifndef tolower
41 
42 #define tolower(C) (((C) >= 'A' && (C) <= 'Z') ? (C)-'A'+'a' : (C))
43 #endif
44 
58 static int compare_A(const void *a, const void *b) {
59  return strcmp(((const command_array_struct *)a)->name,
60  ((const command_array_struct *)b)->name);
61 }
62 
75 static command_array_struct *find_command_element(const char *cmd, command_array_struct *commarray, int commsize) {
76  command_array_struct *asp, dummy;
77 
78  dummy.name = cmd;
79  asp = (command_array_struct *)bsearch((void *)&dummy,
80  (void *)commarray, commsize,
81  sizeof(command_array_struct),
82  compare_A);
83  return asp;
84 }
85 
98 int execute_newserver_command(object *pl, char *command) {
100  char *cp, *low;
101 
102  pl->contr->has_hit = 0;
103 
104  /*
105  * remove trailing spaces from commant
106  */
107  cp = command+strlen(command)-1;
108  while ((cp >= command) && (*cp == ' ')) {
109  *cp = '\0';
110  cp--;
111  }
112  cp = strchr(command, ' ');
113  if (cp) {
114  *(cp++) = '\0';
115  while (*cp == ' ')
116  cp++;
117  }
118 
119  for (low = command; *low; low++)
120  *low = tolower(*low);
121 
122  csp = find_plugin_command(command, pl);
123  if (!csp)
124  csp = find_command_element(command, Commands, CommandsSize);
125  if (!csp)
128  if (!csp && QUERY_FLAG(pl, FLAG_WIZ))
130 
131  if (csp == NULL) {
134  "'%s' is not a valid command.",
135  "'%s' is not a valid command.",
136  command);
137  return 0;
138  }
139 
140  pl->speed_left -= csp->time;
141 
142  /* A character time can never exceed his speed (which in many cases,
143  * if wearing armor, is less than one.) Thus, in most cases, if
144  * the command takes 1.0, the player's speed will be less than zero.
145  * it is only really an issue if time goes below -1
146  * Due to various reasons that are too long to go into here, we will
147  * actually still execute player even if his time is less than 0,
148  * but greater than -1. This is to improve the performance of the
149  * new client/server. In theory, it shouldn't make much difference.
150  */
151 
152  if (csp->time && pl->speed_left < -2.0) {
153  LOG(llevDebug, "execute_newclient_command: Player issued command that takes more time than he has left.\n");
154  }
155  return csp->func(pl, cp);
156 }
157 
168 int command_run(object *op, char *params) {
169  int dir;
170 
171  dir = params ? atoi(params) : 0;
172  if (dir < 0 || dir >= 9) {
174  "Can't run into a non adjacent square.", NULL);
175  return 0;
176  }
177  op->contr->run_on = 1;
178  return move_player(op, dir);
179 }
180 
191 int command_run_stop(object *op, char *params) {
192  op->contr->run_on = 0;
193  return 1;
194 }
195 
206 int command_fire(object *op, char *params) {
207  int dir;
208 
209  dir = params ? atoi(params) : 0;
210  if (dir < 0 || dir >= 9) {
212  "Can't fire to a non adjacent square.", NULL);
213  return 0;
214  }
215  op->contr->fire_on = 1;
216  return move_player(op, dir);
217 }
218 
229 int command_fire_stop(object *op, char *params) {
230  op->contr->fire_on = 0;
231  return 1;
232 }
int command_fire_stop(object *op, char *params)
Definition: c_new.c:229
Definition: player.h:146
command_array_struct Commands[]
Definition: commands.c:49
int move_player(object *op, int dir)
Definition: player.c:2741
const int WizCommandsSize
Definition: commands.c:272
int command_run(object *op, char *params)
Definition: c_new.c:168
void draw_ext_info(int flags, int pri, const object *pl, uint8 type, uint8 subtype, const char *message, const char *oldmessage)
Definition: standalone.c:171
uint32 run_on
Definition: player.h:182
void draw_ext_info_format(int flags, int pri, const object *pl, uint8 type, uint8 subtype, const char *new_format, const char *old_format,...)
Definition: standalone.c:175
const int CommunicationCommandSize
Definition: commands.c:208
float speed_left
Definition: object.h:182
static int compare_A(const void *a, const void *b)
Definition: c_new.c:58
struct pl * contr
Definition: object.h:134
int command_run_stop(object *op, char *params)
Definition: c_new.c:191
int command_fire(object *op, char *params)
Definition: c_new.c:206
const char * name
Definition: commands.h:59
#define QUERY_FLAG(xyz, p)
Definition: define.h:514
#define FLAG_WIZ
Definition: define.h:527
command_array_struct WizCommands[]
Definition: commands.c:211
#define tolower(C)
Definition: c_new.c:42
command_function func
Definition: commands.h:60
int execute_newserver_command(object *pl, char *command)
Definition: c_new.c:98
const int CommandsSize
Definition: commands.c:137
command_array_struct CommunicationCommands[]
Definition: commands.c:140
#define MSG_TYPE_COMMAND
Definition: newclient.h:326
uint32 fire_on
Definition: player.h:181
#define NDI_UNIQUE
Definition: newclient.h:219
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:63
command_array_struct * find_plugin_command(char *cmd, object *op)
Definition: plugins.c:4568
static command_array_struct * find_command_element(const char *cmd, command_array_struct *commarray, int commsize)
Definition: c_new.c:75
#define MSG_TYPE_COMMAND_ERROR
Definition: newclient.h:447
uint32 has_hit
Definition: player.h:183