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
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.
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.
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/>.
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.
27 * nreport.c: File a news report. Downgrade relations if things get hostile.
29 * Known contributors to this file:
32 * Ron Koenderink, 2005
33 * Markus Armbruster, 2004-2012
43 #include "prototypes.h"
52 static struct newscache cache[MAXNOC][SLOTS];
53 static unsigned char cache_oldest[MAXNOC];
56 static struct newscache *
57 ncache(int actor, int event, int victim, int times);
60 nreport(natid actor, int event, natid victim, int times)
63 struct newscache *ncp;
65 if (CANT_HAPPEN((unsigned)event > N_MAX_VERB
66 || rpt[event].r_newstory[0] == rpt[0].r_newstory[0]))
68 if (CANT_HAPPEN(actor >= MAXNOC || victim >= MAXNOC))
71 ncp = ncache(actor, event, victim, times);
72 putnews(ncp->id, &ncp->news);
75 * this is probably pretty expensive, but hopefully we
76 * don't fire zillions of these things off every second.
78 if (victim == 0 || (nice = rpt[event].r_good_will) >= 0)
81 * Pretty schlocky to put it here, but
82 * I guess it can't go anywhere else.
86 if (!chance((double)-nice * times / 20.0))
88 if (relations_with(victim, actor) < HOSTILE)
91 setrel(victim, actor, HOSTILE);
95 * Delete news articles that have expired.
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)
110 /* news id 0..I-1 have expired */
111 CANT_HAPPEN(i > news_tail);
112 /* no items to delete if I is equal zero */
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)
122 CANT_HAPPEN(i + j != news_tail);
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);
131 /* clear cache because moving news invalidated it */
132 memset(&cache, 0, sizeof(cache));
136 * Initialize news reporting.
137 * Must run between open of file EF_NEWS and first nreport().
145 for (newest_item = 0; getnews(newest_item, &news); newest_item++) {
146 if (news.nws_vrb == 0)
149 news_tail = newest_item;
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.
157 static struct newscache *
158 ncache(int actor, int event, int victim, int times)
160 struct newscache *np;
164 time_t now = time(NULL);
166 for (i = 0; i < SLOTS; i++) {
167 np = &cache[actor][i];
168 if (np->news.nws_vrb == 0)
170 dur = now - np->news.nws_when;
171 if (dur > minutes(5))
173 if (np->news.nws_vrb == event && np->news.nws_vno == victim &&
174 np->news.nws_ntm + times <= 127) {
175 np->news.nws_ntm += times;
176 np->news.nws_duration = dur;
180 if (CANT_HAPPEN(!strstr(rpt[event].r_newstory[0], "%s") && victim != 0))
182 oldslot = cache_oldest[actor];
183 if (CANT_HAPPEN(oldslot >= SLOTS))
185 np = &cache[actor][oldslot];
186 cache_oldest[actor] = (oldslot + 1) % SLOTS;
187 ef_blank(EF_NEWS, news_tail, &np->news);
188 np->news.nws_ano = actor;
189 np->news.nws_vrb = event;
190 np->news.nws_vno = victim;
191 np->news.nws_ntm = times;
192 np->news.nws_duration = 0;
193 np->news.nws_when = now;
194 np->id = news_tail++;