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