]> 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-2009, 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  *     Markus Armbruster, 2007-2008
34  */
35
36 #include <config.h>
37
38 #include "file.h"
39 #include "empobj.h"
40 #include "game.h"
41 #include "land.h"
42 #include "lost.h"
43 #include "misc.h"
44 #include "nat.h"
45 #include "nsc.h"
46 #include "optlist.h"
47 #include "plane.h"
48 #include "prototypes.h"
49 #include "sect.h"
50 #include "ship.h"
51 #include "unit.h"
52 #include "xy.h"
53
54 static void takeover_unit(struct empobj *, natid);
55
56 void
57 takeover(struct sctstr *sp, natid newown)
58 {
59     struct plnstr *pp;
60     struct lndstr *lp;
61     int civ;
62     int che_count;
63     int oldche;
64     int n;
65     struct nstr_item ni;
66     struct plnstr p;
67     struct lndstr land;
68
69     /* Wipe all the distribution info */
70     memset(sp->sct_dist, 0, sizeof(sp->sct_dist));
71     memset(sp->sct_del, 0, sizeof(sp->sct_del));
72     if (sp->sct_own == 0)
73         sp->sct_off = 0;
74     else
75         sp->sct_off = 1;
76     sp->sct_dist_x = sp->sct_x;
77     sp->sct_dist_y = sp->sct_y;
78
79     pp = &p;
80     /* Take over planes */
81     snxtitem_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
82     while (nxtitem(&ni, pp)) {
83         if (pp->pln_own != sp->sct_own)
84             continue;
85         takeover_plane(pp, newown);
86     }
87
88     /* Take over land units */
89     lp = &land;
90     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
91     while (nxtitem(&ni, lp)) {
92         if ((lp->lnd_own == newown) || (lp->lnd_own == 0))
93             continue;
94         if (lp->lnd_own != sp->sct_own)
95             continue;
96         if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
97             continue;
98         /* Spies get a chance to hide */
99         if (lchr[(int)lp->lnd_type].l_flags & L_SPY) {
100             if (!(chance(LND_SPY_DETECT_CHANCE(lp->lnd_effic))))
101                 continue;
102         }
103         n = lp->lnd_effic - (30 + (random() % 100));
104         if (n < 0)
105             n = 0;
106         lp->lnd_effic = n;
107         if (lp->lnd_effic < LAND_MINEFF) {
108             lp->lnd_effic = 0;
109             mpr(newown, "%s blown up by the crew!\n", prland(lp));
110             wu(0, lp->lnd_own,
111                "%s blown up by the crew when %s took %s!\n",
112                prland(lp),
113                cname(newown), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
114         } else {
115             mpr(newown, "We have captured %s!\n", prland(lp));
116             wu(0, lp->lnd_own,
117                "%s captured when %s took %s!\n",
118                prland(lp),
119                cname(newown), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
120         }
121         takeover_land(lp, newown);
122     }
123
124     sp->sct_avail = 0;
125     civ = sp->sct_item[I_CIVIL];
126     oldche = sp->sct_che;
127     /*
128      * create guerrillas from civilians
129      * how spunky are these guys?
130      * n: random number from -25:75 + (50 - loyalty)
131      */
132     n = (50 - sp->sct_loyal) + ((random() % 100) - 25);
133     che_count = 0;
134     if (n > 0 && sp->sct_own == sp->sct_oldown) {
135         che_count = (civ * n / 3000) + 5;
136         if (che_count * 2 > civ)
137             che_count = civ / 2;
138         che_count /= hap_fact(getnatp(newown), getnatp(sp->sct_own));
139         if (che_count + oldche > CHE_MAX)
140             che_count = CHE_MAX - oldche;
141         if (che_count > 0) {
142             civ -= che_count;
143             che_count += oldche;
144         } else
145             che_count = oldche;
146     } else
147         che_count = oldche;
148     sp->sct_che = che_count;
149     if (newown != sp->sct_oldown)
150         sp->sct_che_target = newown;
151     if (sp->sct_che_target == 0)
152         sp->sct_che = 0;
153     sp->sct_item[I_CIVIL] = civ;
154     if (sp->sct_oldown == newown || civ == 0) {
155         /*
156          * taking over one of your old sectors
157          */
158         sp->sct_loyal = 0;
159         sp->sct_oldown = newown;
160     } else {
161         /*
162          * taking over someone else's sector
163          */
164         sp->sct_loyal = 50;
165     }
166     sp->sct_own = newown;
167     if (opt_MOB_ACCESS) {
168         game_tick_to_now(&sp->sct_access);
169         sp->sct_mobil = -(etu_per_update / sect_mob_neg_factor);
170     } else {
171         sp->sct_mobil = 0;
172     }
173 }
174
175 void
176 takeover_plane(struct plnstr *pp, natid newown)
177 {
178     int n;
179
180     if ((pp->pln_own == newown) || (pp->pln_own == 0))
181         return;
182     if (pp->pln_flags & PLN_LAUNCHED)
183         return;
184     if (pp->pln_ship >= 0 || pp->pln_land >= 0)
185         return;
186     /*
187      * XXX If this was done right, planes could escape,
188      * flying to a nearby friendly airport.
189      */
190     n = pp->pln_effic - (30 + (random() % 100));
191     if (n < 0)
192         n = 0;
193     pp->pln_effic = n;
194     if (pp->pln_effic < PLANE_MINEFF || pp->pln_harden > 0) {
195         pp->pln_effic = 0;
196         mpr(newown, "%s blown up by the crew!\n", prplane(pp));
197         wu(0, pp->pln_own,
198            "%s blown up by the crew to avoid capture by %s at %s!\n",
199            prplane(pp),
200            cname(newown), xyas(pp->pln_x, pp->pln_y, pp->pln_own));
201     } else {
202         mpr(newown, "We have captured %s!\n", prplane(pp));
203         wu(0, pp->pln_own,
204            "%s captured by %s at %s!\n",
205            prplane(pp),
206            cname(newown), xyas(pp->pln_x, pp->pln_y, pp->pln_own));
207     }
208     takeover_unit((struct empobj *)pp, newown);
209 }
210
211 void
212 takeover_ship(struct shpstr *sp, natid newown)
213 {
214     takeover_unit((struct empobj *)sp, newown);
215 }
216
217 void
218 takeover_land(struct lndstr *landp, natid newown)
219 {
220     takeover_unit((struct empobj *)landp, newown);
221 }
222
223 static void
224 takeover_unit(struct empobj *unit, natid newown)
225 {
226     struct shpstr *sp;
227     struct plnstr *pp;
228     struct lndstr *lp;
229     struct nukstr *np;
230     int type;
231     struct nstr_item ni;
232     union empobj_storage cargo;
233
234     unit->own = newown;
235     if (opt_MARKET)
236         trdswitchown(unit->ef_type, unit, newown);
237     unit_wipe_orders(unit);
238
239     switch (unit->ef_type) {
240     case EF_SHIP:
241         sp = (struct shpstr *)unit;
242         sp->shp_off = 1;
243         break;
244     case EF_PLANE:
245         pp = (struct plnstr *)unit;
246         if (pp->pln_mobil > 0)
247             pp->pln_mobil = 0;
248         pp->pln_off = 1;
249         break;
250     case EF_LAND:
251         lp = (struct lndstr *)unit;
252         if (lp->lnd_mobil > 0)
253             lp->lnd_mobil = 0;
254         lp->lnd_off = 1;
255         lp->lnd_harden = 0;
256         break;
257     case EF_NUKE:
258         np = (struct nukstr *)unit;
259         np->nuk_off = 1;
260         break;
261     default:
262         CANT_REACH();
263     }
264
265     put_empobj(unit->ef_type, unit->uid, unit);
266
267     for (type = EF_PLANE; type <= EF_NUKE; type++) {
268         snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
269         while (nxtitem(&ni, &cargo)) {
270             if (cargo.gen.own == newown)
271                 continue;
272             if (type == EF_PLANE)
273                 cargo.plane.pln_effic = PLANE_MINEFF;
274             takeover_unit(&cargo.gen, newown);
275         }
276     }
277 }