]> git.pond.sub.org Git - empserver/blob - src/lib/player/dispatch.c
5d8f10dfee7c4c1dc01368389dbaf5744c1406c6
[empserver] / src / lib / player / dispatch.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  dispatch.c: Actually execute the command given
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1994
31  *     Steve McClure, 1998
32  *     Markus Armbruster, 2007-2014
33  */
34
35 #include <config.h>
36
37 #include "chance.h"
38 #include "com.h"
39 #include "empio.h"
40 #include "file.h"
41 #include "journal.h"
42 #include "match.h"
43 #include "misc.h"
44 #include "nat.h"
45 #include "optlist.h"
46 #include "player.h"
47 #include "prototypes.h"
48 #include "server.h"
49
50 /*
51  * Last command's PRNG seed.
52  * Only used when running_test_suite.
53  */
54 int test_suite_prng_seed;
55
56 /*
57  * Execute command named by player->argp[0].
58  * @buf is the raw UTF-8 command line.  It should have been passed to
59  * parse() to set up player->argp.
60  * If @redir is not null, it's the command's redirection, in UTF-8.
61  * Return -1 if the command is not unique or doesn't exist, else 0.
62  */
63 int
64 dispatch(char *buf, char *redir)
65 {
66     struct natstr *np;
67     struct cmndstr *command;
68     int cmd;
69
70     cmd = comtch(player->argp[0], player_coms, player->nstat);
71     if (cmd < 0) {
72         if (cmd == M_NOTUNIQUE)
73             pr("Command \"%s\" is ambiguous -- ", player->argp[0]);
74         else if (cmd == M_IGNORE)
75             return 0;
76         else
77             pr("\"%s\" is not a legal command\n", player->argp[0]);
78         return -1;
79     }
80     command = &player_coms[cmd];
81
82     np = getnatp(player->cnum);
83     if (np->nat_btu < command->c_cost && command->c_cost > 0) {
84         if (player->god || opt_BLITZ)
85             np->nat_btu = max_btus;
86         else {
87             pr("You don't have the BTU's, bozo\n");
88             return 0;
89         }
90     }
91     if (!command->c_addr) {
92         pr("Command not implemented\n");
93         return 0;
94     }
95     player->may_sleep = command->c_flags & C_MOD
96         ? PLAYER_SLEEP_ON_INPUT : PLAYER_SLEEP_FREELY;
97     player->command = command;
98     empth_rwlock_rdlock(update_lock);
99
100     /*
101      * When running the test suite, reseed PRNG for each command with
102      * a counter, to keep results stable even when the number of PRNs
103      * consumed changes.
104      * Tests can adjust the counter with "__cmd added ...", to
105      * keep the results stable when commands are inserted or deleted.
106      */
107     test_suite_prng_seed += !(command->c_permit & TESTING);
108     if (running_test_suite)
109         seed_prng(test_suite_prng_seed);
110
111     if (redir) {
112         prredir(redir);
113         uprnf(buf);
114         pr("\n");
115     }
116     journal_command(command->c_form);
117     switch (command->c_addr()) {
118     case RET_OK:
119         player->btused += command->c_cost;
120         break;
121     case RET_FAIL:
122         pr("command failed\n");
123         player->btused += command->c_cost;
124         break;
125     case RET_SYN:
126         pr("Usage: %s\n", command->c_form);
127         break;
128     default:
129         CANT_REACH();
130         break;
131     }
132
133     empth_rwlock_unlock(update_lock);
134     player->command = NULL;
135     if (player->may_sleep != PLAYER_SLEEP_NEVER || !io_eof(player->iop))
136         player->may_sleep = PLAYER_SLEEP_FREELY;
137     /* else we're being kicked out */
138     return 0;
139 }