]> 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-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  news.c: Show current Empire news
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2006-2016
31  */
32
33 #include <config.h>
34
35 #include "chance.h"
36 #include "commands.h"
37 #include "news.h"
38 #include "optlist.h"
39
40 static void preport(struct nwsstr *np);
41 static int sectwon_cmp(const void *p, const void *q);
42
43 struct sectwon {
44     natid ano, vno;
45     unsigned short num;
46 };
47
48 int
49 news(void)
50 {
51     struct natstr *natp;
52     time_t now;
53     int heading, 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     unsigned short sectors_taken[MAXNOC][MAXNOC];
60     natid i, j;
61     int k, n, diff;
62     struct sectwon *sectwon;
63     char num[128];
64     char *verb;
65
66     if (!snxtitem(&nstr, EF_NEWS, "*", NULL))
67         return RET_SYN;
68     memset(sectors_taken, 0, sizeof(sectors_taken));
69     (void)time(&now);
70     natp = getnatp(player->cnum);
71     then = natp->nat_newstim;
72     if (player->argp[1]) {
73         /*
74          * We want to hide events before contact.  Proper solution
75          * would be to timestamp the contact.  Cheesy approximation:
76          * disable old news.
77          */
78         if (opt_HIDDEN && !player->god) {
79             pr("Sorry, argument doesn't work with HIDDEN enabled\n");
80             return RET_FAIL;
81         }
82         delta = days(atoi(player->argp[1]));
83         then = now - delta;
84     }
85     natp->nat_newstim = now;
86     head();
87     pr("\nThe details of Empire news since %s", ctime(&then));
88
89     heading = 0;
90     memset(page_has_news, 0, sizeof(page_has_news));
91     page_has_news[0] = 1;
92
93     for (page = 0; page <= N_MAX_PAGE; page++) {
94         if (!page_has_news[page])
95             continue;
96         snxtitem_rewind(&nstr);
97         while (nxtitem(&nstr, &nws)) {
98             if (CANT_HAPPEN(nws.nws_vrb > N_MAX_VERB))
99                 continue;
100             if (nws.nws_when < then)
101                 continue;
102             if (CANT_HAPPEN(nws.nws_ntm <= 0))
103                 nws.nws_ntm = 1;
104             if (opt_HIDDEN) {
105                 if (!player->god &&
106                     !(in_contact(player->cnum, nws.nws_ano) &&
107                       in_contact(player->cnum, nws.nws_vno)))
108                     continue;
109             }
110             page_has_news[rpt[nws.nws_vrb].r_newspage] = 1;
111             if (rpt[nws.nws_vrb].r_newspage != page)
112                 continue;
113             if (heading != page) {
114                 pr("\n\t ===  %s  ===\n", page_headings[page].name);
115                 heading = page;
116             }
117             if (nws.nws_vrb == N_WON_SECT ||
118                 nws.nws_vrb == N_AWON_SECT ||
119                 nws.nws_vrb == N_PWON_SECT)
120                 sectors_taken[nws.nws_ano][nws.nws_vno] += nws.nws_ntm;
121             preport(&nws);
122         }
123     }
124
125     if (!heading) {
126         pr("\nNo news at the moment...\n");
127         return RET_OK;
128     }
129
130     n = 0;
131     for (i = 0; i < MAXNOC; ++i) {
132         for (j = 0; j < i; ++j)
133             n += !!(sectors_taken[i][j] - sectors_taken[j][i]);
134     }
135     sectwon = malloc(sizeof(*sectwon) * n);
136
137     n = 0;
138     for (i = 0; i < MAXNOC; ++i) {
139         for (j = 0; j < i; ++j) {
140             diff = sectors_taken[i][j] - sectors_taken[j][i];
141             if (diff > 0) {
142                 sectwon[n].ano = i;
143                 sectwon[n].vno = j;
144                 sectwon[n].num = diff;
145                 n++;
146             } else if (diff < 0) {
147                 sectwon[n].ano = j;
148                 sectwon[n].vno = i;
149                 sectwon[n].num = -diff;
150                 n++;
151             }
152         }
153     }
154
155     qsort(sectwon, n, sizeof(*sectwon), sectwon_cmp);
156
157     if (n) {
158         pr("\n\t ===  The Bottom Line   ==\n");
159         for (k = 0; k < n; k++) {
160             if (sectwon[k].num == 1)
161                 verb = "stole";
162             else if (sectwon[k].num < 4)
163                 verb = "took";
164             else if (sectwon[k].num < 8)
165                 verb = "captured";
166             else
167                 verb = "seized";
168             numstr(num, sectwon[k].num);
169             pr("%s %s %s sector%s from %s\n",
170                cname(sectwon[k].ano), verb, num, splur(sectwon[k].num),
171                cname(sectwon[k].vno));
172         }
173     }
174
175     free(sectwon);
176     return RET_OK;
177 }
178
179 static void
180 preport(struct nwsstr *np)
181 {
182     char *cp;
183     int i;
184     char buf[255];
185     char num[128];
186     char *ptr;
187
188     cp = buf;
189     sprintf(buf, "%-16.16s  ", ctime(&np->nws_when));
190     cp += strlen(cp);
191     ptr = numstr(num, np->nws_ntm);
192     /*
193      * vary the order of the printing of "%d times "
194      */
195     if (roll0(4) == 0 && np->nws_ntm > 1) {
196         sprintf(cp, "%s times ", ptr);
197         cp += strlen(cp);
198         np->nws_ntm = 1;
199     }
200     strcpy(cp, cname(np->nws_ano));
201     cp += strlen(cp);
202     *cp++ = ' ';
203     sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[roll0(NUM_RPTS)],
204             cname(np->nws_vno));
205     cp += strlen(cp);
206     if (np->nws_ntm != 1) {
207         sprintf(cp, " %s times", ptr);
208         cp += strlen(cp);
209     }
210     if (cp - buf > 80) {
211         for (i = 80; --i > 60;)
212             if (buf[i] == ' ')
213                 break;
214         buf[i] = '\0';
215         pr("%s\n\t\t  %s\n", buf, &buf[i + 1]);
216     } else {
217         pr("%s\n", buf);
218     }
219     np->nws_ntm = 0;
220     return;
221 }
222
223 static int
224 sectwon_cmp(const void *p, const void *q)
225 {
226     const struct sectwon *a = p, *b = q;
227     int cmp;
228
229     cmp = b->num - a->num;
230     if (cmp)
231        return cmp;
232     cmp = b->ano - a->ano;
233     if (cmp)
234        return cmp;
235     return b->vno - a->vno;
236 }