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