Crossfire Server, Trunk  R20513
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,...)
Sends message to player(s).
Definition: main.c:315
One player.
Definition: player.h:92
command_array_struct * find_plugin_command(const char *cmd, command_array_struct *command)
Tries to find if a given command is handled by a plugin.
Definition: plugins.c:4704
command_array_struct Commands[]
Normal game commands.
Definition: commands.c:36
int move_player(object *op, int dir)
Player gave us a direction, check whether to move or fire.
Definition: player.c:3007
const int WizCommandsSize
Length of WizCommands array.
Definition: commands.c:265
uint32_t run_on
Player should keep moving in dir until run is off.
Definition: player.h:128
Global type definitions and header inclusions.
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Sends message to player(s).
Definition: main.c:310
const int CommunicationCommandSize
Length of the CommunicationCommands array.
Definition: commands.c:199
#define MSG_TYPE_COMMAND
Responses to commands, eg, who.
Definition: newclient.h:379
float speed_left
How much speed is left to spend this round.
Definition: object.h:329
#define MSG_TYPE_COMMAND_ERROR
Bad syntax/can&#39;t use command.
Definition: newclient.h:509
static int compare_A(const void *a, const void *b)
Compare function for commands.
Definition: c_new.c:45
void command_run(object *op, const char *params)
Player wants to start running.
Definition: c_new.c:152
void command_run_stop(object *op, const char *params)
Player wants to stop running.
Definition: c_new.c:175
uint32_t has_hit
If set, weapon_sp instead of speed will count.
Definition: player.h:129
struct pl * contr
Pointer to the player which control this object.
Definition: object.h:276
Defines and structures related to commands the player can send.
const char * name
Command name.
Definition: commands.h:38
#define QUERY_FLAG(xyz, p)
Definition: define.h:225
#define FLAG_WIZ
Object has special privilegies.
Definition: define.h:231
void execute_newserver_command(object *pl, char *command)
Player issued a command, let&#39;s handle it.
Definition: c_new.c:83
command_array_struct WizCommands[]
Wizard commands.
Definition: commands.c:202
#define tolower(C)
Simple macro to convert a letter to lowercase.
Definition: c_new.c:29
void command_fire(object *op, const char *params)
Player wants to start furing.
Definition: c_new.c:187
command_function func
Pointer to command function.
Definition: commands.h:39
Represents one command.
Definition: commands.h:37
uint32_t fire_on
Player should fire object, not move.
Definition: player.h:127
const int CommandsSize
Length of Commands array.
Definition: commands.c:126
Only for debugging purposes.
Definition: logger.h:13
float time
How long it takes to execute this command.
Definition: commands.h:40
command_array_struct CommunicationCommands[]
Chat/shout related commands.
Definition: commands.c:129
#define NDI_UNIQUE
Print immediately, don&#39;t buffer.
Definition: newclient.h:245
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.c:51
static command_array_struct * find_command_element(const char *cmd, command_array_struct *commarray, int commsize)
Finds the specified command in the command array.
Definition: c_new.c:62
void command_fire_stop(object *op, const char *params)
Player wants to stop firing.
Definition: c_new.c:208