]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nreport.c
Update copyright notice.
[empserver] / src / lib / subs / nreport.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "prototypes.h"
36 #include "news.h"
37 #include "file.h"
38 #include "optlist.h"
39
40 #define SLOTS   5
41
42 struct newscache {
43     struct nwsstr news;
44     int id;
45 };
46
47 static struct newscache cache[MAXNOC][SLOTS];
48 static int news_tail;
49
50 static struct newscache *
51 ncache(int actor, int event, int victim, int times);
52
53 void
54 nreport(natid actor, int event, natid victim, int times)
55 {
56     int nice;
57     int rel;
58     struct natstr *natp;
59     struct newscache *ncp;
60
61     ncp = ncache(actor, event, victim, times);
62     putnews(ncp->id, &ncp->news);
63
64     /*
65      * this is probably pretty expensive, but hopefully we
66      * don't fire zillions of these things off every second.
67      */
68     if (victim == 0 || (nice = rpt[event].r_good_will) >= 0)
69         return;
70     /*
71      * Pretty schlocky to put it here, but
72      * I guess it can't go anywhere else.
73      */
74     if (actor == victim)
75         return;
76     if (!chance((double)-nice * times / 20.0))
77         return;
78     if ((natp = getnatp(victim)) == 0)
79         return;
80     if ((rel = getrel(natp, actor)) < HOSTILE)
81         return;
82
83     rel = HOSTILE;
84 /*
85         if (rel > HOSTILE)
86                 rel = HOSTILE;
87         else
88                 rel = AT_WAR;
89  */
90     setrel(victim, actor, rel);
91 }
92
93 /*
94  * Delete news articles that have expired.
95  */
96 void
97 delete_old_news(void)
98 {
99     time_t expiry_time;
100     int i, j, k;
101     struct nwsstr news;
102
103     /* skip over expired news */
104     expiry_time = time(NULL) - days(news_keep_days);
105     for (i = 0; getnews(i, &news); i++) {
106         if (news.nws_when == 0 || news.nws_when >= expiry_time)
107             break;
108     }
109     /* news id 0..I-1 have expired */
110     CANT_HAPPEN(i > news_tail);
111     /* no items to delete if I is equal zero */
112     if (i == 0)
113         return;
114
115     /* move unexpired news I.. to 0.., overwriting expired news */
116     for (j = 0; getnews(i + j, &news); j++) {
117         if (news.nws_when == 0)
118             break;
119         putnews(j, &news);
120     }
121     CANT_HAPPEN(i + j != news_tail);
122     news_tail = j;
123
124     /* mark slots no longer in use */
125     memset(&news, 0, sizeof(news));
126     for (k = 0; k < i; k++)
127         putnews(j + k, &news);
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_when == 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     register struct newscache *np;
159     int i;
160     int oldslot;
161     time_t oldtime;
162     time_t now = time(NULL);
163
164     oldslot = -1;
165     oldtime = 0x7fffffff;
166     for (i = 0; i < SLOTS; i++) {
167         np = &cache[actor][i];
168         if (np->news.nws_when < oldtime) {
169             oldslot = i;
170             oldtime = np->news.nws_when;
171         }
172         if (np->id == 0)
173             continue;
174         if ((now - np->news.nws_when) > minutes(5))
175             continue;
176         if (np->news.nws_vrb == event && np->news.nws_vno == victim &&
177             np->news.nws_ntm + times <= 127) {
178             np->news.nws_ntm += times;
179             return np;
180         }
181     }
182     if (oldslot < 0) {
183         logerror("internal error; ncache oldslot < 0");
184         return &cache[actor][0];
185     }
186     np = &cache[actor][oldslot];
187     np->news.nws_ano = actor;
188     np->news.nws_vno = victim;
189     np->news.nws_when = now;
190     np->news.nws_vrb = event;
191     np->news.nws_ntm = times;
192     ef_ensure_space(EF_NEWS, news_tail, 100);
193     np->id = news_tail++;
194     return np;
195 }