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