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