]> git.pond.sub.org Git - empserver/blob - src/lib/update/revolt.c
update/revolt: Fix how land units take casualties
[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, dam;
423     struct lndstr *lp;
424     struct nstr_item ni;
425
426     /* casualties come out of mil first */
427     orig_mil = sp->sct_item[I_MILIT];
428
429     if (mc <= orig_mil) {
430         sp->sct_item[I_MILIT] = orig_mil - mc;
431         return mc;
432     }
433     sp->sct_item[I_MILIT] = 0;
434
435     /* remaining casualites */
436     mc -= orig_mil;
437     taken = orig_mil;
438
439     /*
440      * Need to take total_casualties and divide
441      * them amongst the land units in the sector
442      * Do security troops first, then others.
443      * Try not to kill any unit.
444      */
445     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
446     while (NULL != (lp = nxtitemp(&ni))) {
447         if (lp->lnd_own != sp->sct_own)
448             continue;
449         if (lp->lnd_ship >= 0)
450             continue;
451         nunits++;
452     }
453
454     if (CANT_HAPPEN(!nunits))
455         return taken;
456
457     each = (mc / nunits) + 2;
458
459     /* kill some security troops */
460     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
461     while (NULL != (lp = nxtitemp(&ni))) {
462         if (lp->lnd_own != sp->sct_own)
463             continue;
464         if (lp->lnd_ship >= 0)
465             continue;
466         if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
467             continue;
468
469         cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
470         cantake = MIN(lp->lnd_item[I_MILIT], cantake);
471         deq = MIN(cantake, each);
472         if (deq <= 0)
473             continue;
474
475         mc -= deq;
476         taken += deq;
477         dam = ((double)deq / lp->lnd_item[I_MILIT]) * 100.0;
478         lp->lnd_effic -= dam;
479         lp->lnd_mobil -= dam / 2;
480         lnd_submil(lp, deq);
481         if (mc <= 0)
482             return taken;
483     }
484
485     /* kill some normal troops */
486     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
487     while (NULL != (lp = nxtitemp(&ni))) {
488         if (lp->lnd_own != sp->sct_own)
489             continue;
490         if (lp->lnd_ship >= 0)
491             continue;
492         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
493             continue;
494
495         cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
496         cantake = MIN(lp->lnd_item[I_MILIT], cantake);
497         deq = MIN(cantake, each);
498         if (deq <= 0)
499             continue;
500
501         mc -= deq;
502         taken += deq;
503         dam = ((double)deq / lp->lnd_item[I_MILIT]) * 100.0;
504         lp->lnd_effic -= dam;
505         lp->lnd_mobil -= dam / 2;
506         lnd_submil(lp, deq);
507         if (mc <= 0)
508             return taken;
509     }
510
511     /* Hmm.. still some left.. kill off units now */
512     /* kill some normal troops */
513     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
514     while (NULL != (lp = nxtitemp(&ni))) {
515         if (lp->lnd_own != sp->sct_own)
516             continue;
517         if (lp->lnd_ship >= 0)
518             continue;
519         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
520             continue;
521
522         mc -= lp->lnd_item[I_MILIT];
523         taken += lp->lnd_item[I_MILIT];
524         lnd_dies_fighting_che(lp);
525         if (mc <= 0)
526             return taken;
527     }
528
529     /* Hmm.. still some left.. kill off units now */
530     /* kill some security troops */
531     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
532     while (NULL != (lp = nxtitemp(&ni))) {
533         if (lp->lnd_own != sp->sct_own)
534             continue;
535         if (lp->lnd_ship >= 0)
536             continue;
537         if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
538             continue;
539
540         mc -= lp->lnd_item[I_MILIT];
541         taken += lp->lnd_item[I_MILIT];
542         lnd_dies_fighting_che(lp);
543         if (mc <= 0)
544             return taken;
545     }
546
547     CANT_REACH();
548     return taken;
549 }
550
551 static void
552 lnd_dies_fighting_che(struct lndstr *lp)
553 {
554     int i, j;
555     struct lndstr *clp;
556     struct plnstr *cpp;
557     struct nukstr *cnp;
558
559     lp->lnd_effic = 0;
560     lnd_submil(lp, 1000);       /* Remove 'em all */
561     wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
562        prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
563     makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
564     lp->lnd_own = 0;
565
566     /* Take dead lp off its carrier */
567     if (lp->lnd_land >= 0) {
568         lnd_carrier_change(lp, EF_LAND, lp->lnd_land, -1);
569         lp->lnd_land = -1;
570     }
571
572     /* Unload lp's land unit cargo */
573     for (i = lnd_first_on_land(lp); i >= 0; i = lnd_next_on_unit(i)) {
574         clp = getlandp(i);
575         if (CANT_HAPPEN(!clp))
576             continue;
577         lnd_carrier_change(clp, EF_LAND, clp->lnd_land, -1);
578         clp->lnd_land = -1;
579     }
580
581     /* Destroy lp's plane cargo */
582     for (i = pln_first_on_land(lp); i >= 0; i = pln_next_on_unit(i)) {
583         cpp = getplanep(i);
584         if (CANT_HAPPEN(!cpp))
585             continue;
586         pln_carrier_change(cpp, EF_LAND, cpp->pln_land, -1);
587         makelost(EF_PLANE, cpp->pln_own, i, cpp->pln_x, cpp->pln_y);
588         wu(0, cpp->pln_own, "%s lost!\n", prplane(cpp));
589         cpp->pln_own = 0;
590         cpp->pln_effic = 0;
591         cpp->pln_land = -1;
592
593         j = nuk_on_plane(cpp);
594         if (j >= 0) {
595             cnp = getnukep(j);
596             if (CANT_HAPPEN(!cnp))
597                 continue;
598             nuk_carrier_change(cnp, EF_PLANE, cnp->nuk_plane, -1);
599             makelost(EF_NUKE, cnp->nuk_own, j, cnp->nuk_x, cnp->nuk_y);
600             wu(0, cnp->nuk_own, "%s lost!\n", prnuke(cnp));
601             cnp->nuk_own = 0;
602             cnp->nuk_effic = 0;
603             cnp->nuk_plane = -1;
604         }
605     }
606 }