2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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, see <http://www.gnu.org/licenses/>.
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.
27 * empdis.c: Empire dispatcher stuff
29 * Known contributors to this file:
32 * Markus Armbruster, 2006-2010
33 * Ron Koenderink, 2004-2009
50 #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);
80 prprompt(natp->nat_timeused / 60, natp->nat_btu);
81 if (recvclient(buf, sizeof(buf)) < 0)
84 if (++player_commands_index >= KEEP_COMMANDS)
85 player_commands_index = 0;
86 sprintf(player_commands[player_commands_index], "%3d %3d %s",
87 player_commands_index, player->cnum, buf);
89 if (player->flags & PF_UTF8)
90 return copy_utf8_no_funny(combufp, buf);
91 return copy_ascii_no_funny(combufp, buf);
95 init_player_commands(void)
99 for (i = 0; i < KEEP_COMMANDS; ++i)
100 *player_commands[i] = 0;
106 log_last_commands(void)
110 logerror("Most recent player commands:");
111 for (i = player_commands_index; i >= 0; --i)
112 if (*player_commands[i])
113 logerror("%s", player_commands[i] + 4);
114 for (i = KEEP_COMMANDS - 1; i > player_commands_index; --i)
115 if (*player_commands[i])
116 logerror("%s", player_commands[i] + 4);
124 pr("\t\tCurrent EMPIRE Command List\n"
125 "\t\t------- ------ ------- ----\n"
126 "Initial number is cost in B.T.U. units.\n"
127 "Next 2 chars (if present) are:\n"
128 "$ - must be non-broke\tc -- must have capital\n"
129 "Args in [brackets] are optional.\n"
130 "All-caps args in <angle brackets>"
131 " have the following meanings:\n"
132 /* FIXME incomplete */
133 " <NUM> :: a number in unspecified units\n"
134 " <COMM> :: a commodity such as `food', `guns', etc\n"
135 " <TYPE> :: an item type such as `ship', `plane', etc\n");
136 for (com = player_coms; com->c_form; com++) {
137 if ((com->c_permit & player->nstat) == com->c_permit) {
138 pr("%2d ", com->c_cost);
139 if (com->c_permit & MONEY)
143 if (com->c_permit & CAP)
147 pr(" %s\n", com->c_form);
150 pr("For further info on command syntax see \"info Syntax\".\n");
157 char *tmp = strdup(disabled_commands);
161 for (name = strtok(tmp, " \t"); name; name = strtok(NULL, " \t")) {
162 cmd = comtch(name, player_coms, -1);
164 logerror("Warning: not disabling %s command %s\n",
165 cmd == M_NOTUNIQUE ? "ambiguous" : "unknown", name);
168 player_coms[cmd].c_permit |= GOD;
175 seconds_since_midnight(time_t time)
177 struct tm *tm = localtime(&time);
183 return time - mktime(tm);
187 update_timeused_login(time_t now)
189 struct natstr *natp = getnatp(player->cnum);
190 time_t midnight_secs = seconds_since_midnight(now);
192 if (now - natp->nat_last_logout > midnight_secs) {
193 natp->nat_timeused = 0;
196 player->lasttime = now;
200 update_timeused(time_t now)
202 struct natstr *natp = getnatp(player->cnum);
203 time_t midnight_secs = seconds_since_midnight(now);
204 time_t dt = now - player->lasttime;
206 if (dt > midnight_secs)
207 natp->nat_timeused = midnight_secs;
209 natp->nat_timeused += dt;
210 player->lasttime = now;
215 enforce_minimum_session_time(void)
217 struct natstr *natp = getnatp(player->cnum);
219 time_t dt = natp->nat_last_logout - natp->nat_last_login;
220 if (dt > seconds_since_midnight(natp->nat_last_logout))
221 dt = seconds_since_midnight(natp->nat_last_logout);
223 natp->nat_timeused += 15 - dt;
228 may_play_now(struct natstr *natp, time_t now)
230 if (CANT_HAPPEN(natp->nat_cnum != player->cnum))
233 if (gamehours(now)) {
234 if (player->flags & PF_HOURS) {
235 pr("\nEmpire hours restriction lifted\n");
236 player->flags &= ~PF_HOURS;
239 if (!(player->flags & PF_HOURS)) {
240 pr("\nEmpire hours restriction in force\n");
241 player->flags |= PF_HOURS;
243 if (natp->nat_stat != STAT_GOD)
247 if (game_play_disabled()) {
248 if (!(player->flags & PF_DOWN)) {
249 show_first_tel(downfil);
250 pr("\nThe game is down\n");
251 player->flags |= PF_DOWN;
253 if (natp->nat_stat != STAT_GOD)
256 player->flags &= ~PF_DOWN;
258 if ((natp->nat_stat != STAT_GOD && natp->nat_stat != STAT_VIS)
259 && natp->nat_timeused > m_m_p_d * 60) {
260 pr("Max minutes per day limit exceeded.\n");