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