2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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-2009
34 * Ron Koenderink, 2004-2009
51 #include "prototypes.h"
54 #define KEEP_COMMANDS 50
56 /* ring buffer of most recent command prompts and commands, user text */
57 static char player_commands[KEEP_COMMANDS][1024 + 8];
59 /* the slot holding the most recent command in player_commands[] */
60 static int player_commands_index = 0;
62 static void disable_coms(void);
65 * Get a command from the current player into COMBUFP[1024], in UTF-8.
66 * This may block for input, yielding the processor. Flush buffered
67 * output when blocking, to make sure player sees the prompt.
68 * Return command's byte length on success, -1 on error.
71 getcommand(char *combufp)
73 struct natstr *natp = getnatp(player->cnum);
74 char buf[1024]; /* user text */
76 if (++player_commands_index >= KEEP_COMMANDS)
77 player_commands_index = 0;
78 sprintf(player_commands[player_commands_index], "%3d %3d [prompt]",
79 player_commands_index, player->cnum);
82 prprompt(natp->nat_timeused / 60, natp->nat_btu);
84 if (recvclient(buf, 1024) < 0) {
87 } while (buf[0] == 0);
89 if (++player_commands_index >= KEEP_COMMANDS)
90 player_commands_index = 0;
91 sprintf(player_commands[player_commands_index], "%3d %3d %s",
92 player_commands_index, player->cnum, buf);
94 if (player->flags & PF_UTF8)
95 return copy_utf8_no_funny(combufp, buf);
96 return copy_ascii_no_funny(combufp, buf);
100 init_player_commands(void)
104 for (i = 0; i < KEEP_COMMANDS; ++i)
105 *player_commands[i] = 0;
111 log_last_commands(void)
115 logerror("Most recent player commands:");
116 for (i = player_commands_index; i >= 0; --i)
117 if (*player_commands[i])
118 logerror("%s", player_commands[i] + 4);
119 for (i = KEEP_COMMANDS - 1; i > player_commands_index; --i)
120 if (*player_commands[i])
121 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 (com = player_coms; com->c_form; com++) {
142 if ((com->c_permit & player->nstat) == com->c_permit) {
143 pr("%2d ", com->c_cost);
144 if (com->c_permit & MONEY)
148 if (com->c_permit & CAP)
152 pr(" %s\n", com->c_form);
155 pr("For further info on command syntax see \"info Syntax\".\n");
162 char *tmp = strdup(disabled_commands);
166 for (name = strtok(tmp, " \t"); name; name = strtok(NULL, " \t")) {
167 cmd = comtch(name, player_coms, -1);
169 logerror("Warning: not disabling %s command %s\n",
170 cmd == M_NOTUNIQUE ? "ambiguous" : "unknown", name);
173 player_coms[cmd].c_permit |= GOD;
180 seconds_since_midnight(time_t time)
182 struct tm *tm = localtime(&time);
188 return time - mktime(tm);
192 update_timeused_login(time_t now)
194 struct natstr *natp = getnatp(player->cnum);
195 time_t midnight_secs = seconds_since_midnight(now);
197 if (now - natp->nat_last_logout > midnight_secs) {
198 natp->nat_timeused = 0;
201 player->lasttime = now;
205 update_timeused(time_t now)
207 struct natstr *natp = getnatp(player->cnum);
208 time_t midnight_secs = seconds_since_midnight(now);
209 time_t dt = now - player->lasttime;
211 if (dt > midnight_secs)
212 natp->nat_timeused = midnight_secs;
214 natp->nat_timeused += dt;
215 player->lasttime = now;
220 enforce_minimum_session_time(void)
222 struct natstr *natp = getnatp(player->cnum);
224 time_t dt = natp->nat_last_logout - natp->nat_last_login;
225 if (dt > seconds_since_midnight(natp->nat_last_logout))
226 dt = seconds_since_midnight(natp->nat_last_logout);
228 natp->nat_timeused += 15 - dt;
233 may_play_now(struct natstr *natp, time_t now)
235 if (CANT_HAPPEN(natp->nat_cnum != player->cnum))
238 if (gamehours(now)) {
239 if (player->flags & PF_HOURS) {
240 pr("\nEmpire hours restriction lifted\n");
241 player->flags &= ~PF_HOURS;
244 if (!(player->flags & PF_HOURS)) {
245 pr("\nEmpire hours restriction in force\n");
246 player->flags |= PF_HOURS;
248 if (natp->nat_stat != STAT_GOD)
252 if (game_play_disabled()) {
253 if (!(player->flags & PF_DOWN)) {
254 show_first_tel(downfil);
255 pr("\nThe game is down\n");
256 player->flags |= PF_DOWN;
258 if (natp->nat_stat != STAT_GOD)
261 player->flags &= ~PF_DOWN;
263 if ((natp->nat_stat != STAT_GOD && natp->nat_stat != STAT_VIS)
264 && natp->nat_timeused > m_m_p_d * 60) {
265 pr("Max minutes per day limit exceeded.\n");