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