]> git.pond.sub.org Git - empserver/blob - src/lib/commands/head.c
Update copyright notice.
[empserver] / src / lib / commands / head.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  *  head.c: Print headlines of the 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 "news.h"
39 #include "nat.h"
40 #include "file.h"
41 #include "xy.h"
42 #include "nsc.h"
43 #include "commands.h"
44
45 struct histstr {
46     int h_past;
47     int h_recent;
48 };
49
50 static s_char *head_meanwhile(int val);
51 static s_char *head_describe(struct histstr *hp, int what);
52 static int head_printscoop(struct histstr (*hist)[MAXNOC], natid ano,
53                            natid vno);
54 static int head_findscoop(struct histstr (*hist)[MAXNOC],
55                           register natid maxcnum, natid *ano, natid *vno);
56
57 int
58 head(void)
59 {
60     register int i;
61     register struct histstr *hp;
62     register natid maxcnum;
63     time_t now;
64     int severity;
65     int scoop;
66     time_t news_per;
67     time_t news_age;
68     struct histstr hist[MAXNOC][MAXNOC];
69     struct natstr *natp;
70     struct nwsstr news;
71     natid actor;
72     natid victim;
73     struct nstr_item nstr;
74     int n;
75
76     (void)time(&now);
77     natp = getnatp(player->cnum);
78     if (player->argp[1] != 0 && *player->argp[1] != 0) {
79         news_per = days(atoi(player->argp[1]));
80         if (news_per > days(3))
81             news_per = days(3);
82     } else
83         /* replaced the following line with the one under it to
84          * fix headlines which never change.
85          * 3/3/90 bailey@math-cs.kent.edu
86          */
87 /*              news_per = natp->nat_newstim; */
88         news_per = now - natp->nat_newstim;
89     pr("\n        -=[  EMPIRE NEWS  ]=-\n");
90     pr("::::::::::::::::::::::::::::::::::::::::::::::::::\n");
91     pr("!       \"All the news that fits, we print.\"      !\n");
92     pr("::::::::::::::::::::::::::::::::::::::::::::::::::\n");
93     pr("       %s", ctime(&now));
94     pr("\n");
95     memset(hist, 0, sizeof(hist));
96     snxtitem_all(&nstr, EF_NEWS);
97     maxcnum = 0;
98     while (nxtitem(&nstr, &news)) {
99         news_age = now - news.nws_when;
100         if (news_age > news_per)
101             continue;
102         if (news.nws_ano == news.nws_vno)
103             continue;
104         if ((i = rpt[(int)news.nws_vrb].r_good_will) == 0)
105             continue;
106         if (news_age > (news_per / 2))
107             hist[news.nws_ano][news.nws_vno].h_past += i;
108         else
109             hist[news.nws_ano][news.nws_vno].h_recent += i;
110         if (maxcnum < news.nws_ano)
111             maxcnum = news.nws_ano;
112         if (maxcnum < news.nws_vno)
113             maxcnum = news.nws_vno;
114     }
115     for (n = 0; n < 5; n++) {
116         if ((scoop = head_findscoop(hist, maxcnum, &actor, &victim)) < 10)
117             break;
118         severity = head_printscoop(hist, actor, victim);
119         hp = &hist[actor][victim];
120         severity = hp->h_recent - hp->h_past;
121         if (severity <= -scoop / 2 || severity >= scoop / 2) {
122             pr("\t%s\n", head_meanwhile(severity));
123             (void)head_printscoop(hist, victim, actor);
124         }
125     }
126     if (n <= 1)
127         pr("\nRelative calm prevails.\n");
128     return RET_OK;
129 }
130
131 static int
132 head_printscoop(struct histstr (*hist)[MAXNOC], natid ano, natid vno)
133 {
134     register struct histstr *hp;
135     int severity;
136
137     hp = &hist[ano][vno];
138     severity = abs(hp->h_past) > abs(hp->h_recent) ? 1 : 0;
139     severity += (hp->h_past >= 0) ? 2 : 0;
140     severity += (hp->h_recent >= 0) ? 4 : 0;
141     pr(head_describe(hp, severity), cname(ano), cname(vno));
142     pr("\n");
143     hp->h_past = 0;
144     hp->h_recent = 0;
145     return severity;
146 }
147
148 static s_char *
149 head_meanwhile(int val)
150 {
151     switch (val & 03) {
152     case 0:
153         return "Meanwhile";
154     case 1:
155         return "On the other hand";
156     case 2:
157         return "At the same time";
158     case 3:
159         return "Although";
160     }
161     /*NOTREACHED*/
162     return "";
163 }
164
165 static s_char *
166 head_describe(struct histstr *hp, int what)
167 {
168     s_char *cp;
169
170     cp = 0;
171     switch (what) {
172     case 0:
173         if (hp->h_recent > hp->h_past / 2)
174             cp = "Bad relations between %s and %s worsen!";
175         else
176             cp = "Carnage wrought by %s on %s continues unabated!";
177         break;
178     case 1:
179         if (hp->h_recent < -16)
180             cp = "%s agression against %s has lessened slightly";
181         else
182             cp = "Peace talks may occur between %s & %s";
183         break;
184     case 2:
185         if (hp->h_recent < -16) {
186             if (hp->h_past > 0)
187                 cp = " ! WAR !  Reversal of prior %s -- %s relations";
188             else if (hp->h_recent >= -25)
189                 cp = "VIOLENCE ERUPTS! -- %s wages war on %s";
190             else
191                 cp = "%s wreaks havoc on %s!";
192         } else
193             cp = "Breakdown in communication between %s & %s";
194         break;
195     case 3:
196         cp = "FLASH!    %s turns on former ally, %s!";
197         break;
198     case 4:
199         cp = "%s \"makes friends\" with %s";
200         break;
201     case 5:
202         if (hp->h_past >= -25)
203             cp = "%s seems to have forgotten earlier disagreement with %s";
204         else
205             cp = "Tensions ease as %s attacks on %s seem at an end";
206         break;
207     case 6:
208         cp = "%s good deeds further growing alliance with %s";
209         break;
210     case 7:
211         if (hp->h_recent - hp->h_past < 12)
212             cp = "Honeymoon appears to be over between %s & %s";
213         else
214             cp = "Friendly relations between %s & %s have cooled";
215         break;
216     }
217     return cp;
218 }
219
220 /*
221  * returns 9 if no scoops were found
222  * Pretty strange.
223  */
224 static int
225 head_findscoop(struct histstr (*hist)[MAXNOC], register natid maxcnum,
226                natid *ano, natid *vno)
227 {
228     register struct histstr *hp;
229     register int i;
230     register int j;
231     register int k;
232     int scoop;
233     natid actor;
234     natid victim;
235
236     scoop = 9;
237     actor = 0;
238     victim = 0;
239     for (i = 1; i < maxcnum; i++) {
240         for (j = 1; j < maxcnum; j++) {
241             hp = &hist[i][j];
242             k = abs(hp->h_recent / 2);
243             if (k > scoop) {
244                 scoop = k;
245                 actor = (natid)i;
246                 victim = (natid)j;
247             }
248             k = abs(hp->h_recent - hp->h_past);
249             if (k > scoop) {
250                 scoop = k;
251                 actor = (natid)i;
252                 victim = (natid)j;
253             }
254         }
255     }
256     *ano = actor;
257     *vno = victim;
258     return scoop;
259 }