2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * empdis.c: Empire dispatcher stuff
30 * Known contributors to this file:
33 * Markus Armbruster, 2006-2008
49 #include "prototypes.h"
53 #define KEEP_COMMANDS 50
55 /* ring buffer of most recent command prompts and commands, user text */
56 static char player_commands[KEEP_COMMANDS][1024 + 8];
58 /* the slot holding the most recent command in player_commands[] */
59 static int player_commands_index = 0;
61 static void disable_coms(void);
64 * Get a command from the current player into COMBUFP[1024], in UTF-8.
65 * This may block for input, yielding the processor. Flush buffered
66 * output when blocking, to make sure player sees the prompt.
67 * Return command's byte length on success, -1 on error.
70 getcommand(char *combufp)
72 struct natstr *natp = getnatp(player->cnum);
73 char buf[1024]; /* user text */
75 if (++player_commands_index >= KEEP_COMMANDS)
76 player_commands_index = 0;
77 sprintf(player_commands[player_commands_index], "%3d %3d [prompt]",
78 player_commands_index, player->cnum);
81 prprompt(natp->nat_minused, natp->nat_btu);
83 if (recvclient(buf, 1024) < 0) {
86 } while (buf[0] == 0);
88 if (++player_commands_index >= KEEP_COMMANDS)
89 player_commands_index = 0;
90 sprintf(player_commands[player_commands_index], "%3d %3d %s",
91 player_commands_index, player->cnum, buf);
93 if (player->flags & PF_UTF8)
94 return copy_utf8_no_funny(combufp, buf);
95 return copy_ascii_no_funny(combufp, buf);
99 init_player_commands(void)
103 for (i = 0; i < KEEP_COMMANDS; ++i)
104 *player_commands[i] = 0;
110 log_last_commands(void)
114 logerror("Most recent player commands:");
115 for (i = player_commands_index; i >= 0; --i)
116 if (*player_commands[i])
117 logerror("%s", player_commands[i] + 4);
118 for (i = KEEP_COMMANDS - 1; i > player_commands_index; --i)
119 if (*player_commands[i])
120 logerror("%s", player_commands[i] + 4);
129 pr("\t\tCurrent EMPIRE Command List\n"
130 "\t\t------- ------ ------- ----\n"
131 "Initial number is cost in B.T.U. units.\n"
132 "Next 2 chars (if present) are:\n"
133 "$ - must be non-broke\tc -- must have capital\n"
134 "Args in [brackets] are optional.\n"
135 "All-caps args in <angle brackets>"
136 " have the following meanings:\n"
137 /* FIXME incomplete */
138 " <NUM> :: a number in unspecified units\n"
139 " <COMM> :: a commodity such as `food', `guns', etc\n"
140 " <TYPE> :: an item type such as `ship', `plane', etc\n");
141 for (i = 0; (format = player_coms[i].c_form) != 0; i++) {
142 if ((player_coms[i].c_permit & player->nstat)
143 == player_coms[i].c_permit) {
144 pr("%2d ", player_coms[i].c_cost);
145 if ((player_coms[i].c_permit & MONEY) == MONEY)
149 if ((player_coms[i].c_permit & CAP) == CAP)
156 pr("For further info on command syntax see \"info Syntax\".\n");
163 char *tmp = strdup(disabled_commands);
167 for (name = strtok(tmp, " \t"); name; name = strtok(NULL, " \t")) {
168 cmd = comtch(name, player_coms, -1);
170 logerror("Warning: not disabling %s command %s\n",
171 cmd == M_NOTUNIQUE ? "ambiguous" : "unknown", name);
174 player_coms[cmd].c_permit |= GOD;
181 * returns true if down
188 char buf[MAXTELSIZE + 1]; /* UTF-8 */
192 if ((down_fp = fopen(downfil, "rb")) == NULL)
194 if (fread(&tgm, sizeof(tgm), 1, down_fp) != 1) {
195 logerror("bad header on login message (downfil)");
199 if (tgm.tel_length >= (long)sizeof(buf)) {
200 logerror("text length (%ld) is too long for login message (downfil)", tgm.tel_length);
204 if (fread(buf, tgm.tel_length, 1, down_fp) != 1) {
205 logerror("bad length %ld on login message", tgm.tel_length);
209 buf[tgm.tel_length] = 0;
211 pr("\nThe game is down\n");
217 daychange(time_t now)
222 natp = getnatp(player->cnum);
223 tm = localtime(&now);
224 if ((tm->tm_yday % 128) != natp->nat_dayno) {
225 natp->nat_dayno = tm->tm_yday % 128;
226 natp->nat_minused = 0;
231 getminleft(time_t now, int mpd)
238 tm = localtime(&now);
239 natp = getnatp(player->cnum);
240 nminleft = mpd - natp->nat_minused;
241 n = 60 * 24 - (tm->tm_min + tm->tm_hour * 60);