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