]> git.pond.sub.org Git - empserver/blob - src/lib/subs/nreport.c
(at_minimum, blocksig, emp_bitinit, filelogerror, iceil, ifloor,
[empserver] / src / lib / subs / nreport.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 "misc.h"
36 #include "news.h"
37 #include "nat.h"
38 #include "deity.h"
39 #include "file.h"
40 #include "empio.h"
41 #include <fcntl.h>
42 #include "prototypes.h"
43
44 static void filereport(int, int, int, int);
45
46 void
47 nreport(natid actor, int event, natid victim, int times)
48 {
49     int nice;
50     int rel;
51     struct natstr *np;
52
53     filereport(actor, event, victim, times);
54     /*
55      * this is probably pretty expensive, but hopefully we
56      * don't fire zillions of these things off every second.
57      */
58     if (victim == 0 || (nice = rpt[event].r_good_will) >= 0)
59         return;
60     /*
61      * Pretty schlocky to put it here, but
62      * I guess it can't go anywhere else.
63      */
64     if (actor == victim)
65         return;
66     if (!chance((double)-nice * times / 20.0))
67         return;
68     if ((np = getnatp(victim)) == 0)
69         return;
70     if ((rel = getrel(np, actor)) < HOSTILE)
71         return;
72
73     rel = HOSTILE;
74 /*
75         if (rel > HOSTILE)
76                 rel = HOSTILE;
77         else
78                 rel = AT_WAR;
79  */
80     setrel(victim, actor, rel);
81 }
82
83 struct free {
84     struct free *next;
85     int id;
86 };
87
88 struct free *freelist;
89
90 static void
91 addfree(int n)
92 {
93     struct free *fp;
94
95     fp = (struct free *)malloc(sizeof(*fp));
96     fp->next = freelist;
97     fp->id = n;
98     freelist = fp;
99 }
100
101 /*
102  * snoop through the news articles looking
103  * for articles which have timed out.  Only
104  * called when no free items left.
105  */
106 static void
107 findfree(void)
108 {
109     register time_t oldnewstime;
110     register int n;
111     struct nwsstr news;
112     time_t newstime;
113
114     (void)time(&newstime);
115     oldnewstime = newstime - NEWS_PERIOD;
116     for (n = 0; getnews(n, &news); n++) {
117         if (news.nws_when < oldnewstime)
118             addfree(n);
119     }
120     if (freelist == 0) {
121         if (!ef_extend(EF_NEWS, 100))
122             return;
123         findfree();
124     }
125 }
126
127 static
128     int
129 nextfree(void)
130 {
131     struct free *fp;
132     int id;
133
134     if (freelist == 0)
135         findfree();
136     if ((fp = freelist) == 0)
137         return 0;
138     freelist = fp->next;
139     id = fp->id;
140     free(fp);
141     return id;
142 }
143
144 #define SLOTS   5
145
146 struct newscache {
147     struct nwsstr news;
148     int id;
149 };
150
151 static
152 struct newscache *
153 ncache(time_t now, int actor, int event, int victim, int times)
154 {
155     static struct newscache cache[MAXNOC][SLOTS];
156     register struct newscache *np;
157     int i;
158     int oldslot;
159     time_t oldtime;
160
161     oldslot = -1;
162     oldtime = 0x7fffffff;
163     for (i = 0; i < SLOTS; i++) {
164         np = &cache[actor][i];
165         if (np->news.nws_when < oldtime) {
166             oldslot = i;
167             oldtime = np->news.nws_when;
168         }
169         if (np->id == 0)
170             continue;
171         if ((now - np->news.nws_when) > minutes(5))
172             continue;
173         if (np->news.nws_vrb == event && np->news.nws_vno == victim &&
174             np->news.nws_ntm + times <= 127) {
175             np->news.nws_ntm += times;
176             return np;
177         }
178     }
179     if (oldslot < 0) {
180         logerror("internal error; ncache oldslot < 0");
181         return &cache[actor][0];
182     }
183     np = &cache[actor][oldslot];
184     np->news.nws_ano = actor;
185     np->news.nws_vno = victim;
186     np->news.nws_when = now;
187     np->news.nws_vrb = event;
188     np->news.nws_ntm = times;
189     np->id = nextfree();
190     return np;
191 }
192
193 static void
194 filereport(int actor, int event, int victim, int times)
195 {
196     struct newscache *np;
197     time_t now;
198
199     time(&now);
200     np = ncache(now, actor, event, victim, times);
201     ef_write(EF_NEWS, np->id, (s_char *)&np->news);
202 }