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