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