]> git.pond.sub.org Git - empserver/blob - src/lib/update/revolt.c
update/revolt: Fix mil count for che move after getting caught
[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 void 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 > 0 && mil > 0) {
238             if (chance(odds)) {
239                 mc++;
240                 mil--;
241             } else {
242                 cc++;
243                 che--;
244             }
245         }
246         if (mil > 0) {
247             /* military won.  */
248             n = sp->sct_loyal - roll0(15);
249             if (n < 0)
250                 n = 0;
251             sp->sct_loyal = n;
252         } else {
253             convert++;
254             recruit++;
255         }
256         take_casualties(sp, 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 > 0 && n > 0) {
289                 if (chance(odds)) {
290                     mc++;
291                     n--;
292                 } else {
293                     cc++;
294                     che--;
295                 }
296             }
297             take_casualties(sp, mc);
298             mil -= mc;
299             recruit = 0;
300         }
301     }
302     if (convert && sp->sct_loyal >= 50) {
303         int n;
304         /* new owner gets to keep the mobility there */
305         oldmob = sp->sct_mobil;
306         /* che won, and sector converts. */
307         if (sp->sct_own == sp->sct_oldown)
308             sp->sct_oldown = 0;
309         else {
310             lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
311                            0, sp->sct_x, sp->sct_y);
312             takeover(sp, sp->sct_oldown);
313         }
314         sp->sct_mobil = oldmob;
315         civ += uw;
316         uw = 0;
317         n = civ / 20;
318         civ -= n;
319         if (civ > ITEM_MAX) {
320             uw = civ - ITEM_MAX;
321             civ = ITEM_MAX;
322         }
323         sp->sct_item[I_CIVIL] = civ;
324         sp->sct_item[I_UW] = uw;
325         sp->sct_item[I_MILIT] = n;
326         move++;
327         recruit = 0;
328         if (sp->sct_own)
329             wu(0, sp->sct_own, "Sector %s has been retaken!\n",
330                xyas(sp->sct_x, sp->sct_y, sp->sct_own));
331     }
332     if (recruit && che > 0) {
333         /* loyalty drops during recruitment efforts */
334         n = sp->sct_loyal;
335         if (n < 30)
336             n += roll(5);
337         else if (n < 70)
338             n += roll(10) + 3;
339         if (n > 127)
340             n = 127;
341         sp->sct_loyal = n;
342         if (sp->sct_oldown != sp->sct_own || n > 100) {
343             n = civ * roll0(3) / 200;
344             n /= hap_fact(tnat, getnatp(sp->sct_oldown));
345             if (n + che > CHE_MAX)
346                 n = CHE_MAX - che;
347             che += n;
348             civ -= n;
349             sp->sct_item[I_CIVIL] = civ;
350         }
351         n = uw * roll0(3) / 200;
352         if (n + che > CHE_MAX)
353             n = CHE_MAX - che;
354         che += n;
355         uw -= n;
356         sp->sct_item[I_UW] = uw;
357     }
358 domove:
359     if (move && che > 0) {
360         struct sctstr *nicest_sp = NULL;
361         if (convert)
362             min_mil = 999;
363         else
364             min_mil = mil;
365         /* search adjacent sectors for a nice one */
366         /* TODO consider land units in addition to mil */
367         for (n = 1; n <= 6; n++) {
368             nsp = getsectp(sp->sct_x + diroff[n][0],
369                            sp->sct_y + diroff[n][1]);
370             if (dchr[nsp->sct_type].d_mob0 < 0)
371                 continue;
372             if (nsp->sct_own != target)
373                 continue;
374             if (nsp->sct_che > 0) {
375                 if (nsp->sct_che_target != target)
376                     continue;
377                 if (nsp->sct_che + che > CHE_MAX)
378                     continue;
379             }
380             val = nsp->sct_item[I_MILIT];
381             /* don't give che more precise info than spy */
382             val = roundintby(val, 10);
383             /* inject a modicum of indeterminism; also
384              * avoids che preferring certain directions */
385             val += roll(10) - 6;
386             if (val >= min_mil)
387                 continue;
388             nicest_sp = nsp;
389             min_mil = val;
390         }
391         /* if we found a nice sector, go there */
392         if (nicest_sp) {
393             nicest_sp->sct_che += che;
394             nicest_sp->sct_che_target = target;
395             che = 0;
396         }
397     }
398     if (che > 0) {
399         sp->sct_che = che;
400         sp->sct_che_target = target;
401     } else {
402         sp->sct_che = 0;
403         sp->sct_che_target = 0;
404     }
405     if (mc > 0 || cc > 0) {
406         wu(0, target,
407            "Guerrilla warfare in %s\n",
408            xyas(sp->sct_x, sp->sct_y, target));
409         if (sp->sct_own == target)
410             wu(0, target, "  body count: troops: %d, rebels: %d\n", mc, cc);
411         else
412             wu(0, target, "  rebels murder %d military\n", mc);
413         nreport(actor, N_FREEDOM_FIGHT, victim, 1);
414     }
415     if (sp->sct_own != victim)
416         wu(0, victim, "Partisans take over %s!\n",
417            xyas(sp->sct_x, sp->sct_y, victim));
418 }
419
420 static void
421 take_casualties(struct sctstr *sp, int mc)
422 {
423     int orig_mil;
424     int cantake;
425     int nunits = 0, each, deq;
426     struct lndstr *lp;
427     struct nstr_item ni;
428
429     /* casualties come out of mil first */
430     orig_mil = sp->sct_item[I_MILIT];
431
432     if (mc <= orig_mil) {
433         sp->sct_item[I_MILIT] = orig_mil - mc;
434         return;
435     }
436     sp->sct_item[I_MILIT] = 0;
437
438     /* remaining casualites */
439     mc -= orig_mil;
440
441     /*
442      * Need to take total_casualties and divide
443      * them amongst the land units in the sector
444      * Do security troops first, then others.
445      * Try not to kill any unit.
446      */
447     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
448     while (NULL != (lp = nxtitemp(&ni))) {
449         if (lp->lnd_own != sp->sct_own)
450             continue;
451         if (lp->lnd_ship >= 0)
452             continue;
453         nunits++;
454     }
455
456     if (nunits == 0)
457         return;
458
459     each = (mc / nunits) + 2;
460
461     /* kill some security troops */
462     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
463     while (NULL != (lp = nxtitemp(&ni))) {
464         if (lp->lnd_own != sp->sct_own)
465             continue;
466         if (lp->lnd_ship >= 0)
467             continue;
468         if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
469             continue;
470
471         cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
472
473         if (cantake >= each) {
474             deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
475             mc -= each;
476         } else if (cantake > 0) {
477             deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
478             mc -= cantake;
479         } else
480             deq = 0;
481
482         lp->lnd_effic -= deq;
483         lp->lnd_mobil -= deq / 2;
484         deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
485         lnd_submil(lp, deq);
486         if (mc <= 0)
487             return;
488     }
489
490     /* kill some normal troops */
491     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
492     while (NULL != (lp = nxtitemp(&ni))) {
493         if (lp->lnd_own != sp->sct_own)
494             continue;
495         if (lp->lnd_ship >= 0)
496             continue;
497         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
498             continue;
499
500         cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
501
502         if (cantake >= each) {
503             deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
504             mc -= each;
505         } else if (cantake > 0) {
506             deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
507             mc -= cantake;
508         } else
509             deq = 0;
510
511         lp->lnd_effic -= deq;
512         lp->lnd_mobil -= deq / 2;
513         deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
514         lnd_submil(lp, deq);
515         if (mc <= 0)
516             return;
517     }
518
519     /* Hmm.. still some left.. kill off units now */
520     /* kill some normal troops */
521     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
522     while (NULL != (lp = nxtitemp(&ni))) {
523         if (lp->lnd_own != sp->sct_own)
524             continue;
525         if (lp->lnd_ship >= 0)
526             continue;
527         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
528             continue;
529
530         mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
531         lnd_dies_fighting_che(lp);
532         if (mc <= 0)
533             return;
534     }
535
536     /* Hmm.. still some left.. kill off units now */
537     /* kill some security troops */
538     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
539     while (NULL != (lp = nxtitemp(&ni))) {
540         if (lp->lnd_own != sp->sct_own)
541             continue;
542         if (lp->lnd_ship >= 0)
543             continue;
544         if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
545             continue;
546
547         mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
548         lnd_dies_fighting_che(lp);
549         if (mc <= 0)
550             return;
551     }
552
553     /* Hmm.. everyone dead.. too bad */
554 }
555
556 static void
557 lnd_dies_fighting_che(struct lndstr *lp)
558 {
559     int i, j;
560     struct lndstr *clp;
561     struct plnstr *cpp;
562     struct nukstr *cnp;
563
564     lp->lnd_effic = 0;
565     lnd_submil(lp, 1000);       /* Remove 'em all */
566     wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
567        prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
568     makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
569     lp->lnd_own = 0;
570
571     /* Take dead lp off its carrier */
572     if (lp->lnd_land >= 0) {
573         lnd_carrier_change(lp, EF_LAND, lp->lnd_land, -1);
574         lp->lnd_land = -1;
575     }
576
577     /* Unload lp's land unit cargo */
578     for (i = lnd_first_on_land(lp); i >= 0; i = lnd_next_on_unit(i)) {
579         clp = getlandp(i);
580         if (CANT_HAPPEN(!clp))
581             continue;
582         lnd_carrier_change(clp, EF_LAND, clp->lnd_land, -1);
583         clp->lnd_land = -1;
584     }
585
586     /* Destroy lp's plane cargo */
587     for (i = pln_first_on_land(lp); i >= 0; i = pln_next_on_unit(i)) {
588         cpp = getplanep(i);
589         if (CANT_HAPPEN(!cpp))
590             continue;
591         pln_carrier_change(cpp, EF_LAND, cpp->pln_land, -1);
592         makelost(EF_PLANE, cpp->pln_own, i, cpp->pln_x, cpp->pln_y);
593         wu(0, cpp->pln_own, "%s lost!\n", prplane(cpp));
594         cpp->pln_own = 0;
595         cpp->pln_effic = 0;
596         cpp->pln_land = -1;
597
598         j = nuk_on_plane(cpp);
599         if (j >= 0) {
600             cnp = getnukep(j);
601             if (CANT_HAPPEN(!cnp))
602                 continue;
603             nuk_carrier_change(cnp, EF_PLANE, cnp->nuk_plane, -1);
604             makelost(EF_NUKE, cnp->nuk_own, j, cnp->nuk_x, cnp->nuk_y);
605             wu(0, cnp->nuk_own, "%s lost!\n", prnuke(cnp));
606             cnp->nuk_own = 0;
607             cnp->nuk_effic = 0;
608             cnp->nuk_plane = -1;
609         }
610     }
611 }