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