]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nreport.c
Record news more compactly
[empserver] / src / lib / subs / nreport.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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  *  nreport.c: File a news report.  Downgrade relations if things get hostile.
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 1997
33  *     Ron Koenderink, 2005
34  *     Markus Armbruster, 2004-2009
35  */
36
37 #include <config.h>
38
39 #include "file.h"
40 #include "nat.h"
41 #include "news.h"
42 #include "optlist.h"
43 #include "prototypes.h"
44
45 #define SLOTS   5
46
47 struct newscache {
48     struct nwsstr news;
49     int id;
50 };
51
52 static struct newscache cache[MAXNOC][SLOTS];
53 static int news_tail;
54
55 static struct newscache *
56 ncache(int actor, int event, int victim, int times);
57
58 void
59 nreport(natid actor, int event, natid victim, int times)
60 {
61     int nice;
62     struct natstr *natp;
63     struct newscache *ncp;
64
65     if (CANT_HAPPEN((unsigned)event > N_MAX_VERB
66                     || rpt[event].r_newstory[0] == rpt[0].r_newstory[0]))
67         return;
68
69     ncp = ncache(actor, event, victim, times);
70     putnews(ncp->id, &ncp->news);
71
72     /*
73      * this is probably pretty expensive, but hopefully we
74      * don't fire zillions of these things off every second.
75      */
76     if (victim == 0 || (nice = rpt[event].r_good_will) >= 0)
77         return;
78     /*
79      * Pretty schlocky to put it here, but
80      * I guess it can't go anywhere else.
81      */
82     if (actor == victim)
83         return;
84     if (!chance((double)-nice * times / 20.0))
85         return;
86     if (!(natp = getnatp(victim)))
87         return;
88     if (getrel(natp, actor) < HOSTILE)
89         return;
90
91     setrel(victim, actor, HOSTILE);
92 }
93
94 /*
95  * Delete news articles that have expired.
96  */
97 void
98 delete_old_news(void)
99 {
100     time_t expiry_time;
101     int i, j, k;
102     struct nwsstr news;
103
104     /* skip over expired news */
105     expiry_time = time(NULL) - days(news_keep_days);
106     for (i = 0; getnews(i, &news); i++) {
107         if (news.nws_vrb == 0 || news.nws_when >= expiry_time)
108             break;
109     }
110     /* news id 0..I-1 have expired */
111     CANT_HAPPEN(i > news_tail);
112     /* no items to delete if I is equal zero */
113     if (i == 0)
114         return;
115
116     /* move unexpired news I.. to 0.., overwriting expired news */
117     for (j = 0; getnews(i + j, &news); j++) {
118         if (news.nws_vrb == 0)
119             break;
120         putnews(j, &news);
121     }
122     CANT_HAPPEN(i + j != news_tail);
123     news_tail = j;
124
125     /* mark slots no longer in use */
126     for (k = 0; k < i; k++) {
127         ef_blank(EF_NEWS, j + k, &news);
128         putnews(j + k, &news);
129     }
130
131     /* clear cache because moving news invalidated it */
132     memset(&cache, 0, sizeof(cache));
133 }
134
135 /*
136  * Initialize news reporting.
137  * Must run between open of file EF_NEWS and first nreport().
138  */
139 void
140 init_nreport(void)
141 {
142     int newest_item;
143     struct nwsstr news;
144
145     for (newest_item = 0; getnews(newest_item, &news); newest_item++) {
146         if (news.nws_vrb == 0)
147             break;
148     }
149     news_tail = newest_item;
150 }
151
152 /*
153  * Look to see if the same message has been generated
154  * in the last 5 minutes, if so just increment the times
155  * field instead of creating a new message.
156  */
157 static struct newscache *
158 ncache(int actor, int event, int victim, int times)
159 {
160     struct newscache *np;
161     int i;
162     int oldslot;
163     time_t oldtime, dur;
164     time_t now = time(NULL);
165
166     oldslot = -1;
167     oldtime = 0x7fffffff;
168     for (i = 0; i < SLOTS; i++) {
169         np = &cache[actor][i];
170         if (np->news.nws_when < oldtime) {
171             oldslot = i;
172             oldtime = np->news.nws_when;
173         }
174         if (np->news.nws_vrb == 0)
175             continue;
176         dur = now - np->news.nws_when;
177         if (dur > minutes(5))
178             continue;
179         if (np->news.nws_vrb == event && np->news.nws_vno == victim &&
180             np->news.nws_ntm + times <= 127) {
181             np->news.nws_ntm += times;
182             np->news.nws_duration = dur;
183             return np;
184         }
185     }
186     if (CANT_HAPPEN(oldslot < 0))
187         oldslot = 0;
188     if (CANT_HAPPEN(!strstr(rpt[event].r_newstory[0], "%s") && victim != 0))
189         victim = 0;
190     np = &cache[actor][oldslot];
191     ef_blank(EF_NEWS, news_tail, &np->news);
192     np->news.nws_ano = actor;
193     np->news.nws_vrb = event;
194     np->news.nws_vno = victim;
195     np->news.nws_ntm = times;
196     np->news.nws_duration = 0;
197     np->news.nws_when = now;
198     np->id = news_tail++;
199     return np;
200 }