]> git.pond.sub.org Git - empserver/blob - src/lib/player/empdis.c
(kw_read, kw_find, kwtab, kw_list): Were used to evaluate the hours
[empserver] / src / lib / player / empdis.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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  */
34
35 #include "prototypes.h"
36 #include <stdio.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "nat.h"
40 #include "tel.h"
41 #include "proto.h"
42 #include "com.h"
43 #include "keyword.h"
44 #include "file.h"
45 #include "empio.h"
46 #include "subs.h"
47 #include "common.h"
48 #include "optlist.h"
49
50 #include <fcntl.h>
51 #include <time.h>
52 #if !defined(_WIN32)
53 #include <unistd.h>
54 #endif
55 #include <signal.h>
56
57 #define KEEP_COMMANDS 50
58 s_char player_commands[KEEP_COMMANDS][1024 + 8];
59 int player_commands_index = 0;
60
61 int
62 getcommand(s_char *combufp)
63 {
64     struct natstr *natp;
65     s_char buf[1024];
66
67 /* Note this now assumes a 1024 byte buffer is being passed in */
68     natp = getnatp(player->cnum);
69     if (++player_commands_index >= KEEP_COMMANDS)
70         player_commands_index = 0;
71     sprintf(player_commands[player_commands_index], "%3d %3d [prompt]",
72             player_commands_index, player->cnum);
73     do {
74         prprompt(natp->nat_minused, natp->nat_btu);
75         buf[0] = 0;
76         if (recvclient(buf, 1024) < 0) {
77             return -1;
78         }
79     } while (buf[0] == 0);
80     if (++player_commands_index >= KEEP_COMMANDS)
81         player_commands_index = 0;
82     sprintf(player_commands[player_commands_index], "%3d %3d %s",
83             player_commands_index, player->cnum, buf);
84     strcpy(combufp, buf);
85     return (strlen(combufp));
86 }
87
88 void
89 init_player_commands(void)
90 {
91     int i;
92
93     for (i = 0; i < KEEP_COMMANDS; ++i)
94         *player_commands[i] = 0;
95 }
96
97 void
98 log_last_commands(void)
99 {
100     int i;
101
102     logerror("Most recent player commands:");
103     for (i = player_commands_index; i >= 0; --i)
104         if (*player_commands[i])
105             logerror("%s", player_commands[i] + 4);
106     for (i = KEEP_COMMANDS - 1; i > player_commands_index; --i)
107         if (*player_commands[i])
108             logerror("%s", player_commands[i] + 4);
109 }
110
111 int
112 explain(void)
113 {
114     register s_char *format;
115     register int i;
116
117     pr("\t\tCurrent EMPIRE Command List\n");
118     pr("\t\t------- ------ ------- ----\n");
119     pr("Initial number is cost in B.T.U. units.\n");
120     pr("Next 2 chars (if present) are:\n");
121     pr("$ - must be non-broke\tc -- must have capital\n");
122     pr("Args in [brackets] are optional.\n");
123     if (player->nstat > 4) {
124         pr("All-caps args in <angle brackets>");
125         pr(" have the following meanings:\n");
126         pr("  <NUM> :: a number in unspecified units\n");
127         pr("  <COMM> :: a commodity such as `food', `guns', etc\n");
128         pr("  <VAR> :: a commodity such as `food', `guns', etc\n");
129         pr("  <TYPE> :: an item type such as `ship', `plane', etc\n");
130     }
131     for (i = 0; (format = player_coms[i].c_form) != 0; i++) {
132         if ((player_coms[i].c_permit & player->ncomstat) ==
133             player_coms[i].c_permit) {
134             pr("%2d ", player_coms[i].c_cost);
135             if ((player_coms[i].c_permit & MONEY) == MONEY)
136                 pr("$");
137             else
138                 pr(" ");
139             if ((player_coms[i].c_permit & CAP) == CAP)
140                 pr("c");
141             else
142                 pr(" ");
143             pr(" %s\n", format);
144         }
145     }
146     pr("For further info on command syntax see \"info Syntax\".\n");
147     return RET_OK;
148 }
149
150 /*
151  * returns true if down
152  */
153 int
154 gamedown(void)
155 {
156     int downf;
157     struct telstr tgm;
158     s_char buf[1024];
159
160     if (player->god)
161         return 0;
162 #if !defined(_WIN32)
163     if ((downf = open(downfil, O_RDONLY, 0)) < 0)
164 #else
165     if ((downf = open(downfil, O_RDONLY | O_BINARY, 0)) < 0)
166 #endif
167         return 0;
168     if (read(downf, (s_char *)&tgm, sizeof(tgm)) != sizeof(tgm)) {
169         logerror("bad header on login message (downfil)");
170         close(downf);
171         return 1;
172     }
173     if (read(downf, buf, tgm.tel_length) != tgm.tel_length) {
174         logerror("bad length %ld on login message", tgm.tel_length);
175         close(downf);
176         return 1;
177     }
178     if (tgm.tel_length >= (long)sizeof(buf))
179         tgm.tel_length = sizeof(buf) - 1;
180     buf[tgm.tel_length] = 0;
181     pr(buf);
182     pr("\nThe game is down\n");
183     (void)close(downf);
184     return 1;
185 }
186
187 void
188 daychange(time_t now)
189 {
190     struct natstr *natp;
191     struct tm *tm;
192
193     natp = getnatp(player->cnum);
194     tm = localtime(&now);
195     if ((tm->tm_yday % 128) != natp->nat_dayno) {
196         natp->nat_dayno = tm->tm_yday % 128;
197         natp->nat_minused = 0;
198     }
199 }
200
201 int
202 getminleft(time_t now, int *hour, int *mpd)
203 {
204     s_char *bp;
205     struct tm *tm;
206     int nminleft;
207     int curtime;
208     struct natstr *natp;
209     int n;
210
211     tm = localtime(&now);
212     curtime = tm->tm_min + tm->tm_hour * 60;
213     natp = getnatp(player->cnum);
214     nminleft = *mpd - natp->nat_minused;
215     n = 60 * 24 - (tm->tm_min + tm->tm_hour * 60);
216     if (n < nminleft)
217         nminleft = n;
218     return nminleft;
219 }