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