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