]> git.pond.sub.org Git - empserver/blob - src/lib/update/revolt.c
Include "file.h" where it's needed
[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-2012
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 cc, mc;
135     double odds;
136     int civ;
137     int n;
138     int uw;
139     natid target;
140     struct natstr *tnat;
141     int convert;
142     natid actor;
143     natid victim;
144     int tmp;
145     int min_mil;
146     int val;
147     int oldmob;
148     struct lndstr *lp;
149     struct nstr_item ni;
150
151     mc = cc = 0;
152     recruit = 0;
153     convert = 0;
154     move = 0;
155     if (!sp->sct_che)
156         return;
157     civ = sp->sct_item[I_CIVIL];
158     uw = sp->sct_item[I_UW];
159     victim = sp->sct_own;
160     actor = sp->sct_oldown;
161     che = sp->sct_che;
162     mil = sp->sct_item[I_MILIT];
163
164     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
165
166     while (NULL != (lp = nxtitemp(&ni))) {
167         if (lp->lnd_own != sp->sct_own)
168             continue;
169         if (lp->lnd_ship >= 0)
170             continue;
171
172         mil += lp->lnd_item[I_MILIT];
173
174         if (sp->sct_che_target != sp->sct_own)
175             continue;
176
177         /* Security troops can now kill up to 1/5 their complement each
178            update, before doing anything else. */
179         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
180             int che_kill, r;
181
182             mil += lp->lnd_item[I_MILIT];
183             r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
184             che_kill = r < 1 ? 0 : roll(r);
185             if (che_kill > che)
186                 che_kill = che;
187             if (che_kill) {
188                 wu(0, sp->sct_own,
189                    "%s kills %d guerrilla%s in raid at %s!\n",
190                    prland(lp), che_kill, splur(che_kill), ownxy(sp));
191                 che -= che_kill;
192             }
193         }
194     }
195
196     /* Security forces killed all the che */
197     if (che <= 0) {
198         sp->sct_che = 0;
199         sp->sct_che_target = 0;
200         return;
201     }
202
203     target = sp->sct_che_target;
204     if (CANT_HAPPEN(target == 0))
205         return;
206     tnat = getnatp(target);
207     if (tnat->nat_stat == STAT_UNUSED) {
208         /* target nation has dissolved: che's retire. */
209         logerror("%d Che targeted at country %d retiring", che, target);
210         sp->sct_che = 0;
211         sp->sct_che_target = 0;
212         sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
213         return;
214     }
215
216     if (sp->sct_own != target) {
217         move++;
218         goto domove;
219     }
220
221     ratio = mil / che;
222     odds = (double)che / (mil + che);
223     odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
224     if (mil == 0) {
225         wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
226            ownxy(sp));
227         recruit++;
228         convert++;
229     } else if (che > mil && mil > 0) {
230         /*
231          * shoot it out with the military, and kill them off.
232          * If loyalty bad enough, then take the sector over,
233          * and enlist 5% of civ as military force.
234          */
235         while (che > 0 && mil > 0) {
236             if (chance(odds)) {
237                 mc++;
238                 mil--;
239             } else {
240                 cc++;
241                 che--;
242             }
243         }
244         if (mil > 0) {
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         take_casualties(sp, mc);
255     } else if (ratio < 5) {
256         /*
257          * guerrillas have to resort to blowing things up.
258          * Note this disrupts work in the sector.
259          */
260         n = roll0(10) + roll0(che);
261         if (n > 100)
262             n = 100;
263         tmp = sp->sct_work - n;
264         if (tmp < 0)
265             tmp = 0;
266         sp->sct_work = tmp;
267         wu(0, sp->sct_own,
268            "Production %s disrupted by terrorists in %s\n",
269            effadv(n), ownxy(sp));
270         sect_damage(sp, n / 10);
271         recruit++;
272     } else {
273         /* ratio >= 5 */
274         move++;
275     }
276     if (mil > 0 && che > 0) {
277         /*
278          * we only get here if we haven't had combat previously.
279          * Chance to catch them.
280          * 20% of mil involved in attacking the che's.
281          */
282         if (chance(ratio * 0.10)) {
283             n = (mil / 5) + 1;
284             odds = (double)che / (n + che);
285             odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
286             while (che > 0 && n > 0) {
287                 if (chance(odds)) {
288                     mc++;
289                     n--;
290                 } else {
291                     cc++;
292                     che--;
293                 }
294             }
295             take_casualties(sp, 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         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 void
417 take_casualties(struct sctstr *sp, int mc)
418 {
419     int orig_mil;
420     int cantake;
421     int nunits = 0, each, deq;
422     struct lndstr *lp;
423     struct nstr_item ni;
424
425     /* casualties come out of mil first */
426     orig_mil = sp->sct_item[I_MILIT];
427
428     if (mc <= orig_mil) {
429         sp->sct_item[I_MILIT] = orig_mil - mc;
430         return;
431     }
432     sp->sct_item[I_MILIT] = 0;
433
434     /* remaining casualites */
435     mc -= orig_mil;
436
437     /*
438      * Need to take total_casualties and divide
439      * them amongst the land units in the sector
440      * Do security troops first, then others.
441      * Try not to kill any unit.
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         nunits++;
450         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
451             nunits++;
452     }
453
454     if (nunits == 0)
455         return;
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
471         if (cantake >= each) {
472             deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
473             mc -= 2 * each;
474         } else if (cantake > 0) {
475             deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
476             mc -= 2 * cantake;
477         } else
478             deq = 0;
479
480         lp->lnd_effic -= deq;
481         lp->lnd_mobil -= deq / 2;
482         deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
483         lnd_submil(lp, deq);
484         if (mc <= 0)
485             return;
486     }
487
488     /* kill some normal troops */
489     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
490     while (NULL != (lp = nxtitemp(&ni))) {
491         if (lp->lnd_own != sp->sct_own)
492             continue;
493         if (lp->lnd_ship >= 0)
494             continue;
495         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
496             continue;
497
498         cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
499
500         if (cantake >= each) {
501             deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
502             mc -= each;
503         } else if (cantake > 0) {
504             deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
505             mc -= cantake;
506         } else
507             deq = 0;
508
509         lp->lnd_effic -= deq;
510         lp->lnd_mobil -= deq / 2;
511         deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
512         lnd_submil(lp, deq);
513         if (mc <= 0)
514             return;
515     }
516
517     /* Hmm.. still some left.. kill off units now */
518     /* kill some normal troops */
519     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
520     while (NULL != (lp = nxtitemp(&ni))) {
521         if (lp->lnd_own != sp->sct_own)
522             continue;
523         if (lp->lnd_ship >= 0)
524             continue;
525         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
526             continue;
527
528         mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
529         lnd_dies_fighting_che(lp);
530         if (mc <= 0)
531             return;
532     }
533
534     /* Hmm.. still some left.. kill off units now */
535     /* kill some security troops */
536     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
537     while (NULL != (lp = nxtitemp(&ni))) {
538         if (lp->lnd_own != sp->sct_own)
539             continue;
540         if (lp->lnd_ship >= 0)
541             continue;
542         if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
543             continue;
544
545         mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
546         lnd_dies_fighting_che(lp);
547         if (mc <= 0)
548             return;
549     }
550
551     /* Hmm.. everyone dead.. too bad */
552 }
553
554 static void
555 lnd_dies_fighting_che(struct lndstr *lp)
556 {
557     int i, j;
558     struct lndstr *clp;
559     struct plnstr *cpp;
560     struct nukstr *cnp;
561
562     lp->lnd_effic = 0;
563     lnd_submil(lp, 1000);       /* Remove 'em all */
564     wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
565        prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
566     makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
567     lp->lnd_own = 0;
568
569     /* Take dead lp off its carrier */
570     if (lp->lnd_land >= 0) {
571         lnd_carrier_change(lp, EF_LAND, lp->lnd_land, -1);
572         lp->lnd_land = -1;
573     }
574
575     /* Unload lp's land unit cargo */
576     for (i = lnd_first_on_land(lp); i >= 0; i = lnd_next_on_unit(i)) {
577         clp = getlandp(i);
578         if (CANT_HAPPEN(!clp))
579             continue;
580         lnd_carrier_change(clp, EF_LAND, clp->lnd_land, -1);
581         clp->lnd_land = -1;
582     }
583
584     /* Destroy lp's plane cargo */
585     for (i = pln_first_on_land(lp); i >= 0; i = pln_next_on_unit(i)) {
586         cpp = getplanep(i);
587         if (CANT_HAPPEN(!cpp))
588             continue;
589         pln_carrier_change(cpp, EF_LAND, cpp->pln_land, -1);
590         makelost(EF_PLANE, cpp->pln_own, i, cpp->pln_x, cpp->pln_y);
591         wu(0, cpp->pln_own, "%s lost!\n", prplane(cpp));
592         cpp->pln_own = 0;
593         cpp->pln_effic = 0;
594         cpp->pln_land = -1;
595
596         j = nuk_on_plane(cpp);
597         if (j >= 0) {
598             cnp = getnukep(j);
599             if (CANT_HAPPEN(!cnp))
600                 continue;
601             nuk_carrier_change(cnp, EF_PLANE, cnp->nuk_plane, -1);
602             makelost(EF_NUKE, cnp->nuk_own, j, cnp->nuk_x, cnp->nuk_y);
603             wu(0, cnp->nuk_own, "%s lost!\n", prnuke(cnp));
604             cnp->nuk_own = 0;
605             cnp->nuk_effic = 0;
606             cnp->nuk_plane = -1;
607         }
608     }
609 }