2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
28 * nreport.c: File a news report. Downgrade relations if things get hostile.
30 * Known contributors to this file:
33 * Ron Koenderink, 2005
42 #include "prototypes.h"
46 static struct nwsstr cache[MAXNOC][SLOTS];
49 static struct nwsstr *ncache(int actor, int event, int victim, int times);
52 nreport(natid actor, int event, natid victim, int times)
58 if (CANT_HAPPEN((unsigned)event > N_MAX_VERB
59 || rpt[event].r_newstory[0] == rpt[0].r_newstory[0]))
62 np = ncache(actor, event, victim, times);
63 putnews(np->nws_uid, np);
66 * this is probably pretty expensive, but hopefully we
67 * don't fire zillions of these things off every second.
69 if (victim == 0 || (nice = rpt[event].r_good_will) >= 0)
72 * Pretty schlocky to put it here, but
73 * I guess it can't go anywhere else.
77 if (!chance((double)-nice * times / 20.0))
79 if ((natp = getnatp(victim)) == 0)
81 if (getrel(natp, actor) < HOSTILE)
84 setrel(victim, actor, HOSTILE);
88 * Delete news articles that have expired.
97 /* skip over expired news */
98 expiry_time = time(NULL) - days(news_keep_days);
99 for (i = 0; getnews(i, &news); i++) {
100 if (news.nws_when == 0 || news.nws_when >= expiry_time)
103 /* news id 0..I-1 have expired */
104 CANT_HAPPEN(i > news_tail);
105 /* no items to delete if I is equal zero */
109 /* move unexpired news I.. to 0.., overwriting expired news */
110 for (j = 0; getnews(i + j, &news); j++) {
111 if (news.nws_when == 0)
116 CANT_HAPPEN(i + j != news_tail);
119 /* mark slots no longer in use */
120 for (k = 0; k < i; k++) {
121 ef_blank(EF_NEWS, j + k, &news);
122 putnews(j + k, &news);
125 /* clear cache because moving news invalidated it */
126 memset(&cache, 0, sizeof(cache));
130 * Initialize news reporting.
131 * Must run between open of file EF_NEWS and first nreport().
139 for (newest_item = 0; getnews(newest_item, &news); newest_item++) {
140 if (news.nws_when == 0)
143 news_tail = newest_item;
147 * Look to see if the same message has been generated
148 * in the last 5 minutes, if so just increment the times
149 * field instead of creating a new message.
151 static struct nwsstr *
152 ncache(int actor, int event, int victim, int times)
158 time_t now = time(NULL);
161 oldtime = 0x7fffffff;
162 for (i = 0; i < SLOTS; i++) {
163 np = &cache[actor][i];
164 if (np->nws_when < oldtime) {
166 oldtime = np->nws_when;
168 if (np->nws_uid == 0)
170 if (now - np->nws_when > minutes(5))
172 if (np->nws_vrb == event && np->nws_vno == victim
173 && np->nws_ntm + times <= 127) {
174 np->nws_ntm += times;
178 if (CANT_HAPPEN(oldslot < 0))
180 if (CANT_HAPPEN(!strstr(rpt[event].r_newstory[0], "%s") && victim != 0))
182 np = &cache[actor][oldslot];
183 ef_blank(EF_NEWS, news_tail, np);
185 np->nws_vno = victim;