]> git.pond.sub.org Git - empserver/blob - src/lib/update/revolt.c
Update copyright notice
[empserver] / src / lib / update / revolt.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2018, 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  *  revolt.c: Have disloyal populace revolt!
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1986
31  *     Steve McClure, 1997-2000
32  *     Markus Armbruster, 2004-2016
33  */
34
35 #include <config.h>
36
37 #include "chance.h"
38 #include "land.h"
39 #include "lost.h"
40 #include "nat.h"
41 #include "news.h"
42 #include "nsc.h"
43 #include "nuke.h"
44 #include "path.h"
45 #include "plane.h"
46 #include "prototypes.h"
47 #include "sect.h"
48 #include "update.h"
49
50 static int take_casualties(struct sctstr *, int);
51 static int take_casualties_from_lands(struct sctstr *, int, int, int);
52 static void lnd_dies_fighting_che(struct lndstr *);
53
54 void
55 revolt(struct sctstr *sp)
56 {
57     int che_civ;
58     int che_uw;
59     int civ;
60     int uw;
61     int che;
62     int n;
63
64     che = sp->sct_che;
65     if (che != 0 && (sp->sct_che_target != sp->sct_own || che >= CHE_MAX))
66         return;
67     civ = sp->sct_item[I_CIVIL];
68     uw = sp->sct_item[I_UW];
69     if (che > (civ + uw) * 3)
70         return;
71     che_uw = 0;
72     /* che due to civilian unrest */
73     n = 10 - roll0(20);
74     che_civ = 3 + (civ * n / 500);
75     if (che_civ < 0)
76         che_civ = 0;
77     else if (che_civ * 3 > civ)
78         che_civ = civ / 3;
79     if (che + che_civ > CHE_MAX)
80         che_civ = CHE_MAX - che;
81     che += che_civ;
82     if (che < CHE_MAX) {
83         /* che due to uw unrest */
84         n = 9 + roll(30);
85         che_uw = 5 + (uw * n / 500);
86         if (che_uw > uw)
87             che_uw = uw;
88         if (che + che_uw > CHE_MAX)
89             che_uw = CHE_MAX - che_uw;
90         che += che_uw;
91     }
92     if (che_civ + che_uw > 0) {
93         civ -= che_civ;
94         uw -= che_uw;
95         sp->sct_che_target = sp->sct_own;
96         sp->sct_che = che;
97         if (che_civ > 0)
98             sp->sct_item[I_CIVIL] = civ;
99         if (che_uw > 0)
100             sp->sct_item[I_UW] = uw;
101     }
102 }
103
104 /*
105  * summary of effects.
106  * if there are no military in the sector, che recruit from
107  *   populace if pop loyalty is > 10.  They spread subversion otherwise,
108  *   trying to lower pop loyalty.
109  * if che outnumber military, they stay and shoot it out, kill the
110  *   military.
111  * if che are outnumbered by less than 5 to 1, they blow up stuff,
112  *   killing innocent civilians (never uw's) and damaging commodities.
113  * if che are outnumbered by more than 5 to 1, they try to leave the
114  *   sector for a nearby sector with fewer military.
115  *
116  * if the military lose any attacks, the pop loyalty in the sector
117  *   gets worse, representing military defeat.
118  * military can "catch" che's after bombing attacks, or after they move.
119  *   If military catch them, then they get to shoot it out with a portion
120  *   of the che's depending on the # of mil in the sector.  Chance to contact
121  *   is around 10% per every equal number of mil:che ratio in the sector.
122  *   "contact" is by 20% of the military in the sector, and odds are equal.
123  *
124  * Without a doubt this routine should be broken up, if only for readabilty.
125  */
126 void
127 guerrilla(struct sctstr *sp)
128 {
129     struct sctstr *nsp;
130     int recruit;
131     int move;
132     double ratio;
133     int che;
134     int mil;
135     double security_bonus;
136     int cc, mc;
137     double odds;
138     int civ;
139     int n;
140     int uw;
141     natid target;
142     struct natstr *tnat;
143     int convert;
144     natid actor;
145     natid victim;
146     int tmp;
147     int min_mil;
148     int val;
149     int oldmob;
150     struct lndstr *lp;
151     struct nstr_item ni;
152
153     mc = cc = 0;
154     recruit = 0;
155     convert = 0;
156     move = 0;
157     if (!sp->sct_che)
158         return;
159     civ = sp->sct_item[I_CIVIL];
160     uw = sp->sct_item[I_UW];
161     victim = sp->sct_own;
162     actor = sp->sct_oldown;
163     che = sp->sct_che;
164     mil = sp->sct_item[I_MILIT];
165     security_bonus = 0;
166
167     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
168
169     while (NULL != (lp = nxtitemp(&ni))) {
170         if (lp->lnd_own != sp->sct_own)
171             continue;
172         if (lp->lnd_ship >= 0)
173             continue;
174
175         mil += lp->lnd_item[I_MILIT];
176
177         if (sp->sct_che_target != sp->sct_own)
178             continue;
179
180         /* Security troops can now kill up to 1/5 their complement each
181            update, before doing anything else. */
182         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
183             int che_kill, r;
184
185             security_bonus += 3 * lp->lnd_item[I_MILIT] * lp->lnd_effic / 100.0;
186             r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
187             che_kill = r < 1 ? 0 : roll(r);
188             if (che_kill > che)
189                 che_kill = che;
190             if (che_kill) {
191                 wu(0, sp->sct_own,
192                    "%s kills %d guerrilla%s in raid at %s!\n",
193                    prland(lp), che_kill, splur(che_kill), ownxy(sp));
194                 che -= che_kill;
195             }
196         }
197     }
198
199     /* Security forces killed all the che */
200     if (che <= 0) {
201         sp->sct_che = 0;
202         sp->sct_che_target = 0;
203         return;
204     }
205
206     target = sp->sct_che_target;
207     if (CANT_HAPPEN(target == 0))
208         return;
209     tnat = getnatp(target);
210     if (tnat->nat_stat == STAT_UNUSED) {
211         /* target nation has dissolved: che's retire. */
212         logerror("%d Che targeted at country %d retiring", che, target);
213         sp->sct_che = 0;
214         sp->sct_che_target = 0;
215         sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
216         return;
217     }
218
219     if (sp->sct_own != target) {
220         move++;
221         goto domove;
222     }
223
224     ratio = (mil + security_bonus) / che;
225     odds = (double)che / (mil + security_bonus + che);
226     odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
227     if (mil == 0) {
228         wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
229            ownxy(sp));
230         recruit++;
231         convert++;
232     } else if (che > mil && mil > 0) {
233         /*
234          * shoot it out with the military, and kill them off.
235          * If loyalty bad enough, then take the sector over,
236          * and enlist 5% of civ as military force.
237          */
238         while (che > cc && mil > mc) {
239             if (chance(odds))
240                 mc++;
241             else
242                 cc++;
243         }
244         if (mil > mc) {
245             /* military won.  */
246             n = sp->sct_loyal - roll0(15);
247             if (n < 0)
248                 n = 0;
249             sp->sct_loyal = n;
250         } else {
251             convert++;
252             recruit++;
253         }
254         mc = take_casualties(sp, mc);
255         che -= cc;
256         mil -= mc;
257     } else if (ratio < 5) {
258         /*
259          * guerrillas have to resort to blowing things up.
260          * Note this disrupts work in the sector.
261          */
262         n = roll0(10) + roll0(che);
263         if (n > 100)
264             n = 100;
265         tmp = sp->sct_work - n;
266         if (tmp < 0)
267             tmp = 0;
268         sp->sct_work = tmp;
269         wu(0, sp->sct_own,
270            "Production %s disrupted by terrorists in %s\n",
271            effadv(n), ownxy(sp));
272         sect_damage(sp, n / 10);
273         recruit++;
274     } else {
275         /* ratio >= 5 */
276         move++;
277     }
278     if (mil > 0 && che > 0) {
279         /*
280          * we only get here if we haven't had combat previously.
281          * Chance to catch them.
282          * 20% of mil involved in attacking the che's.
283          */
284         if (chance(ratio * 0.10)) {
285             n = (mil / 5) + 1;
286             odds = (double)che / (n + security_bonus / 5 + che);
287             odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
288             while (che > cc && n > mc) {
289                 if (chance(odds))
290                     mc++;
291                 else
292                     cc++;
293             }
294             mc = take_casualties(sp, mc);
295             che -= cc;
296             mil -= mc;
297             recruit = 0;
298         }
299     }
300     if (convert && sp->sct_loyal >= 50) {
301         int n;
302         /* new owner gets to keep the mobility there */
303         oldmob = sp->sct_mobil;
304         /* che won, and sector converts. */
305         if (sp->sct_own == sp->sct_oldown)
306             sp->sct_oldown = 0;
307         lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
308                        0, sp->sct_x, sp->sct_y);
309         takeover(sp, sp->sct_oldown);
310         sp->sct_mobil = oldmob;
311         civ += uw;
312         uw = 0;
313         n = civ / 20;
314         civ -= n;
315         if (civ > ITEM_MAX) {
316             uw = civ - ITEM_MAX;
317             civ = ITEM_MAX;
318         }
319         sp->sct_item[I_CIVIL] = civ;
320         sp->sct_item[I_UW] = uw;
321         sp->sct_item[I_MILIT] = n;
322         move++;
323         recruit = 0;
324         if (sp->sct_own)
325             wu(0, sp->sct_own, "Sector %s has been retaken!\n",
326                xyas(sp->sct_x, sp->sct_y, sp->sct_own));
327     }
328     if (recruit && che > 0) {
329         /* loyalty drops during recruitment efforts */
330         n = sp->sct_loyal;
331         if (n < 30)
332             n += roll(5);
333         else if (n < 70)
334             n += roll(10) + 3;
335         if (n > 127)
336             n = 127;
337         sp->sct_loyal = n;
338         if (sp->sct_oldown != sp->sct_own || n > 100) {
339             n = civ * roll0(3) / 200;
340             n /= hap_fact(tnat, getnatp(sp->sct_oldown));
341             if (n + che > CHE_MAX)
342                 n = CHE_MAX - che;
343             che += n;
344             civ -= n;
345             sp->sct_item[I_CIVIL] = civ;
346         }
347         n = uw * roll0(3) / 200;
348         if (n + che > CHE_MAX)
349             n = CHE_MAX - che;
350         che += n;
351         uw -= n;
352         sp->sct_item[I_UW] = uw;
353     }
354 domove:
355     if (move && che > 0) {
356         struct sctstr *nicest_sp = NULL;
357         if (convert)
358             min_mil = 999;
359         else
360             min_mil = mil;
361         /* search adjacent sectors for a nice one */
362         /* TODO consider land units in addition to mil */
363         for (n = 1; n <= 6; n++) {
364             nsp = getsectp(sp->sct_x + diroff[n][0],
365                            sp->sct_y + diroff[n][1]);
366             if (dchr[nsp->sct_type].d_mob0 < 0)
367                 continue;
368             if (nsp->sct_own != target)
369                 continue;
370             if (nsp->sct_che > 0) {
371                 if (nsp->sct_che_target != target)
372                     continue;
373                 if (nsp->sct_che + che > CHE_MAX)
374                     continue;
375             }
376             val = nsp->sct_item[I_MILIT];
377             /* don't give che more precise info than spy */
378             val = roundintby(val, 10);
379             /* inject a modicum of indeterminism; also
380              * avoids che preferring certain directions */
381             val += roll(10) - 6;
382             if (val >= min_mil)
383                 continue;
384             nicest_sp = nsp;
385             min_mil = val;
386         }
387         /* if we found a nice sector, go there */
388         if (nicest_sp) {
389             nicest_sp->sct_che += che;
390             nicest_sp->sct_che_target = target;
391             che = 0;
392         }
393     }
394     if (che > 0) {
395         sp->sct_che = che;
396         sp->sct_che_target = target;
397     } else {
398         sp->sct_che = 0;
399         sp->sct_che_target = 0;
400     }
401     if (mc > 0 || cc > 0) {
402         wu(0, target,
403            "Guerrilla warfare in %s\n",
404            xyas(sp->sct_x, sp->sct_y, target));
405         if (sp->sct_own == target)
406             wu(0, target, "  body count: troops: %d, rebels: %d\n", mc, cc);
407         else
408             wu(0, target, "  rebels murder %d military\n", mc);
409         nreport(actor, N_FREEDOM_FIGHT, victim, 1);
410     }
411     if (sp->sct_own != victim)
412         wu(0, victim, "Partisans take over %s!\n",
413            xyas(sp->sct_x, sp->sct_y, victim));
414 }
415
416 static int
417 take_casualties(struct sctstr *sp, int mc)
418 {
419     int orig_mil, taken;
420     int nunits = 0, each;
421     struct lndstr *lp;
422     struct nstr_item ni;
423
424     /* casualties come out of mil first */
425     orig_mil = sp->sct_item[I_MILIT];
426
427     if (mc <= orig_mil) {
428         sp->sct_item[I_MILIT] = orig_mil - mc;
429         return mc;
430     }
431     sp->sct_item[I_MILIT] = 0;
432
433     /*
434      * Need to take total_casualties and divide
435      * them amongst the land units in the sector
436      * Do security troops first, then others.
437      * Try not to kill any unit.
438      * TODO Spread proportionally to mil instead of evenly
439      */
440     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
441     while (NULL != (lp = nxtitemp(&ni))) {
442         if (lp->lnd_own != sp->sct_own)
443             continue;
444         if (lp->lnd_ship >= 0)
445             continue;
446         if (!lp->lnd_item[I_MILIT])
447             continue;
448         nunits++;
449     }
450
451     if (CANT_HAPPEN(!nunits))
452         return orig_mil;
453
454     taken = orig_mil;
455     each = (mc - taken) / nunits + 2;
456
457     /* kill some security troops */
458     taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 1, 0);
459
460     /* kill some normal troops */
461     taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 0, 0);
462
463     /* Hmm.. still some left.. kill off units now */
464     /* kill some normal troops */
465     taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 0, 1);
466
467     /* Hmm.. still some left.. kill off units now */
468     /* kill some security troops */
469     taken += take_casualties_from_lands(sp, MIN(each, mc - taken), 1, 1);
470
471     CANT_HAPPEN(taken < mc);
472     return taken;
473 }
474
475 int
476 take_casualties_from_lands(struct sctstr *sp, int cas,
477                            int security, int may_kill)
478 {
479     struct nstr_item ni;
480     struct lndstr *lp;
481     double eff_per_cas;
482     int cantake, deq, taken;
483
484     taken = 0;
485     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
486     while (taken < cas && (lp = nxtitemp(&ni))) {
487         if (lp->lnd_own != sp->sct_own)
488             continue;
489         if (lp->lnd_ship >= 0)
490             continue;
491         if (!lp->lnd_item[I_MILIT])
492             continue;
493         if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY) == !!security)
494             continue;
495
496         eff_per_cas = 100.0 / lchr[lp->lnd_type].l_item[I_MILIT];
497         cantake = lp->lnd_item[I_MILIT];
498         if (!may_kill)
499             cantake = MIN(cantake,
500                           (int)((lp->lnd_effic - 40) / eff_per_cas));
501         deq = MIN(cantake, cas - taken);
502         if (deq <= 0)
503             continue;
504
505         taken += deq;
506         lp->lnd_effic -= deq * eff_per_cas;
507         lp->lnd_mobil -= deq * eff_per_cas / 2;
508         lnd_submil(lp, deq);
509         if (lp->lnd_effic < LAND_MINEFF) {
510             CANT_HAPPEN(!may_kill);
511             taken += lp->lnd_item[I_MILIT];
512             lnd_dies_fighting_che(lp);
513         }
514     }
515
516     return taken;
517 }
518
519 static void
520 lnd_dies_fighting_che(struct lndstr *lp)
521 {
522     int i, j;
523     struct lndstr *clp;
524     struct plnstr *cpp;
525     struct nukstr *cnp;
526
527     lp->lnd_effic = 0;
528     lnd_submil(lp, 1000);       /* Remove 'em all */
529     wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
530        prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
531     makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
532     lp->lnd_own = 0;
533
534     /* Take dead lp off its carrier */
535     if (lp->lnd_land >= 0) {
536         lnd_carrier_change(lp, EF_LAND, lp->lnd_land, -1);
537         lp->lnd_land = -1;
538     }
539
540     /* Unload lp's land unit cargo */
541     for (i = lnd_first_on_land(lp); i >= 0; i = lnd_next_on_unit(i)) {
542         clp = getlandp(i);
543         if (CANT_HAPPEN(!clp))
544             continue;
545         lnd_carrier_change(clp, EF_LAND, clp->lnd_land, -1);
546         clp->lnd_land = -1;
547     }
548
549     /* Destroy lp's plane cargo */
550     for (i = pln_first_on_land(lp); i >= 0; i = pln_next_on_unit(i)) {
551         cpp = getplanep(i);
552         if (CANT_HAPPEN(!cpp))
553             continue;
554         pln_carrier_change(cpp, EF_LAND, cpp->pln_land, -1);
555         makelost(EF_PLANE, cpp->pln_own, i, cpp->pln_x, cpp->pln_y);
556         wu(0, cpp->pln_own, "%s lost!\n", prplane(cpp));
557         cpp->pln_own = 0;
558         cpp->pln_effic = 0;
559         cpp->pln_land = -1;
560
561         j = nuk_on_plane(cpp);
562         if (j >= 0) {
563             cnp = getnukep(j);
564             if (CANT_HAPPEN(!cnp))
565                 continue;
566             nuk_carrier_change(cnp, EF_PLANE, cnp->nuk_plane, -1);
567             makelost(EF_NUKE, cnp->nuk_own, j, cnp->nuk_x, cnp->nuk_y);
568             wu(0, cnp->nuk_own, "%s lost!\n", prnuke(cnp));
569             cnp->nuk_own = 0;
570             cnp->nuk_effic = 0;
571             cnp->nuk_plane = -1;
572         }
573     }
574 }