]> git.pond.sub.org Git - empserver/blob - src/lib/commands/news.c
Properly declare stuff in headers; remove redundant declarations.
[empserver] / src / lib / commands / news.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  *  news.c: Show current Empire news
29  * 
30  *  Known contributors to this file:
31  *     
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "nat.h"
37 #include "news.h"
38 #include "file.h"
39 #include "xy.h"
40 #include "nsc.h"
41 #include "deity.h"
42 #include "commands.h"
43 #include "optlist.h"
44
45 static void preport(register struct nwsstr *np);
46
47 int
48 news(void)
49 {
50     struct natstr *natp;
51     time_t now;
52     int page;
53     time_t then;
54     time_t delta;
55     struct nwsstr nws;
56     struct nstr_item nstr;
57     int page_has_news[N_MAX_PAGE + 1];
58     int there_is_news = 0;
59     short sectors_taken[MAXNOC][MAXNOC];
60     short sectors_delta;
61     short max_delta = -1;
62     short abs_delta;
63     short k;
64     int sectors_were_taken = 0;
65     natid i, j;
66     s_char num[128];
67     s_char *verb;
68
69     memset(page_has_news, 0, sizeof(page_has_news));
70     memset(sectors_taken, 0, sizeof(sectors_taken));
71     (void)head();
72     (void)time(&now);
73     natp = getnatp(player->cnum);
74     then = natp->nat_newstim;
75     if (!opt_HIDDEN) {
76         if (player->argp[1] != 0 && isdigit(*player->argp[1])) {
77             delta = days(atoi(player->argp[1]));
78             then = now - delta;
79         }
80     }
81     natp->nat_newstim = now;
82 /*      if (then < now - days(3))
83                 then = now - days(3);
84 */
85     snxtitem(&nstr, EF_NEWS, "*");
86     pr("\nThe details of Empire news since %s", ctime(&then));
87     while (nxtitem(&nstr, (s_char *)&nws)) {
88         if (nws.nws_when < then)
89             continue;
90         if (opt_HIDDEN) {
91             if (!player->god &&
92                 !(getcontact(getnatp(player->cnum), nws.nws_ano) &&
93                   getcontact(getnatp(player->cnum), nws.nws_vno)))
94                 continue;
95         }
96         ++page_has_news[rpt[(int)nws.nws_vrb].r_newspage];
97         ++there_is_news;
98     }
99     for (page = 1; page <= N_MAX_PAGE; page++) {
100         if (!page_has_news[page])
101             continue;
102         pr("\n\t ===  %s  ===\n", page_headings[page]);
103         snxtitem_rewind(&nstr);
104         while (nxtitem(&nstr, (s_char *)&nws)) {
105             if (rpt[(int)nws.nws_vrb].r_newspage != page)
106                 continue;
107             if (nws.nws_when < then)
108                 continue;
109             if (nws.nws_ntm == 0)
110                 nws.nws_ntm = 1;
111             if (opt_HIDDEN) {
112                 if (!player->god &&
113                     !(getcontact(getnatp(player->cnum), nws.nws_ano) &&
114                       getcontact(getnatp(player->cnum), nws.nws_vno)))
115                     continue;
116             }
117             if (page == N_FRONT &&
118                 (nws.nws_vrb == N_WON_SECT ||
119                  nws.nws_vrb == N_AWON_SECT ||
120                  nws.nws_vrb == N_PWON_SECT)) {
121                 sectors_taken[nws.nws_ano][nws.nws_vno] += nws.nws_ntm;
122                 sectors_were_taken += nws.nws_ntm;
123             }
124             preport(&nws);
125         }
126     }
127     if (sectors_were_taken) {
128         for (i = 0; i < MAXNOC; ++i) {
129             for (j = 0; j < i; ++j) {
130                 sectors_delta = sectors_taken[i][j] - sectors_taken[j][i];
131                 if (max_delta < abs(sectors_delta))
132                     max_delta = abs(sectors_delta);
133             }
134         }
135         pr("\n\t ===  The Bottom Line   ==\n");
136         for (k = max_delta; k > 0; --k) {
137             for (i = 0; i < MAXNOC; ++i) {
138                 for (j = 0; j < i; ++j) {
139                     sectors_delta = sectors_taken[i][j] -
140                         sectors_taken[j][i];
141                     abs_delta = abs(sectors_delta);
142                     if (abs_delta != k)
143                         continue;
144                     if (abs_delta == 1)
145                         verb = "stole";
146                     else if (abs_delta < 4)
147                         verb = "took";
148                     else if (abs_delta < 8)
149                         verb = "captured";
150                     else
151                         verb = "seized";
152                     if (sectors_delta > 0) {
153                         numstr(num, abs_delta);
154                         pr("%s %s %s sector%s from %s\n", cname(i), verb,
155                            num, splur(sectors_delta), cname(j));
156                     } else if (sectors_delta < 0) {
157                         numstr(num, abs_delta);
158                         pr("%s %s %s sector%s from %s\n", cname(j), verb,
159                            num, splur(-sectors_delta), cname(i));
160                     }
161                 }
162             }
163         }
164     }
165     if (!there_is_news)
166         pr("\nNo news at the moment...\n");
167     return 0;
168 }
169
170 static void
171 preport(register struct nwsstr *np)
172 {
173     register s_char *cp;
174     register int i;
175     s_char buf[255];
176     s_char num[128];
177     s_char *ptr;
178
179     cp = buf;
180     sprintf(buf, "%-16.16s  ", ctime(&np->nws_when));
181     cp += strlen(cp);
182     ptr = numstr(num, np->nws_ntm);
183     /*
184      * vary the order of the printing of "%d times "
185      */
186     if ((random() & 3) == 0 && np->nws_ntm > 1) {
187         sprintf(cp, "%s times ", ptr);
188         cp += strlen(cp);
189         np->nws_ntm = 1;
190     }
191     strcpy(cp, cname(np->nws_ano));
192     cp += strlen(cp);
193     *cp++ = ' ';
194     if (np->nws_vrb < 1 || np->nws_vrb > N_MAX_VERB)
195         np->nws_vrb = 0;
196     sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[random() % NUM_RPTS],
197             cname(np->nws_vno));
198     cp += strlen(cp);
199     if (np->nws_ntm != 1) {
200         sprintf(cp, " %s times", ptr);
201         cp += strlen(cp);
202     }
203     if (*buf >= 'a' && *buf <= 'z')
204         *buf += 'A' - 'a';
205     if (cp - buf > 80) {
206         for (i = 80; --i > 60;)
207             if (buf[i] == ' ')
208                 break;
209         buf[i] = '\0';
210         pr("%s\n\t\t  %s\n", buf, &buf[i + 1]);
211     } else {
212         pr("%s\n", buf);
213     }
214     np->nws_ntm = 0;
215     return;
216 }