]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nreport.c
Spell ID and UID consistently all-caps
[empserver] / src / lib / subs / nreport.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2017, 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-2016
34  */
35
36 #include <config.h>
37
38 #include "chance.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 *ncache(int, int, int, int);
56
57 void
58 nreport(natid actor, int event, natid victim, int times)
59 {
60     int nice;
61     struct newscache *ncp;
62
63     if (CANT_HAPPEN((unsigned)event > N_MAX_VERB
64                     || rpt[event].r_newstory[0] == rpt[0].r_newstory[0]))
65         return;
66     if (CANT_HAPPEN(actor >= MAXNOC || victim >= MAXNOC))
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 (relations_with(victim, actor) < HOSTILE)
87         return;
88
89     setrel(victim, actor, HOSTILE);
90 }
91
92 /*
93  * Delete news articles that have expired.
94  */
95 void
96 delete_old_news(void)
97 {
98     time_t expiry_time;
99     int i, j, k;
100     struct nwsstr news;
101
102     /* skip over expired news */
103     expiry_time = time(NULL) - days(news_keep_days);
104     for (i = 0; getnews(i, &news); i++) {
105         if (news.nws_vrb == 0 || news.nws_when >= expiry_time)
106             break;
107     }
108     /* news ID 0..I-1 have expired */
109     CANT_HAPPEN(i > news_tail);
110     /* no items to delete if I is equal zero */
111     if (i == 0)
112         return;
113
114     /* move unexpired news I.. to 0.., overwriting expired news */
115     for (j = 0; getnews(i + j, &news); j++) {
116         if (news.nws_vrb == 0)
117             break;
118         putnews(j, &news);
119     }
120     CANT_HAPPEN(i + j != news_tail);
121     news_tail = j;
122
123     /* mark slots no longer in use */
124     for (k = 0; k < i; k++) {
125         ef_blank(EF_NEWS, j + k, &news);
126         putnews(j + k, &news);
127     }
128
129     /* clear cache because moving news invalidated it */
130     memset(&cache, 0, sizeof(cache));
131 }
132
133 /*
134  * Initialize news reporting.
135  * Must run between open of file EF_NEWS and first nreport().
136  */
137 void
138 init_nreport(void)
139 {
140     int newest_item;
141     struct nwsstr news;
142
143     for (newest_item = 0; getnews(newest_item, &news); newest_item++) {
144         if (news.nws_vrb == 0)
145             break;
146     }
147     news_tail = newest_item;
148 }
149
150 /*
151  * Look to see if the same message has been generated
152  * in the last 5 minutes, if so just increment the times
153  * field instead of creating a new message.
154  */
155 static struct newscache *
156 ncache(int actor, int event, int victim, int times)
157 {
158     struct newscache *np;
159     int i;
160     unsigned oldslot;
161     time_t dur;
162     time_t now = time(NULL);
163
164     for (i = 0; i < SLOTS; i++) {
165         np = &cache[actor][i];
166         if (np->news.nws_vrb == 0)
167             continue;
168         dur = now - np->news.nws_when;
169         if (dur > minutes(5))
170             continue;
171         if (np->news.nws_vrb == event && np->news.nws_vno == victim) {
172             np->news.nws_ntm = LIMIT_TO(np->news.nws_ntm + times,
173                                         0, 65535);
174             np->news.nws_duration = dur;
175             return np;
176         }
177     }
178     if (CANT_HAPPEN(!strstr(rpt[event].r_newstory[0], "%s") && victim != 0))
179         victim = 0;
180     oldslot = cache_oldest[actor];
181     if (CANT_HAPPEN(oldslot >= SLOTS))
182         oldslot = 0;
183     np = &cache[actor][oldslot];
184     cache_oldest[actor] = (oldslot + 1) % SLOTS;
185     ef_blank(EF_NEWS, news_tail, &np->news);
186     np->news.nws_ano = actor;
187     np->news.nws_vrb = event;
188     np->news.nws_vno = victim;
189     np->news.nws_ntm = times;
190     np->news.nws_duration = 0;
191     np->news.nws_when = now;
192     np->id = news_tail++;
193     return np;
194 }