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