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