]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nreport.c
Update known contributors comment.
[empserver] / src / lib / subs / nreport.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 "prototypes.h"
39 #include "news.h"
40 #include "file.h"
41 #include "optlist.h"
42
43 #define SLOTS   5
44
45 struct newscache {
46     struct nwsstr news;
47     int id;
48 };
49
50 static struct newscache cache[MAXNOC][SLOTS];
51 static int news_tail;
52
53 static struct newscache *
54 ncache(int actor, int event, int victim, int times);
55
56 void
57 nreport(natid actor, int event, natid victim, int times)
58 {
59     int nice;
60     struct natstr *natp;
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
67     ncp = ncache(actor, event, victim, times);
68     putnews(ncp->id, &ncp->news);
69
70     /*
71      * this is probably pretty expensive, but hopefully we
72      * don't fire zillions of these things off every second.
73      */
74     if (victim == 0 || (nice = rpt[event].r_good_will) >= 0)
75         return;
76     /*
77      * Pretty schlocky to put it here, but
78      * I guess it can't go anywhere else.
79      */
80     if (actor == victim)
81         return;
82     if (!chance((double)-nice * times / 20.0))
83         return;
84     if ((natp = getnatp(victim)) == 0)
85         return;
86     if (getrel(natp, 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_when == 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_when == 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     memset(&news, 0, sizeof(news));
125     for (k = 0; k < i; k++)
126         putnews(j + k, &news);
127
128     /* clear cache because moving news invalidated it */
129     memset(&cache, 0, sizeof(cache));
130 }
131
132 /*
133  * Initialize news reporting.
134  * Must run between open of file EF_NEWS and first nreport().
135  */
136 void
137 init_nreport(void)
138 {
139     int newest_item;
140     struct nwsstr news;
141
142     for (newest_item = 0; getnews(newest_item, &news); newest_item++) {
143         if (news.nws_when == 0)
144             break;
145     }
146     news_tail = newest_item;
147 }
148
149 /*
150  * Look to see if the same message has been generated
151  * in the last 5 minutes, if so just increment the times
152  * field instead of creating a new message.
153  */
154 static struct newscache *
155 ncache(int actor, int event, int victim, int times)
156 {
157     register struct newscache *np;
158     int i;
159     int oldslot;
160     time_t oldtime;
161     time_t now = time(NULL);
162
163     oldslot = -1;
164     oldtime = 0x7fffffff;
165     for (i = 0; i < SLOTS; i++) {
166         np = &cache[actor][i];
167         if (np->news.nws_when < oldtime) {
168             oldslot = i;
169             oldtime = np->news.nws_when;
170         }
171         if (np->id == 0)
172             continue;
173         if ((now - np->news.nws_when) > minutes(5))
174             continue;
175         if (np->news.nws_vrb == event && np->news.nws_vno == victim &&
176             np->news.nws_ntm + times <= 127) {
177             np->news.nws_ntm += times;
178             return np;
179         }
180     }
181     if (CANT_HAPPEN(oldslot < 0))
182         oldslot = 0;
183     if (CANT_HAPPEN(!strstr(rpt[event].r_newstory[0], "%s") && victim != 0))
184         victim = 0;
185     np = &cache[actor][oldslot];
186     np->news.nws_ano = actor;
187     np->news.nws_vno = victim;
188     np->news.nws_when = now;
189     np->news.nws_vrb = event;
190     np->news.nws_ntm = times;
191     ef_ensure_space(EF_NEWS, news_tail, 100);
192     np->id = news_tail++;
193     return np;
194 }