]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nreport.c
Fix trailing whitespace
[empserver] / src / lib / subs / nreport.c
1 /*
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
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-2008
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 static struct nwsstr cache[MAXNOC][SLOTS];
48 static int news_tail;
49
50 static struct nwsstr *ncache(int actor, int event, int victim, int times);
51
52 void
53 nreport(natid actor, int event, natid victim, int times)
54 {
55     int nice;
56     struct natstr *natp;
57     struct nwsstr *np;
58
59     if (CANT_HAPPEN((unsigned)event > N_MAX_VERB
60                     || rpt[event].r_newstory[0] == rpt[0].r_newstory[0]))
61         return;
62
63     np = ncache(actor, event, victim, times);
64     putnews(np->nws_uid, np);
65
66     /*
67      * this is probably pretty expensive, but hopefully we
68      * don't fire zillions of these things off every second.
69      */
70     if (victim == 0 || (nice = rpt[event].r_good_will) >= 0)
71         return;
72     /*
73      * Pretty schlocky to put it here, but
74      * I guess it can't go anywhere else.
75      */
76     if (actor == victim)
77         return;
78     if (!chance((double)-nice * times / 20.0))
79         return;
80     if ((natp = getnatp(victim)) == 0)
81         return;
82     if (getrel(natp, actor) < HOSTILE)
83         return;
84
85     setrel(victim, actor, HOSTILE);
86 }
87
88 /*
89  * Delete news articles that have expired.
90  */
91 void
92 delete_old_news(void)
93 {
94     time_t expiry_time;
95     int i, j, k;
96     struct nwsstr news;
97
98     /* skip over expired news */
99     expiry_time = time(NULL) - days(news_keep_days);
100     for (i = 0; getnews(i, &news); i++) {
101         if (news.nws_vrb == 0 || news.nws_when >= expiry_time)
102             break;
103     }
104     /* news id 0..I-1 have expired */
105     CANT_HAPPEN(i > news_tail);
106     /* no items to delete if I is equal zero */
107     if (i == 0)
108         return;
109
110     /* move unexpired news I.. to 0.., overwriting expired news */
111     for (j = 0; getnews(i + j, &news); j++) {
112         if (news.nws_vrb == 0)
113             break;
114         ef_set_uid(EF_NEWS, &news, j);
115         putnews(j, &news);
116     }
117     CANT_HAPPEN(i + j != news_tail);
118     news_tail = j;
119
120     /* mark slots no longer in use */
121     for (k = 0; k < i; k++) {
122         ef_blank(EF_NEWS, j + k, &news);
123         putnews(j + k, &news);
124     }
125
126     /* clear cache because moving news invalidated it */
127     memset(&cache, 0, sizeof(cache));
128 }
129
130 /*
131  * Initialize news reporting.
132  * Must run between open of file EF_NEWS and first nreport().
133  */
134 void
135 init_nreport(void)
136 {
137     int newest_item;
138     struct nwsstr news;
139
140     for (newest_item = 0; getnews(newest_item, &news); newest_item++) {
141         if (news.nws_vrb == 0)
142             break;
143     }
144     news_tail = newest_item;
145 }
146
147 /*
148  * Look to see if the same message has been generated
149  * in the last 5 minutes, if so just increment the times
150  * field instead of creating a new message.
151  */
152 static struct nwsstr *
153 ncache(int actor, int event, int victim, int times)
154 {
155     struct nwsstr *np;
156     int i;
157     int oldslot;
158     time_t oldtime;
159     time_t now = time(NULL);
160
161     oldslot = -1;
162     oldtime = 0x7fffffff;
163     for (i = 0; i < SLOTS; i++) {
164         np = &cache[actor][i];
165         if (np->nws_when < oldtime) {
166             oldslot = i;
167             oldtime = np->nws_when;
168         }
169         if (np->nws_vrb == 0)
170             continue;
171         if (now - np->nws_when > minutes(5))
172             continue;
173         if (np->nws_vrb == event && np->nws_vno == victim
174             && np->nws_ntm + times <= 127) {
175             np->nws_ntm += times;
176             return np;
177         }
178     }
179     if (CANT_HAPPEN(oldslot < 0))
180         oldslot = 0;
181     if (CANT_HAPPEN(!strstr(rpt[event].r_newstory[0], "%s") && victim != 0))
182         victim = 0;
183     np = &cache[actor][oldslot];
184     ef_blank(EF_NEWS, news_tail, np);
185     np->nws_ano = actor;
186     np->nws_vno = victim;
187     np->nws_when = now;
188     np->nws_vrb = event;
189     np->nws_ntm = times;
190     news_tail++;
191     return np;
192 }