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