]> git.pond.sub.org Git - empserver/blob - src/lib/commands/news.c
Import of Empire 4.2.12
[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         extern  s_char *page_headings[];
51         struct  natstr *natp;
52         time_t  now;
53         int     page;
54         time_t  then;
55         time_t  delta;
56         struct  nwsstr nws;
57         struct  nstr_item nstr;
58         int     page_has_news[N_MAX_PAGE+1];
59         int     there_is_news = 0;
60         short   sectors_taken[MAXNOC][MAXNOC];
61         short   sectors_delta;
62         short   max_delta = -1;
63         short   abs_delta;
64         short   k;
65         int     sectors_were_taken = 0;
66         natid   i,j;
67         s_char  num[128];
68         s_char  *verb;
69
70         bzero((s_char *)page_has_news, sizeof(page_has_news));
71         bzero((s_char *)sectors_taken, sizeof(sectors_taken));
72         (void) head();
73         (void) time(&now);
74         natp = getnatp(player->cnum);
75         then = natp->nat_newstim;
76         if (!opt_HIDDEN) {
77             if (player->argp[1] != 0 && isdigit(*player->argp[1])) {
78                 delta = days(atoi(player->argp[1]));
79                 then = now - delta;
80             }
81         }
82         natp->nat_newstim = now;
83 /*      if (then < now - days(3))
84                 then = now - days(3);
85 */
86         snxtitem(&nstr, EF_NEWS, "*");
87         pr("\nThe details of Empire news since %s", ctime(&then));
88         while (nxtitem(&nstr, (s_char *)&nws)) {
89                 if (nws.nws_when < then)
90                         continue;
91                 if (opt_HIDDEN) {
92                     if (!player->god &&
93                         !(getcontact(getnatp(player->cnum), nws.nws_ano) &&
94                           getcontact(getnatp(player->cnum), nws.nws_vno)))
95                         continue;
96                 }
97                 ++page_has_news[rpt[(int)nws.nws_vrb].r_newspage];
98                 ++there_is_news;
99         }
100         for (page = 1; page <= N_MAX_PAGE; page++) {
101                 if (!page_has_news[page])
102                         continue;
103                 pr("\n\t ===  %s  ===\n", page_headings[page]);
104                 snxtitem_rewind(&nstr);
105                 while (nxtitem(&nstr, (s_char *)&nws)) {
106                         if (rpt[(int)nws.nws_vrb].r_newspage != page)
107                                 continue;
108                         if (nws.nws_when < then)
109                                 continue;
110                         if (nws.nws_ntm == 0)
111                                 nws.nws_ntm = 1;
112                         if (opt_HIDDEN) {
113                             if (!player->god &&
114                                 !(getcontact(getnatp(player->cnum), nws.nws_ano) &&
115                                   getcontact(getnatp(player->cnum), nws.nws_vno)))
116                                 continue;
117                         }
118                         if (page == N_FRONT &&
119                             (nws.nws_vrb == N_WON_SECT ||
120                              nws.nws_vrb == N_AWON_SECT ||
121                              nws.nws_vrb == N_PWON_SECT)) {
122                                 sectors_taken[nws.nws_ano][nws.nws_vno] += nws.nws_ntm;
123                                 sectors_were_taken += nws.nws_ntm;
124                         }
125                         preport(&nws);
126                 }
127         }
128         if (sectors_were_taken) {
129                 for (i = 0; i < MAXNOC; ++i) {
130                         for (j = 0; j < i; ++j) {
131                                 sectors_delta = sectors_taken[i][j] -
132                                                 sectors_taken[j][i];
133                                 if (max_delta < abs(sectors_delta))
134                                         max_delta = abs(sectors_delta);
135                         }
136                 }
137                 pr("\n\t ===  The Bottom Line   ==\n");
138                 for (k = max_delta; k > 0; --k) {
139                         for (i = 0; i < MAXNOC; ++i) {
140                                 for (j = 0; j < i; ++j) {
141                                         sectors_delta = sectors_taken[i][j] -
142                                                 sectors_taken[j][i];
143                                         abs_delta = abs(sectors_delta);
144                                         if (abs_delta != k)
145                                                 continue;
146                                         if (abs_delta == 1)
147                                                 verb = "stole";
148                                         else if (abs_delta < 4)
149                                                 verb = "took";
150                                         else if (abs_delta < 8)
151                                                 verb = "captured";
152                                         else
153                                                 verb = "seized";
154                                         if (sectors_delta > 0) {
155                                                 numstr(num, abs_delta);
156                                                 pr("%s %s %s sector%s from %s\n", cname(i), verb, num, splur(sectors_delta), cname(j));
157                                         } else if (sectors_delta < 0) {
158                                                 numstr(num, abs_delta);
159                                                 pr("%s %s %s sector%s from %s\n", cname(j), verb, 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] == ' ') break;
208                 buf[i] = '\0';
209                 pr("%s\n\t\t  %s\n", buf, &buf[i+1]);
210         } else {
211                 pr("%s\n", buf);
212         }
213         np->nws_ntm = 0;
214         return;
215 }