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