]> git.pond.sub.org Git - empserver/blob - src/lib/commands/head.c
bc38c9155170dd996d67285d97b2cd2e4d235d6d
[empserver] / src / lib / commands / head.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2017, 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  *     Markus Armbruster, 2006-2013
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     static char *meanwhile[4] = {
141         "Meanwhile", "On the other hand", "At the same time", "Although"
142     };
143     return meanwhile[val % 4];
144 }
145
146 static void
147 head_describe(struct histstr *hp, int what, char *aname, char *vname)
148 {
149     switch (what) {
150     case 0:
151         if (hp->h_recent > hp->h_past / 2)
152             pr("Bad relations between %s and %s worsen!", aname, vname);
153         else
154             pr("Carnage wrought by %s on %s continues unabated!",
155                aname, vname);
156         break;
157     case 1:
158         if (hp->h_recent < -16)
159             pr("%s aggression against %s has lessened slightly",
160                aname, vname);
161         else
162             pr("Peace talks may occur between %s & %s", aname, vname);
163         break;
164     case 2:
165         if (hp->h_recent < -16) {
166             if (hp->h_past > 0)
167                 pr(" ! WAR !  Reversal of prior %s -- %s relations",
168                    aname, vname);
169             else if (hp->h_recent >= -25)
170                 pr("VIOLENCE ERUPTS! -- %s wages war on %s", aname, vname);
171             else
172                 pr("%s wreaks havoc on %s!", aname, vname);
173         } else
174             pr("Breakdown in communication between %s & %s", aname, vname);
175         break;
176     case 3:
177         pr("FLASH!    %s turns on former ally, %s!", aname, vname);
178         break;
179     case 4:
180         pr("%s \"makes friends\" with %s", aname, vname);
181         break;
182     case 5:
183         if (hp->h_past >= -25)
184             pr("%s seems to have forgotten earlier disagreement with %s",
185                aname, vname);
186         else
187             pr("Tensions ease as %s attacks on %s seem at an end",
188                aname, vname);
189         break;
190     case 6:
191         pr("%s good deeds further growing alliance with %s", aname, vname);
192         break;
193     case 7:
194         if (hp->h_recent - hp->h_past < 12)
195             pr("Honeymoon appears to be over between %s & %s",
196                aname, vname);
197         else
198             pr("Friendly relations between %s & %s have cooled",
199                aname, vname);
200         break;
201     }
202 }
203
204 /*
205  * returns 9 if no scoops were found
206  * Pretty strange.
207  */
208 static int
209 head_findscoop(struct histstr (*hist)[MAXNOC], natid maxcnum,
210                natid *ano, natid *vno)
211 {
212     struct histstr *hp;
213     int i, j, k, scoop;
214     natid actor, victim;
215
216     scoop = 9;
217     actor = 0;
218     victim = 0;
219     for (i = 1; i < maxcnum; i++) {
220         for (j = 1; j < maxcnum; j++) {
221             hp = &hist[i][j];
222             k = abs(hp->h_recent / 2);
223             if (k > scoop) {
224                 scoop = k;
225                 actor = (natid)i;
226                 victim = (natid)j;
227             }
228             k = abs(hp->h_recent - hp->h_past);
229             if (k > scoop) {
230                 scoop = k;
231                 actor = (natid)i;
232                 victim = (natid)j;
233             }
234         }
235     }
236     *ano = actor;
237     *vno = victim;
238     return scoop;
239 }