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