]> 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-2005, 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     memset(hist, 0, 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 int
130 head_printscoop(struct histstr (*hist)[MAXNOC], natid ano, natid vno)
131 {
132     register struct histstr *hp;
133     int severity;
134
135     hp = &hist[ano][vno];
136     severity = abs(hp->h_past) > abs(hp->h_recent) ? 1 : 0;
137     severity += (hp->h_past >= 0) ? 2 : 0;
138     severity += (hp->h_recent >= 0) ? 4 : 0;
139     pr(head_describe(hp, severity), cname(ano), cname(vno));
140     pr("\n");
141     hp->h_past = 0;
142     hp->h_recent = 0;
143     return severity;
144 }
145
146 static s_char *
147 head_meanwhile(int val)
148 {
149     switch (val & 03) {
150     case 0:
151         return "Meanwhile";
152     case 1:
153         return "On the other hand";
154     case 2:
155         return "At the same time";
156     case 3:
157         return "Although";
158     }
159     /*NOTREACHED*/
160     return "";
161 }
162
163 static s_char *
164 head_describe(struct histstr *hp, int what)
165 {
166     s_char *cp;
167
168     cp = 0;
169     switch (what) {
170     case 0:
171         if (hp->h_recent > hp->h_past / 2)
172             cp = "Bad relations between %s and %s worsen!";
173         else
174             cp = "Carnage wrought by %s on %s continues unabated!";
175         break;
176     case 1:
177         if (hp->h_recent < -16)
178             cp = "%s agression against %s has lessened slightly";
179         else
180             cp = "Peace talks may occur between %s & %s";
181         break;
182     case 2:
183         if (hp->h_recent < -16) {
184             if (hp->h_past > 0)
185                 cp = " ! WAR !  Reversal of prior %s -- %s relations";
186             else if (hp->h_recent >= -25)
187                 cp = "VIOLENCE ERUPTS! -- %s wages war on %s";
188             else
189                 cp = "%s wreaks havoc on %s!";
190         } else
191             cp = "Breakdown in communication between %s & %s";
192         break;
193     case 3:
194         cp = "FLASH!    %s turns on former ally, %s!";
195         break;
196     case 4:
197         cp = "%s \"makes friends\" with %s";
198         break;
199     case 5:
200         if (hp->h_past >= -25)
201             cp = "%s seems to have forgotten earlier disagreement with %s";
202         else
203             cp = "Tensions ease as %s attacks on %s seem at an end";
204         break;
205     case 6:
206         cp = "%s good deeds further growing alliance with %s";
207         break;
208     case 7:
209         if (hp->h_recent - hp->h_past < 12)
210             cp = "Honeymoon appears to be over between %s & %s";
211         else
212             cp = "Friendly relations between %s & %s have cooled";
213         break;
214     }
215     return cp;
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 }