]> 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-2004, 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 "file.h"
39 #include "empio.h"
40 #include <fcntl.h>
41 #include "optlist.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     time_t oldnewstime;
110     int n;
111     struct nwsstr news;
112
113     oldnewstime = time(NULL) - days(news_keep_days);
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 int
126 nextfree(void)
127 {
128     struct free *fp;
129     int id;
130
131     if (freelist == 0)
132         findfree();
133     if ((fp = freelist) == 0)
134         return 0;
135     freelist = fp->next;
136     id = fp->id;
137     free(fp);
138     return id;
139 }
140
141 #define SLOTS   5
142
143 struct newscache {
144     struct nwsstr news;
145     int id;
146 };
147
148 static struct newscache *
149 ncache(time_t now, int actor, int event, int victim, int times)
150 {
151     static struct newscache cache[MAXNOC][SLOTS];
152     register struct newscache *np;
153     int i;
154     int oldslot;
155     time_t oldtime;
156
157     oldslot = -1;
158     oldtime = 0x7fffffff;
159     for (i = 0; i < SLOTS; i++) {
160         np = &cache[actor][i];
161         if (np->news.nws_when < oldtime) {
162             oldslot = i;
163             oldtime = np->news.nws_when;
164         }
165         if (np->id == 0)
166             continue;
167         if ((now - np->news.nws_when) > minutes(5))
168             continue;
169         if (np->news.nws_vrb == event && np->news.nws_vno == victim &&
170             np->news.nws_ntm + times <= 127) {
171             np->news.nws_ntm += times;
172             return np;
173         }
174     }
175     if (oldslot < 0) {
176         logerror("internal error; ncache oldslot < 0");
177         return &cache[actor][0];
178     }
179     np = &cache[actor][oldslot];
180     np->news.nws_ano = actor;
181     np->news.nws_vno = victim;
182     np->news.nws_when = now;
183     np->news.nws_vrb = event;
184     np->news.nws_ntm = times;
185     np->id = nextfree();
186     return np;
187 }
188
189 static void
190 filereport(int actor, int event, int victim, int times)
191 {
192     struct newscache *np;
193     time_t now;
194
195     time(&now);
196     np = ncache(now, actor, event, victim, times);
197     ef_write(EF_NEWS, np->id, (s_char *)&np->news);
198 }