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