]> git.pond.sub.org Git - empserver/blob - src/lib/commands/news.c
e165ab2d3f5902266a65b08d48f2311f434cbb32
[empserver] / src / lib / commands / news.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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     if (!opt_HIDDEN) {
75         if (player->argp[1] != 0 && isdigit(*player->argp[1])) {
76             delta = days(atoi(player->argp[1]));
77             then = now - delta;
78         }
79     }
80     natp->nat_newstim = now;
81 /*      if (then < now - days(3))
82                 then = now - days(3);
83 */
84     snxtitem(&nstr, EF_NEWS, "*");
85     pr("\nThe details of Empire news since %s", ctime(&then));
86     while (nxtitem(&nstr, (s_char *)&nws)) {
87         if (nws.nws_when < then)
88             continue;
89         if (opt_HIDDEN) {
90             if (!player->god &&
91                 !(getcontact(getnatp(player->cnum), nws.nws_ano) &&
92                   getcontact(getnatp(player->cnum), nws.nws_vno)))
93                 continue;
94         }
95         ++page_has_news[rpt[(int)nws.nws_vrb].r_newspage];
96         ++there_is_news;
97     }
98     for (page = 1; page <= N_MAX_PAGE; page++) {
99         if (!page_has_news[page])
100             continue;
101         pr("\n\t ===  %s  ===\n", page_headings[page]);
102         snxtitem_rewind(&nstr);
103         while (nxtitem(&nstr, (s_char *)&nws)) {
104             if (rpt[(int)nws.nws_vrb].r_newspage != page)
105                 continue;
106             if (nws.nws_when < then)
107                 continue;
108             if (nws.nws_ntm == 0)
109                 nws.nws_ntm = 1;
110             if (opt_HIDDEN) {
111                 if (!player->god &&
112                     !(getcontact(getnatp(player->cnum), nws.nws_ano) &&
113                       getcontact(getnatp(player->cnum), nws.nws_vno)))
114                     continue;
115             }
116             if (page == N_FRONT &&
117                 (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                 sectors_were_taken += nws.nws_ntm;
122             }
123             preport(&nws);
124         }
125     }
126     if (sectors_were_taken) {
127         for (i = 0; i < MAXNOC; ++i) {
128             for (j = 0; j < i; ++j) {
129                 sectors_delta = sectors_taken[i][j] - sectors_taken[j][i];
130                 if (max_delta < abs(sectors_delta))
131                     max_delta = abs(sectors_delta);
132             }
133         }
134         pr("\n\t ===  The Bottom Line   ==\n");
135         for (k = max_delta; k > 0; --k) {
136             for (i = 0; i < MAXNOC; ++i) {
137                 for (j = 0; j < i; ++j) {
138                     sectors_delta = sectors_taken[i][j] -
139                         sectors_taken[j][i];
140                     abs_delta = abs(sectors_delta);
141                     if (abs_delta != k)
142                         continue;
143                     if (abs_delta == 1)
144                         verb = "stole";
145                     else if (abs_delta < 4)
146                         verb = "took";
147                     else if (abs_delta < 8)
148                         verb = "captured";
149                     else
150                         verb = "seized";
151                     if (sectors_delta > 0) {
152                         numstr(num, abs_delta);
153                         pr("%s %s %s sector%s from %s\n", cname(i), verb,
154                            num, splur(sectors_delta), cname(j));
155                     } else if (sectors_delta < 0) {
156                         numstr(num, abs_delta);
157                         pr("%s %s %s sector%s from %s\n", cname(j), verb,
158                            num, splur(-sectors_delta), cname(i));
159                     }
160                 }
161             }
162         }
163     }
164     if (!there_is_news)
165         pr("\nNo news at the moment...\n");
166     return 0;
167 }
168
169 static void
170 preport(register struct nwsstr *np)
171 {
172     register s_char *cp;
173     register int i;
174     s_char buf[255];
175     s_char num[128];
176     s_char *ptr;
177
178     cp = buf;
179     sprintf(buf, "%-16.16s  ", ctime(&np->nws_when));
180     cp += strlen(cp);
181     ptr = numstr(num, np->nws_ntm);
182     /*
183      * vary the order of the printing of "%d times "
184      */
185     if ((random() & 3) == 0 && np->nws_ntm > 1) {
186         sprintf(cp, "%s times ", ptr);
187         cp += strlen(cp);
188         np->nws_ntm = 1;
189     }
190     strcpy(cp, cname(np->nws_ano));
191     cp += strlen(cp);
192     *cp++ = ' ';
193     if (np->nws_vrb < 1 || np->nws_vrb > N_MAX_VERB)
194         np->nws_vrb = 0;
195     sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[random() % NUM_RPTS],
196             cname(np->nws_vno));
197     cp += strlen(cp);
198     if (np->nws_ntm != 1) {
199         sprintf(cp, " %s times", ptr);
200         cp += strlen(cp);
201     }
202     if (*buf >= 'a' && *buf <= 'z')
203         *buf += 'A' - 'a';
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 }