Crossfire Server, Trunk  R21041
c_new.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 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "commands.h"
25 #include "sproto.h"
26 
27 #ifndef tolower
28 
29 #define tolower(C) (((C) >= 'A' && (C) <= 'Z') ? (C)-'A'+'a' : (C))
30 #endif
31 
45 static int compare_A(const void *a, const void *b) {
46  return strcmp(((const command_array_struct *)a)->name,
47  ((const command_array_struct *)b)->name);
48 }
49 
62 static command_array_struct *find_command_element(const char *cmd, command_array_struct *commarray, int commsize) {
63  command_array_struct *asp, dummy;
64 
65  dummy.name = cmd;
66  asp = (command_array_struct *)bsearch((void *)&dummy,
67  (void *)commarray, commsize,
68  sizeof(command_array_struct),
69  compare_A);
70  return asp;
71 }
72 
83 void execute_newserver_command(object *pl, char *command) {
84  command_array_struct *csp, sent;
85  char *cp, *low;
86 
87  pl->contr->has_hit = 0;
88 
89  /*
90  * remove trailing spaces from commant
91  */
92  cp = command+strlen(command)-1;
93  while ((cp >= command) && (*cp == ' ')) {
94  *cp = '\0';
95  cp--;
96  }
97  cp = strchr(command, ' ');
98  if (cp) {
99  *(cp++) = '\0';
100  while (*cp == ' ')
101  cp++;
102  } else {
103  cp = strchr(command, '\0');
104  }
105 
106  for (low = command; *low; low++)
107  *low = tolower(*low);
108 
109  csp = find_plugin_command(command, &sent);
110  if (!csp)
111  csp = find_command_element(command, Commands, CommandsSize);
112  if (!csp)
115  if (!csp && QUERY_FLAG(pl, FLAG_WIZ))
117 
118  if (csp == NULL) {
121  "'%s' is not a valid command.",
122  command);
123  return;
124  }
125 
126  pl->speed_left -= csp->time;
127 
128  /* A character time can never exceed his speed (which in many cases,
129  * if wearing armor, is less than one.) Thus, in most cases, if
130  * the command takes 1.0, the player's speed will be less than zero.
131  * it is only really an issue if time goes below -1
132  * Due to various reasons that are too long to go into here, we will
133  * actually still execute player even if his time is less than 0,
134  * but greater than -1. This is to improve the performance of the
135  * new client/server. In theory, it shouldn't make much difference.
136  */
137 
138  if (csp->time && pl->speed_left < -2.0) {
139  LOG(llevDebug, "execute_newclient_command: Player issued command that takes more time than he has left.\n");
140  }
141  csp->func(pl, cp);
142 }
143 
152 void command_run(object *op, const char *params) {
153  int dir;
154 
155  dir = atoi(params);
156  if (dir < 0 || dir >= 9) {
158  "Can't run into a non adjacent square.");
159  return;
160  }
161  op->contr->run_on = 1;
162  move_player(op, dir);
163 }
164 
175 void command_run_stop(object *op, const char *params) {
176  op->contr->run_on = 0;
177 }
178 
187 void command_fire(object *op, const char *params) {
188  int dir;
189 
190  dir = atoi(params);
191  if (dir < 0 || dir >= 9) {
193  "Can't fire to a non adjacent square.");
194  return;
195  }
196  op->contr->fire_on = 1;
197  move_player(op, dir);
198 }
199 
208 void command_fire_stop(object *op, const char *params) {
209  op->contr->fire_on = 0;
210 }
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...)
Definition: main.c:315
Definition: player.h:92
command_array_struct * find_plugin_command(const char *cmd, command_array_struct *command)
Definition: plugins.c:4711
command_array_struct Commands[]
Definition: commands.c:36
int move_player(object *op, int dir)
Definition: player.c:3014
const int WizCommandsSize
Definition: commands.c:265
uint32_t run_on
Definition: player.h:128
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.c:310
const int CommunicationCommandSize
Definition: commands.c:199
#define MSG_TYPE_COMMAND
Definition: newclient.h:379
float speed_left
Definition: object.h:329
#define MSG_TYPE_COMMAND_ERROR
Definition: newclient.h:509
static int compare_A(const void *a, const void *b)
Definition: c_new.c:45
void command_run(object *op, const char *params)
Definition: c_new.c:152
void command_run_stop(object *op, const char *params)
Definition: c_new.c:175
uint32_t has_hit
Definition: player.h:129
struct pl * contr
Definition: object.h:276
const char * name
Definition: commands.h:38
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define FLAG_WIZ
Definition: define.h:231
void execute_newserver_command(object *pl, char *command)
Definition: c_new.c:83
command_array_struct WizCommands[]
Definition: commands.c:202
#define tolower(C)
Definition: c_new.c:29
void command_fire(object *op, const char *params)
Definition: c_new.c:187
command_function func
Definition: commands.h:39
uint32_t fire_on
Definition: player.h:127
const int CommandsSize
Definition: commands.c:126
command_array_struct CommunicationCommands[]
Definition: commands.c:129
#define NDI_UNIQUE
Definition: newclient.h:245
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
static command_array_struct * find_command_element(const char *cmd, command_array_struct *commarray, int commsize)
Definition: c_new.c:62
void command_fire_stop(object *op, const char *params)
Definition: c_new.c:208