]> git.pond.sub.org Git - empserver/blob - src/lib/subs/takeover.c
Remove takeover_ship() and takeover_land() parameter hostile
[empserver] / src / lib / subs / takeover.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 "file.h"
38 #include "game.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_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
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_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
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);
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     sp->sct_own = newown;
162     if (opt_MOB_ACCESS) {
163         game_tick_to_now(&sp->sct_access);
164         sp->sct_mobil = -(etu_per_update / sect_mob_neg_factor);
165     } else {
166         sp->sct_mobil = 0;
167     }
168 }
169
170 void
171 takeover_plane(struct plnstr *pp, natid newown)
172 {
173     int n;
174
175     if ((pp->pln_own == newown) || (pp->pln_own == 0))
176         return;
177     if (pp->pln_flags & PLN_LAUNCHED)
178         return;
179     if (pp->pln_ship >= 0 || pp->pln_land >= 0)
180         return;
181     /*
182      * XXX If this was done right, planes could escape,
183      * flying to a nearby friendly airport.
184      */
185     n = pp->pln_effic - (30 + (random() % 100));
186     if (n < 0)
187         n = 0;
188     pp->pln_effic = n;
189     if (pp->pln_effic < PLANE_MINEFF || pp->pln_harden > 0) {
190         pp->pln_effic = 0;
191         mpr(newown, "%s blown up by the crew!\n", prplane(pp));
192         wu(0, pp->pln_own,
193            "%s blown up by the crew to avoid capture by %s at %s!\n",
194            prplane(pp),
195            cname(newown), xyas(pp->pln_x, pp->pln_y, pp->pln_own));
196     } else {
197         mpr(newown, "We have captured %s!\n", prplane(pp));
198         wu(0, pp->pln_own,
199            "%s captured by %s at %s!\n",
200            prplane(pp),
201            cname(newown), xyas(pp->pln_x, pp->pln_y, pp->pln_own));
202     }
203     if (opt_MARKET)
204         trdswitchown(EF_PLANE, pp, newown);
205     if (pp->pln_mobil > 0)
206         pp->pln_mobil = 0;
207     pp->pln_own = newown;
208     pp->pln_mission = 0;
209     pp->pln_wing = 0;
210     putplane(pp->pln_uid, pp);
211 }
212
213 void
214 takeover_ship(struct shpstr *sp, natid newown)
215 {
216     struct plnstr *pp;
217     struct lndstr *lp;
218     struct nstr_item ni;
219     struct plnstr p;
220     struct lndstr llp;
221
222     if (opt_MARKET)
223         trdswitchown(EF_SHIP, sp, newown);
224     sp->shp_own = newown;
225     sp->shp_mission = 0;
226     sp->shp_fleet = 0;
227     sp->shp_rflags = 0;
228     memset(sp->shp_rpath, 0, sizeof(sp->shp_rpath));
229     pp = &p;
230     lp = &llp;
231     /* Take over planes */
232     snxtitem_cargo(&ni, EF_PLANE, EF_SHIP, sp->shp_uid);
233     while (nxtitem(&ni, pp)) {
234         if (pp->pln_effic > PLANE_MINEFF)
235             pp->pln_effic = PLANE_MINEFF;
236         pp->pln_mobil = 0;
237         if (opt_MOB_ACCESS)
238             game_tick_to_now(&pp->pln_access);
239         if (opt_MARKET)
240             trdswitchown(EF_PLANE, pp, newown);
241         pp->pln_mission = 0;
242         pp->pln_own = newown;
243         putplane(pp->pln_uid, pp);
244     }
245     /* Take over land units */
246     snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
247     while (nxtitem(&ni, lp))
248         takeover_land(lp, newown);
249     putship(sp->shp_uid, sp);
250 }
251
252 void
253 takeover_land(struct lndstr *landp, natid newown)
254 {
255     struct plnstr *pp;
256     struct lndstr *lp;
257     struct nstr_item ni;
258     struct plnstr p;
259     struct lndstr llp;
260
261     if (landp->lnd_effic < LAND_MINEFF) {
262         putland(landp->lnd_uid, landp);
263         return;
264     }
265     landp->lnd_army = 0;
266     landp->lnd_mobil = 0;
267     if (opt_MOB_ACCESS)
268         game_tick_to_now(&landp->lnd_access);
269     landp->lnd_harden = 0;
270     if (opt_MARKET)
271         trdswitchown(EF_LAND, landp, newown);
272     landp->lnd_mission = 0;
273     landp->lnd_own = newown;
274     pp = &p;
275     lp = &llp;
276     /* Take over planes */
277     snxtitem_cargo(&ni, EF_PLANE, EF_LAND, landp->lnd_uid);
278     while (nxtitem(&ni, pp)) {
279         if (pp->pln_effic > PLANE_MINEFF)
280             pp->pln_effic = PLANE_MINEFF;
281         pp->pln_mobil = 0;
282         if (opt_MOB_ACCESS)
283             game_tick_to_now(&pp->pln_access);
284         if (opt_MARKET)
285             trdswitchown(EF_PLANE, pp, newown);
286         pp->pln_mission = 0;
287         pp->pln_own = newown;
288         putplane(pp->pln_uid, pp);
289     }
290     /* Take over land units */
291     snxtitem_cargo(&ni, EF_LAND, EF_LAND, landp->lnd_uid);
292     while (nxtitem(&ni, lp))
293         takeover_land(lp, newown);
294     putland(landp->lnd_uid, landp);
295 }