]> git.pond.sub.org Git - empserver/blob - src/lib/player/dispatch.c
Update copyright notice
[empserver] / src / lib / player / dispatch.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2020, 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 "journal.h"
41 #include "match.h"
42 #include "misc.h"
43 #include "nat.h"
44 #include "optlist.h"
45 #include "player.h"
46 #include "prototypes.h"
47 #include "server.h"
48
49 /*
50  * Last command's PRNG seed.
51  * Only used when running_test_suite.
52  */
53 int test_suite_prng_seed;
54
55 /*
56  * Execute command named by player->argp[0].
57  * @buf is the raw UTF-8 command line.  It should have been passed to
58  * parse() to set up player->argp.
59  * If @redir is not null, it's the command's redirection, in UTF-8.
60  * Return -1 if the command is not unique or doesn't exist, else 0.
61  */
62 int
63 dispatch(char *buf, char *redir)
64 {
65     struct natstr *np;
66     struct cmndstr *command;
67     int cmd;
68
69     cmd = comtch(player->argp[0], player_coms, player->nstat);
70     if (cmd < 0) {
71         if (cmd == M_NOTUNIQUE)
72             pr("Command \"%s\" is ambiguous -- ", player->argp[0]);
73         else if (cmd == M_IGNORE)
74             return 0;
75         else
76             pr("\"%s\" is not a legal command\n", player->argp[0]);
77         return -1;
78     }
79     command = &player_coms[cmd];
80
81     np = getnatp(player->cnum);
82     if (np->nat_btu < command->c_cost && command->c_cost > 0) {
83         if (player->god || opt_BLITZ)
84             np->nat_btu = max_btus;
85         else {
86             pr("You don't have the BTUs, bozo\n");
87             return 0;
88         }
89     }
90     if (!command->c_addr) {
91         pr("Command not implemented\n");
92         return 0;
93     }
94     player->may_sleep = command->c_flags & C_MOD
95         ? PLAYER_SLEEP_ON_INPUT : PLAYER_SLEEP_FREELY;
96     player->command = command;
97     empth_rwlock_rdlock(update_lock);
98
99     /*
100      * When running the test suite, reseed PRNG for each command with
101      * a counter, to keep results stable even when the number of PRNs
102      * consumed changes.
103      * Tests can adjust the counter with "__cmd added ...", to
104      * keep the results stable when commands are inserted or deleted.
105      */
106     test_suite_prng_seed += !(command->c_permit & TESTING);
107     if (running_test_suite)
108         seed_prng(test_suite_prng_seed);
109
110     if (redir) {
111         prredir(redir);
112         uprnf(buf);
113         pr("\n");
114     }
115     journal_command(command->c_form);
116     switch (command->c_addr()) {
117     case RET_OK:
118         player->btused += command->c_cost;
119         break;
120     case RET_FAIL:
121         pr("command failed\n");
122         player->btused += command->c_cost;
123         break;
124     case RET_SYN:
125         pr("Usage: %s\n", command->c_form);
126         break;
127     default:
128         CANT_REACH();
129         break;
130     }
131
132     empth_rwlock_unlock(update_lock);
133     player->command = NULL;
134     if (player->may_sleep != PLAYER_SLEEP_NEVER || !io_eof(player->iop))
135         player->may_sleep = PLAYER_SLEEP_FREELY;
136     /* else we're being kicked out */
137     return 0;
138 }