]> git.pond.sub.org Git - empserver/blob - src/lib/player/empdis.c
Break inclusion cycle: prototypes.h and commands.h included each
[empserver] / src / lib / player / empdis.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
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.
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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  empdis.c: Empire dispatcher stuff
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 2000
33  *     Markus Armbruster, 2006
34  */
35
36 #include <config.h>
37
38 #include <stdio.h>
39 #include "com.h"
40 #include "empio.h"
41 #include "file.h"
42 #include "match.h"
43 #include "misc.h"
44 #include "nat.h"
45 #include "optlist.h"
46 #include "player.h"
47 #include "proto.h"
48 #include "prototypes.h"
49 #include "tel.h"
50
51 #include <fcntl.h>
52 #include <time.h>
53 #if !defined(_WIN32)
54 #include <unistd.h>
55 #endif
56 #include <signal.h>
57
58 #define KEEP_COMMANDS 50
59
60 /* ring buffer of most recent command prompts and commands, user text */
61 static char player_commands[KEEP_COMMANDS][1024 + 8];
62
63 /* the slot holding the most recent command in player_commands[] */
64 static int player_commands_index = 0;
65
66 static void disable_coms(void);
67
68 /*
69  * Get a command from the current player into COMBUFP[1024], in UTF-8.
70  * This may block for input, yielding the processor.  Flush buffered
71  * output when blocking, to make sure player sees the prompt.
72  * Return command's byte length on success, -1 on error.
73  */
74 int
75 getcommand(char *combufp)
76 {
77     struct natstr *natp = getnatp(player->cnum);
78     char buf[1024];             /* user text */
79
80     if (++player_commands_index >= KEEP_COMMANDS)
81         player_commands_index = 0;
82     sprintf(player_commands[player_commands_index], "%3d %3d [prompt]",
83             player_commands_index, player->cnum);
84
85     do {
86         prprompt(natp->nat_minused, natp->nat_btu);
87         buf[0] = 0;
88         if (recvclient(buf, 1024) < 0) {
89             return -1;
90         }
91     } while (buf[0] == 0);
92
93     if (++player_commands_index >= KEEP_COMMANDS)
94         player_commands_index = 0;
95     sprintf(player_commands[player_commands_index], "%3d %3d %s",
96             player_commands_index, player->cnum, buf);
97
98     if (player->flags & PF_UTF8)
99         return copy_utf8_no_funny(combufp, buf);
100     return copy_ascii_no_funny(combufp, buf);
101 }
102
103 void
104 init_player_commands(void)
105 {
106     int i;
107
108     for (i = 0; i < KEEP_COMMANDS; ++i)
109         *player_commands[i] = 0;
110
111     disable_coms();
112 }
113
114 void
115 log_last_commands(void)
116 {
117     int i;
118
119     logerror("Most recent player commands:");
120     for (i = player_commands_index; i >= 0; --i)
121         if (*player_commands[i])
122             logerror("%s", player_commands[i] + 4);
123     for (i = KEEP_COMMANDS - 1; i > player_commands_index; --i)
124         if (*player_commands[i])
125             logerror("%s", player_commands[i] + 4);
126 }
127
128 int
129 explain(void)
130 {
131     char *format;
132     int i;
133
134     pr("\t\tCurrent EMPIRE Command List\n"
135        "\t\t------- ------ ------- ----\n"
136        "Initial number is cost in B.T.U. units.\n"
137        "Next 2 chars (if present) are:\n"
138        "$ - must be non-broke\tc -- must have capital\n"
139        "Args in [brackets] are optional.\n"
140        "All-caps args in <angle brackets>"
141        " have the following meanings:\n"
142        "  <NUM> :: a number in unspecified units\n"
143        "  <COMM> :: a commodity such as `food', `guns', etc\n"
144        "  <VAR> :: a commodity such as `food', `guns', etc\n"
145        "  <TYPE> :: an item type such as `ship', `plane', etc\n");
146     for (i = 0; (format = player_coms[i].c_form) != 0; i++) {
147         if ((player_coms[i].c_permit & player->ncomstat) ==
148             player_coms[i].c_permit) {
149             pr("%2d ", player_coms[i].c_cost);
150             if ((player_coms[i].c_permit & MONEY) == MONEY)
151                 pr("$");
152             else
153                 pr(" ");
154             if ((player_coms[i].c_permit & CAP) == CAP)
155                 pr("c");
156             else
157                 pr(" ");
158             pr(" %s\n", format);
159         }
160     }
161     pr("For further info on command syntax see \"info Syntax\".\n");
162     return RET_OK;
163 }
164
165 static void
166 disable_coms(void)
167 {
168     char *tmp = strdup(disabled_commands);
169     char *name;
170     int cmd;
171
172     for (name = strtok(tmp, " \t"); name; name = strtok(NULL, " \t")) {
173         cmd = comtch(name, player_coms, -1);
174         if (cmd < 0) {
175             logerror("Warning: not disabling %s command %s\n",
176                     cmd == M_NOTUNIQUE ? "ambiguous" : "unknown", name);
177             continue;
178         }
179         player_coms[cmd].c_permit |= GOD;
180     }
181
182     free(tmp);
183 }
184
185 /*
186  * returns true if down
187  */
188 int
189 gamedown(void)
190 {
191     FILE *down_fp;
192     struct telstr tgm;
193     char buf[MAXTELSIZE + 1];   /* UTF-8 */
194
195     if (player->god)
196         return 0;
197     if ((down_fp = fopen(downfil, "rb")) == NULL)
198         return 0;
199     if (fread(&tgm, sizeof(tgm), 1, down_fp) != 1) {
200         logerror("bad header on login message (downfil)");
201         fclose(down_fp);
202         return 1;
203     }
204     if (tgm.tel_length >= (long)sizeof(buf)) {
205         logerror("text length (%ld) is too long for login message (downfil)", tgm.tel_length);
206         fclose(down_fp);
207         return 1;
208     }
209     if (fread(buf, tgm.tel_length, 1, down_fp) != 1) {
210         logerror("bad length %ld on login message", tgm.tel_length);
211         fclose(down_fp);
212         return 1;
213     }
214     buf[tgm.tel_length] = 0;
215     uprnf(buf);
216     pr("\nThe game is down\n");
217     fclose(down_fp);
218     return 1;
219 }
220
221 void
222 daychange(time_t now)
223 {
224     struct natstr *natp;
225     struct tm *tm;
226
227     natp = getnatp(player->cnum);
228     tm = localtime(&now);
229     if ((tm->tm_yday % 128) != natp->nat_dayno) {
230         natp->nat_dayno = tm->tm_yday % 128;
231         natp->nat_minused = 0;
232     }
233 }
234
235 int
236 getminleft(time_t now, int mpd)
237 {
238     struct tm *tm;
239     int nminleft;
240     struct natstr *natp;
241     int n;
242
243     tm = localtime(&now);
244     natp = getnatp(player->cnum);
245     nminleft = mpd - natp->nat_minused;
246     n = 60 * 24 - (tm->tm_min + tm->tm_hour * 60);
247     if (n < nminleft)
248         nminleft = n;
249     return nminleft;
250 }