]> git.pond.sub.org Git - empserver/blob - src/lib/subs/takeover.c
Update copyright notice.
[empserver] / src / lib / subs / takeover.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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  *  takeover.c: Take over from another country
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1996-2000
33  */
34
35 #include <config.h>
36
37 #include <stdlib.h>
38 #include "file.h"
39 #include "land.h"
40 #include "lost.h"
41 #include "misc.h"
42 #include "nat.h"
43 #include "nsc.h"
44 #include "optlist.h"
45 #include "plane.h"
46 #include "prototypes.h"
47 #include "sect.h"
48 #include "ship.h"
49 #include "xy.h"
50
51 void
52 takeover(struct sctstr *sp, natid newown)
53 {
54     struct plnstr *pp;
55     struct lndstr *lp;
56     int civ;
57     int che_count;
58     int oldche;
59     int n;
60     struct nstr_item ni;
61     struct plnstr p;
62     struct lndstr land;
63
64     /* Wipe all the distribution info */
65     memset(sp->sct_dist, 0, sizeof(sp->sct_dist));
66     memset(sp->sct_del, 0, sizeof(sp->sct_del));
67     if (sp->sct_own == 0)
68         sp->sct_off = 0;
69     else
70         sp->sct_off = 1;
71     sp->sct_dist_x = sp->sct_x;
72     sp->sct_dist_y = sp->sct_y;
73
74     pp = &p;
75     /* Take over planes */
76     snxtitem_dist(&ni, EF_PLANE, sp->sct_x, sp->sct_y, 0);
77     while (nxtitem(&ni, pp)) {
78         if (pp->pln_own != sp->sct_own)
79             continue;
80         takeover_plane(pp, newown);
81     }
82
83     /* Take over land units */
84     lp = &land;
85     snxtitem_dist(&ni, EF_LAND, sp->sct_x, sp->sct_y, 0);
86     while (nxtitem(&ni, lp)) {
87         if ((lp->lnd_own == newown) || (lp->lnd_own == 0))
88             continue;
89         if (lp->lnd_own != sp->sct_own)
90             continue;
91         if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
92             continue;
93         /* Spies get a chance to hide */
94         if (lchr[(int)lp->lnd_type].l_flags & L_SPY) {
95             if (!(chance(LND_SPY_DETECT_CHANCE(lp->lnd_effic))))
96                 continue;
97         }
98         n = lp->lnd_effic - (30 + (random() % 100));
99         if (n < 0)
100             n = 0;
101         lp->lnd_effic = n;
102         if (lp->lnd_effic < LAND_MINEFF) {
103             lp->lnd_effic = 0;
104             mpr(newown, "%s blown up by the crew!\n", prland(lp));
105             wu(0, lp->lnd_own,
106                "%s blown up by the crew when %s took %s!\n",
107                prland(lp),
108                cname(newown), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
109         } else {
110             mpr(newown, "We have captured %s!\n", prland(lp));
111             wu(0, lp->lnd_own,
112                "%s captured when %s took %s!\n",
113                prland(lp),
114                cname(newown), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
115         }
116         takeover_land(lp, newown, 1);
117     }
118
119     sp->sct_avail = 0;
120     civ = sp->sct_item[I_CIVIL];
121     oldche = sp->sct_che;
122     /*
123      * create guerrillas from civilians
124      * how spunky are these guys?
125      * n: random number from -25:75 + (50 - loyalty)
126      */
127     n = (50 - sp->sct_loyal) + ((random() % 100) - 25);
128     che_count = 0;
129     if (n > 0 && sp->sct_own == sp->sct_oldown) {
130         che_count = (civ * n / 3000) + 5;
131         if (che_count * 2 > civ)
132             che_count = civ / 2;
133         che_count /= hap_fact(getnatp(newown), getnatp(sp->sct_own));
134         if (che_count + oldche > CHE_MAX)
135             che_count = CHE_MAX - oldche;
136         if (che_count > 0) {
137             civ -= che_count;
138             che_count += oldche;
139         } else
140             che_count = oldche;
141     } else
142         che_count = oldche;
143     sp->sct_che = che_count;
144     if (newown != sp->sct_oldown)
145         sp->sct_che_target = newown;
146     if (sp->sct_che_target == 0)
147         sp->sct_che = 0;
148     sp->sct_item[I_CIVIL] = civ;
149     if (sp->sct_oldown == newown || civ == 0) {
150         /*
151          * taking over one of your old sectors
152          */
153         sp->sct_loyal = 0;
154         sp->sct_oldown = newown;
155     } else {
156         /*
157          * taking over someone else's sector
158          */
159         sp->sct_loyal = 50;
160     }
161     makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
162     makenotlost(EF_SECTOR, newown, 0, sp->sct_x, sp->sct_y);
163     sp->sct_own = newown;
164     if (opt_MOB_ACCESS) {
165         time(&sp->sct_access);
166         sp->sct_mobil = -(etu_per_update / sect_mob_neg_factor);
167     } else {
168         sp->sct_mobil = 0;
169     }
170 }
171
172 void
173 takeover_plane(struct plnstr *pp, natid newown)
174 {
175     int n;
176
177     if ((pp->pln_own == newown) || (pp->pln_own == 0))
178         return;
179     if (pp->pln_flags & PLN_LAUNCHED)
180         return;
181     if (pp->pln_ship >= 0 || pp->pln_land >= 0)
182         return;
183     /*
184      * XXX If this was done right, planes could escape,
185      * flying to a nearby friendly airport.
186      */
187     n = pp->pln_effic - (30 + (random() % 100));
188     if (n < 0)
189         n = 0;
190     pp->pln_effic = n;
191     if (pp->pln_effic < PLANE_MINEFF || pp->pln_harden > 0) {
192         pp->pln_effic = 0;
193         mpr(newown, "%s blown up by the crew!\n", prplane(pp));
194         wu(0, pp->pln_own,
195            "%s blown up by the crew to avoid capture by %s at %s!\n",
196            prplane(pp),
197            cname(newown), xyas(pp->pln_x, pp->pln_y, pp->pln_own));
198     } else {
199         mpr(newown, "We have captured %s!\n", prplane(pp));
200         wu(0, pp->pln_own,
201            "%s captured by %s at %s!\n",
202            prplane(pp),
203            cname(newown), xyas(pp->pln_x, pp->pln_y, pp->pln_own));
204     }
205     if (opt_MARKET)
206         trdswitchown(EF_PLANE, pp, newown);
207     if (pp->pln_mobil > 0)
208         pp->pln_mobil = 0;
209     makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
210     pp->pln_own = newown;
211     makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
212     pp->pln_mission = 0;
213     pp->pln_wing = 0;
214     putplane(pp->pln_uid, pp);
215 }
216
217 void
218 takeover_ship(struct shpstr *sp, natid newown, int hostile)
219 {
220     struct plnstr *pp;
221     struct lndstr *lp;
222     struct nstr_item ni;
223     struct plnstr p;
224     struct lndstr llp;
225
226     if (opt_MARKET)
227         trdswitchown(EF_SHIP, sp, newown);
228     makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
229     sp->shp_own = newown;
230     makenotlost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
231     sp->shp_mission = 0;
232     sp->shp_fleet = 0;
233     sp->shp_rflags = 0;
234     /* Keep track of when this was taken over */
235     time(&sp->shp_access);
236     memset(sp->shp_rpath, 0, sizeof(sp->shp_rpath));
237     pp = &p;
238     lp = &llp;
239     /* Take over planes */
240     snxtitem_all(&ni, EF_PLANE);
241     while (nxtitem(&ni, pp)) {
242         if (pp->pln_ship != sp->shp_uid)
243             continue;
244         if (pp->pln_own == 0)
245             continue;
246         if (hostile) {
247             if (pp->pln_effic > PLANE_MINEFF)
248                 pp->pln_effic = PLANE_MINEFF;
249         }
250         pp->pln_mobil = 0;
251         /* Keep track of when this was taken over */
252         time(&pp->pln_access);
253         if (opt_MARKET)
254             trdswitchown(EF_PLANE, pp, newown);
255         pp->pln_mission = 0;
256         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
257         pp->pln_own = newown;
258         makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid,
259                     pp->pln_x, pp->pln_y);
260         putplane(pp->pln_uid, pp);
261     }
262     /* Take over land units */
263     snxtitem_all(&ni, EF_LAND);
264     while (nxtitem(&ni, lp)) {
265         if (lp->lnd_ship != sp->shp_uid)
266             continue;
267         if (lp->lnd_own == 0)
268             continue;
269         takeover_land(lp, newown, hostile);
270     }
271     putship(sp->shp_uid, sp);
272 }
273
274 void
275 takeover_land(struct lndstr *landp, natid newown, int hostile)
276 {
277     struct plnstr *pp;
278     struct lndstr *lp;
279     struct nstr_item ni;
280     struct plnstr p;
281     struct lndstr llp;
282
283     if (landp->lnd_effic < LAND_MINEFF) {
284         putland(landp->lnd_uid, landp);
285         return;
286     }
287     landp->lnd_army = 0;
288     landp->lnd_mobil = 0;
289     landp->lnd_harden = 0;
290     /* Keep track of when this was taken over */
291     time(&landp->lnd_access);
292     if (opt_MARKET)
293         trdswitchown(EF_LAND, landp, newown);
294     landp->lnd_mission = 0;
295     makelost(EF_LAND, landp->lnd_own, landp->lnd_uid,
296              landp->lnd_x, landp->lnd_y);
297     landp->lnd_own = newown;
298     makenotlost(EF_LAND, landp->lnd_own, landp->lnd_uid,
299                 landp->lnd_x, landp->lnd_y);
300     pp = &p;
301     lp = &llp;
302     /* Take over planes */
303     snxtitem_all(&ni, EF_PLANE);
304     while (nxtitem(&ni, pp)) {
305         if (pp->pln_land != landp->lnd_uid)
306             continue;
307         if (pp->pln_own == 0)
308             continue;
309         if (hostile) {
310             if (pp->pln_effic > PLANE_MINEFF)
311                 pp->pln_effic = PLANE_MINEFF;
312         }
313         pp->pln_mobil = 0;
314         /* Keep track of when this was taken over */
315         time(&pp->pln_access);
316         if (opt_MARKET)
317             trdswitchown(EF_PLANE, pp, newown);
318         pp->pln_mission = 0;
319         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
320         pp->pln_own = newown;
321         makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid,
322                     pp->pln_x, pp->pln_y);
323         putplane(pp->pln_uid, pp);
324     }
325     /* Take over land units */
326     snxtitem_all(&ni, EF_LAND);
327     while (nxtitem(&ni, lp)) {
328         if (lp->lnd_land != landp->lnd_uid)
329             continue;
330         if (lp->lnd_own == 0)
331             continue;
332         takeover_land(lp, newown, hostile);
333     }
334     putland(landp->lnd_uid, landp);
335 }