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