]> 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-2013, 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  *
31  */
32
33 #include <config.h>
34
35 #include "commands.h"
36 #include "news.h"
37 #include "optlist.h"
38
39 static void preport(struct nwsstr *np);
40
41 int
42 news(void)
43 {
44     struct natstr *natp;
45     time_t now;
46     int page;
47     time_t then;
48     time_t delta;
49     struct nwsstr nws;
50     struct nstr_item nstr;
51     int page_has_news[N_MAX_PAGE + 1];
52     int there_is_news = 0;
53     short sectors_taken[MAXNOC][MAXNOC];
54     short sectors_delta;
55     short max_delta = -1;
56     short abs_delta;
57     short k;
58     int sectors_were_taken = 0;
59     natid i, j;
60     char num[128];
61     char *verb;
62
63     if (!snxtitem(&nstr, EF_NEWS, "*", NULL))
64         return RET_SYN;
65     memset(page_has_news, 0, sizeof(page_has_news));
66     memset(sectors_taken, 0, sizeof(sectors_taken));
67     (void)time(&now);
68     natp = getnatp(player->cnum);
69     then = natp->nat_newstim;
70     if (player->argp[1]) {
71         /*
72          * We want to hide events before contact.  Proper solution
73          * would be to timestamp the contact.  Cheesy approximation:
74          * disable old news.
75          */
76         if (opt_HIDDEN && !player->god) {
77             pr("Sorry, argument doesn't work with HIDDEN enabled\n");
78             return RET_FAIL;
79         }
80         delta = days(atoi(player->argp[1]));
81         then = now - delta;
82     }
83     natp->nat_newstim = now;
84     head();
85     pr("\nThe details of Empire news since %s", ctime(&then));
86     while (nxtitem(&nstr, &nws)) {
87         if (!nws.nws_vrb || CANT_HAPPEN(nws.nws_vrb > N_MAX_VERB))
88             continue;
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].name);
104         snxtitem_rewind(&nstr);
105         while (nxtitem(&nstr, &nws)) {
106             if (CANT_HAPPEN(nws.nws_vrb > N_MAX_VERB))
107                 continue;
108             if (rpt[(int)nws.nws_vrb].r_newspage != page)
109                 continue;
110             if (nws.nws_when < then)
111                 continue;
112             if (nws.nws_ntm == 0)
113                 nws.nws_ntm = 1;
114             if (opt_HIDDEN) {
115                 if (!player->god &&
116                     !(getcontact(getnatp(player->cnum), nws.nws_ano) &&
117                       getcontact(getnatp(player->cnum), nws.nws_vno)))
118                     continue;
119             }
120             if (page == N_FRONT &&
121                 (nws.nws_vrb == N_WON_SECT ||
122                  nws.nws_vrb == N_AWON_SECT ||
123                  nws.nws_vrb == N_PWON_SECT)) {
124                 sectors_taken[nws.nws_ano][nws.nws_vno] += nws.nws_ntm;
125                 sectors_were_taken += nws.nws_ntm;
126             }
127             preport(&nws);
128         }
129     }
130     if (sectors_were_taken) {
131         for (i = 0; i < MAXNOC; ++i) {
132             for (j = 0; j < i; ++j) {
133                 sectors_delta = sectors_taken[i][j] - sectors_taken[j][i];
134                 if (max_delta < abs(sectors_delta))
135                     max_delta = abs(sectors_delta);
136             }
137         }
138         pr("\n\t ===  The Bottom Line   ==\n");
139         for (k = max_delta; k > 0; --k) {
140             for (i = 0; i < MAXNOC; ++i) {
141                 for (j = 0; j < i; ++j) {
142                     sectors_delta = sectors_taken[i][j] -
143                         sectors_taken[j][i];
144                     abs_delta = abs(sectors_delta);
145                     if (abs_delta != k)
146                         continue;
147                     if (abs_delta == 1)
148                         verb = "stole";
149                     else if (abs_delta < 4)
150                         verb = "took";
151                     else if (abs_delta < 8)
152                         verb = "captured";
153                     else
154                         verb = "seized";
155                     if (sectors_delta > 0) {
156                         numstr(num, abs_delta);
157                         pr("%s %s %s sector%s from %s\n", cname(i), verb,
158                            num, splur(sectors_delta), cname(j));
159                     } else if (sectors_delta < 0) {
160                         numstr(num, abs_delta);
161                         pr("%s %s %s sector%s from %s\n", cname(j), verb,
162                            num, splur(-sectors_delta), cname(i));
163                     }
164                 }
165             }
166         }
167     }
168     if (!there_is_news)
169         pr("\nNo news at the moment...\n");
170     return 0;
171 }
172
173 static void
174 preport(struct nwsstr *np)
175 {
176     char *cp;
177     int i;
178     char buf[255];
179     char num[128];
180     char *ptr;
181
182     cp = buf;
183     sprintf(buf, "%-16.16s  ", ctime(&np->nws_when));
184     cp += strlen(cp);
185     ptr = numstr(num, np->nws_ntm);
186     /*
187      * vary the order of the printing of "%d times "
188      */
189     if ((random() & 3) == 0 && np->nws_ntm > 1) {
190         sprintf(cp, "%s times ", ptr);
191         cp += strlen(cp);
192         np->nws_ntm = 1;
193     }
194     strcpy(cp, cname(np->nws_ano));
195     cp += strlen(cp);
196     *cp++ = ' ';
197     sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[random() % NUM_RPTS],
198             cname(np->nws_vno));
199     cp += strlen(cp);
200     if (np->nws_ntm != 1) {
201         sprintf(cp, " %s times", ptr);
202         cp += strlen(cp);
203     }
204     if (cp - buf > 80) {
205         for (i = 80; --i > 60;)
206             if (buf[i] == ' ')
207                 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 }