]> git.pond.sub.org Git - empserver/blob - src/lib/commands/news.c
e8301152b3287e73e57f30ee909f39704678c0c4
[empserver] / src / lib / commands / news.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future 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 <config.h>
35
36 #include "commands.h"
37 #include "news.h"
38 #include "optlist.h"
39
40 static void preport(struct nwsstr *np);
41
42 int
43 news(void)
44 {
45     struct natstr *natp;
46     time_t now;
47     int page;
48     time_t then;
49     time_t delta;
50     struct nwsstr nws;
51     struct nstr_item nstr;
52     int page_has_news[N_MAX_PAGE + 1];
53     int there_is_news = 0;
54     short sectors_taken[MAXNOC][MAXNOC];
55     short sectors_delta;
56     short max_delta = -1;
57     short abs_delta;
58     short k;
59     int sectors_were_taken = 0;
60     natid i, j;
61     char num[128];
62     char *verb;
63
64     if (!snxtitem(&nstr, EF_NEWS, "*", NULL))
65         return RET_SYN;
66     memset(page_has_news, 0, sizeof(page_has_news));
67     memset(sectors_taken, 0, sizeof(sectors_taken));
68     (void)time(&now);
69     natp = getnatp(player->cnum);
70     then = natp->nat_newstim;
71     if (player->argp[1]) {
72         /*
73          * We want to hide events before contact.  Proper solution
74          * would be to timestamp the contact.  Cheesy approximation:
75          * disable old news.
76          */
77         if (opt_HIDDEN && !player->god) {
78             pr("Sorry, argument doesn't work with HIDDEN enabled\n");
79             return RET_FAIL;
80         }
81         delta = days(atoi(player->argp[1]));
82         then = now - delta;
83     }
84     natp->nat_newstim = now;
85     head();
86     pr("\nThe details of Empire news since %s", ctime(&then));
87     while (nxtitem(&nstr, &nws)) {
88         if (!nws.nws_vrb || CANT_HAPPEN(nws.nws_vrb > N_MAX_VERB))
89             continue;
90         if (nws.nws_when < then)
91             continue;
92         if (opt_HIDDEN) {
93             if (!player->god &&
94                 !(getcontact(getnatp(player->cnum), nws.nws_ano) &&
95                   getcontact(getnatp(player->cnum), nws.nws_vno)))
96                 continue;
97         }
98         ++page_has_news[rpt[(int)nws.nws_vrb].r_newspage];
99         ++there_is_news;
100     }
101     for (page = 1; page <= N_MAX_PAGE; page++) {
102         if (!page_has_news[page])
103             continue;
104         pr("\n\t ===  %s  ===\n", page_headings[page].name);
105         snxtitem_rewind(&nstr);
106         while (nxtitem(&nstr, &nws)) {
107             if (CANT_HAPPEN(nws.nws_vrb > N_MAX_VERB))
108                 continue;
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(struct nwsstr *np)
176 {
177     char *cp;
178     int i;
179     char buf[255];
180     char num[128];
181     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     sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[random() % NUM_RPTS],
199             cname(np->nws_vno));
200     cp += strlen(cp);
201     if (np->nws_ntm != 1) {
202         sprintf(cp, " %s times", ptr);
203         cp += strlen(cp);
204     }
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 }