]> git.pond.sub.org Git - empserver/blob - src/lib/update/revolt.c
Factor lnd_dies_fighting_che() out of take_casualties()
[empserver] / src / lib / update / revolt.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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  */
33
34 #include <config.h>
35
36 #include "land.h"
37 #include "lost.h"
38 #include "news.h"
39 #include "nsc.h"
40 #include "path.h"
41 #include "plane.h"
42 #include "update.h"
43
44 static void take_casualties(struct sctstr *, int);
45 static void lnd_dies_fighting_che(struct lndstr *);
46
47 void
48 revolt(struct sctstr *sp)
49 {
50     int che_civ;
51     int che_uw;
52     int civ;
53     int uw;
54     int che;
55     int n;
56
57     che = sp->sct_che;
58     if (che != 0 && (sp->sct_che_target != sp->sct_own || che >= CHE_MAX))
59         return;
60     civ = sp->sct_item[I_CIVIL];
61     uw = sp->sct_item[I_UW];
62     if (che > (civ + uw) * 3)
63         return;
64     che_uw = 0;
65     /* che due to civilian unrest */
66     n = 10 - (random() % 20);
67     che_civ = 3 + (civ * n / 500);
68     if (che_civ < 0)
69         che_civ = 0;
70     else if (che_civ * 3 > civ)
71         che_civ = civ / 3;
72     if (che + che_civ > CHE_MAX)
73         che_civ = CHE_MAX - che;
74     che += che_civ;
75     if (che < CHE_MAX) {
76         /* che due to uw unrest */
77         n = 10 + (random() % 30);
78         che_uw = 5 + (uw * n / 500);
79         if (che_uw > uw)
80             che_uw = uw;
81         if (che + che_uw > CHE_MAX)
82             che_uw = CHE_MAX - che_uw;
83         che += che_uw;
84     }
85     if (che_civ + che_uw > 0) {
86         civ -= che_civ;
87         uw -= che_uw;
88         sp->sct_che_target = sp->sct_own;
89         sp->sct_che = che;
90         if (che_civ > 0)
91             sp->sct_item[I_CIVIL] = civ;
92         if (che_uw > 0)
93             sp->sct_item[I_UW] = uw;
94     }
95 }
96
97 /*
98  * summary of effects.
99  * if there are no military in the sector, che recruit from
100  *   populace if pop loyalty is > 10.  They spread subversion otherwise,
101  *   trying to lower pop loyalty.
102  * if che outnumber military, they stay and shoot it out, kill the
103  *   military.
104  * if che are outnumbered by less than 5 to 1, they blow up stuff,
105  *   killing innocent civilians (never uw's) and damaging commodities.
106  * if che are outnumbered by more than 5 to 1, they try to leave the
107  *   sector for a nearby sector with fewer military.
108  *
109  * if the military lose any attacks, the pop loyalty in the sector
110  *   gets worse, representing military defeat.
111  * military can "catch" che's after bombing attacks, or after they move.
112  *   If military catch them, then they get to shoot it out with a portion
113  *   of the che's depending on the # of mil in the sector.  Chance to contact
114  *   is around 10% per every equal number of mil:che ratio in the sector.
115  *   "contact" is by 20% of the military in the sector, and odds are equal.
116  *
117  * Without a doubt this routine should be broken up, if only for readabilty.
118  */
119 void
120 guerrilla(struct sctstr *sp)
121 {
122     struct sctstr *nsp;
123     int recruit;
124     int move;
125     int ratio;
126     int che;
127     int mil;
128     int cc, mc;
129     double odds;
130     int civ;
131     int n;
132     int uw;
133     natid target;
134     struct natstr *tnat;
135     int convert;
136     natid actor;
137     natid victim;
138     int tmp;
139     int min_mil;
140     int val;
141     int oldmob;
142     struct lndstr *lp;
143     struct nstr_item ni;
144
145     mc = cc = 0;
146     recruit = 0;
147     convert = 0;
148     move = 0;
149     if (!sp->sct_che)
150         return;
151     civ = sp->sct_item[I_CIVIL];
152     uw = sp->sct_item[I_UW];
153     victim = sp->sct_own;
154     actor = sp->sct_oldown;
155     che = sp->sct_che;
156     mil = sp->sct_item[I_MILIT];
157
158     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
159
160     while (NULL != (lp = nxtitemp(&ni))) {
161         if (lp->lnd_own != sp->sct_own)
162             continue;
163         if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
164             continue;
165
166         mil += lp->lnd_item[I_MILIT];
167
168         if (sp->sct_che_target != sp->sct_own)
169             continue;
170
171         /* Security troops can now kill up to 1/5 their complement each
172            update, before doing anything else. */
173         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
174             int che_kill, r;
175
176             mil += lp->lnd_item[I_MILIT];
177             r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
178             che_kill = r < 1 ? 0 : roll(r);
179             if (che_kill > che)
180                 che_kill = che;
181             if (che_kill) {
182                 wu(0, sp->sct_own,
183                    "%s kills %d guerrilla%s in raid at %s!\n",
184                    prland(lp), che_kill, splur(che_kill), ownxy(sp));
185                 che -= che_kill;
186             }
187         }
188     }
189
190     /* Security forces killed all the che */
191     if (che <= 0) {
192         sp->sct_che = 0;
193         sp->sct_che_target = 0;
194         return;
195     }
196
197     target = sp->sct_che_target;
198     if (CANT_HAPPEN(target == 0))
199         return;
200     tnat = getnatp(target);
201     if (tnat->nat_stat == STAT_UNUSED) {
202         /* target nation has dissolved: che's retire.  */
203         logerror("%d Che targeted at country %d retiring", che, target);
204         sp->sct_che = 0;
205         sp->sct_che_target = 0;
206         sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
207         return;
208     }
209
210     if (sp->sct_own != target) {
211         move++;
212         goto domove;
213     }
214
215     ratio = mil / che;
216     odds = (double)che / (mil + che);
217     odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
218     if (mil == 0) {
219         wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
220            ownxy(sp));
221         recruit++;
222         convert++;
223     } else if (che > mil && mil > 0) {
224         /*
225          * shoot it out with the military, and kill them off.
226          * If loyalty bad enough, then take the sector over,
227          * and enlist 5% of civ as military force.
228          */
229         while (che > 0 && mil > 0) {
230             if (chance(odds)) {
231                 mc++;
232                 mil--;
233             } else {
234                 cc++;
235                 che--;
236             }
237         }
238         if (mil > 0) {
239             /* military won.  */
240             n = sp->sct_loyal - (random() % 15);
241             if (n < 0)
242                 n = 0;
243             sp->sct_loyal = n;
244         } else {
245             convert++;
246             recruit++;
247         }
248         take_casualties(sp, mc);
249     } else if (ratio < 5) {
250         /*
251          * guerrillas have to resort to blowing things up.
252          * Note this disrupts work in the sector.
253          */
254         n = (random() % 10) + (random() % che);
255         if (n > 100)
256             n = 100;
257         tmp = sp->sct_work - n;
258         if (tmp < 0)
259             tmp = 0;
260         sp->sct_work = tmp;
261         wu(0, sp->sct_own,
262            "Production %s disrupted by terrorists in %s\n",
263            effadv(n), ownxy(sp));
264         sect_damage(sp, n / 10);
265         recruit++;
266     } else {
267         /* ratio >= 5 */
268         move++;
269     }
270     if (mil > 0 && che > 0) {
271         /*
272          * we only get here if we haven't had combat previously.
273          * Chance to catch them.
274          * 20% of mil involved in attacking the che's.
275          */
276         if (chance(ratio * 0.10)) {
277             n = (mil / 5) + 1;
278             odds = (double)che / (n + che);
279             odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
280             while (che > 0 && n > 0) {
281                 if (chance(odds)) {
282                     mc++;
283                     n--;
284                 } else {
285                     cc++;
286                     che--;
287                 }
288             }
289             take_casualties(sp, mc);
290             recruit = 0;
291         }
292     }
293     if (convert && sp->sct_loyal >= 50) {
294         int n;
295         /* new owner gets to keep the mobility there */
296         oldmob = sp->sct_mobil;
297         /* che won, and sector converts. */
298         if (sp->sct_own == sp->sct_oldown)
299             sp->sct_oldown = 0;
300         else {
301             lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
302                            0, sp->sct_x, sp->sct_y);
303             takeover(sp, sp->sct_oldown);
304         }
305         sp->sct_mobil = oldmob;
306         civ += uw;
307         uw = 0;
308         n = civ / 20;
309         civ -= n;
310         if (civ > ITEM_MAX) {
311             uw = civ - ITEM_MAX;
312             civ = ITEM_MAX;
313         }
314         sp->sct_item[I_CIVIL] = civ;
315         sp->sct_item[I_UW] = uw;
316         sp->sct_item[I_MILIT] = n;
317         move++;
318         recruit = 0;
319         if (sp->sct_own)
320             wu(0, sp->sct_own, "Sector %s has been retaken!\n",
321                xyas(sp->sct_x, sp->sct_y, sp->sct_own));
322     }
323     if (recruit && che > 0) {
324         /* loyalty drops during recruitment efforts */
325         n = sp->sct_loyal;
326         if (n < 30)
327             n += (random() % 5) + 1;
328         else if (n < 70)
329             n += (random() % 10) + 4;
330         if (n > 127)
331             n = 127;
332         sp->sct_loyal = n;
333         if (sp->sct_oldown != sp->sct_own || n > 100) {
334             n = civ * (random() % 3) / 200;
335             n /= hap_fact(tnat, getnatp(sp->sct_oldown));
336             if (n + che > CHE_MAX)
337                 n = CHE_MAX - che;
338             che += n;
339             civ -= n;
340             sp->sct_item[I_CIVIL] = civ;
341         }
342         n = uw * (random() % 3) / 200;
343         if (n + che > CHE_MAX)
344             n = CHE_MAX - che;
345         che += n;
346         uw -= n;
347         sp->sct_item[I_UW] = uw;
348     }
349 domove:
350     if (move && che > 0) {
351         struct sctstr *nicest_sp = NULL;
352         if (convert)
353             min_mil = 999;
354         else
355             min_mil = mil;
356         /* search adjacent sectors for a nice one */
357         for (n = 1; n <= 6; n++) {
358             nsp = getsectp(sp->sct_x + diroff[n][0],
359                            sp->sct_y + diroff[n][1]);
360             if (dchr[nsp->sct_type].d_mob0 < 0)
361                 continue;
362             if (nsp->sct_own != target)
363                 continue;
364             if (nsp->sct_che > 0) {
365                 if (nsp->sct_che_target != target)
366                     continue;
367                 if (nsp->sct_che + che > CHE_MAX)
368                     continue;
369             }
370             val = nsp->sct_item[I_MILIT];
371             /* don't give che more precise info than spy */
372             val = roundintby(val, 10);
373             /* inject a modicum of indeterminism; also
374              * avoids che preferring certain directions */
375             val += random() % 10 - 5;
376             if (val >= min_mil)
377                 continue;
378             nicest_sp = nsp;
379             min_mil = val;
380         }
381         /* if we found a nice sector, go there */
382         if (nicest_sp) {
383             nicest_sp->sct_che += che;
384             nicest_sp->sct_che_target = target;
385             che = 0;
386         }
387     }
388     if (che > 0) {
389         sp->sct_che = che;
390         sp->sct_che_target = target;
391     } else {
392         sp->sct_che = 0;
393         sp->sct_che_target = 0;
394     }
395     if (mc > 0 || cc > 0) {
396         wu(0, target,
397            "Guerrilla warfare in %s\n",
398            xyas(sp->sct_x, sp->sct_y, target));
399         if (sp->sct_own == target)
400             wu(0, target, "  body count: troops: %d, rebels: %d\n", mc, cc);
401         else
402             wu(0, target, "  rebels murder %d military\n", mc);
403         nreport(actor, N_FREEDOM_FIGHT, victim, 1);
404     }
405     if (sp->sct_own != victim)
406         wu(0, victim, "Partisans take over %s!\n",
407            xyas(sp->sct_x, sp->sct_y, victim));
408 }
409
410 static void
411 take_casualties(struct sctstr *sp, int mc)
412 {
413     int orig_mil;
414     int cantake;
415     int nunits = 0, each, deq;
416     struct lndstr *lp;
417     struct nstr_item ni;
418
419     /* casualties come out of mil first */
420     orig_mil = sp->sct_item[I_MILIT];
421
422     if (mc <= orig_mil) {
423         sp->sct_item[I_MILIT] = orig_mil - mc;
424         return;
425     }
426     sp->sct_item[I_MILIT] = 0;
427
428     /* remaining casualites */
429     mc -= orig_mil;
430
431     /*
432      * Need to take total_casualties and divide
433      * them amongst the land units in the sector
434      * Do security troops first, then others.
435      * Try not to kill any unit.
436      */
437     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
438     while (NULL != (lp = nxtitemp(&ni))) {
439         if (lp->lnd_own != sp->sct_own)
440             continue;
441         if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
442             continue;
443         nunits++;
444         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
445             nunits++;
446     }
447
448     if (nunits == 0)
449         return;
450
451     each = (mc / nunits) + 2;
452
453     /* kill some security troops */
454     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
455     while (NULL != (lp = nxtitemp(&ni))) {
456         if (lp->lnd_own != sp->sct_own)
457             continue;
458         if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
459             continue;
460         if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
461             continue;
462
463         cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
464
465         if (cantake >= each) {
466             deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
467             mc -= 2 * each;
468         } else if (cantake > 0) {
469             deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
470             mc -= 2 * cantake;
471         } else
472             deq = 0;
473
474         lp->lnd_effic -= deq;
475         lp->lnd_mobil -= deq / 2;
476         deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
477         lnd_submil(lp, deq);
478         if (mc <= 0)
479             return;
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 || lp->lnd_land >= 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
494         if (cantake >= each) {
495             deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
496             mc -= each;
497         } else if (cantake > 0) {
498             deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
499             mc -= cantake;
500         } else
501             deq = 0;
502
503         lp->lnd_effic -= deq;
504         lp->lnd_mobil -= deq / 2;
505         deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
506         lnd_submil(lp, deq);
507         if (mc <= 0)
508             return;
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 || lp->lnd_land >= 0)
518             continue;
519         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
520             continue;
521
522         mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
523         lnd_dies_fighting_che(lp);
524         if (mc <= 0)
525             return;
526     }
527
528     /* Hmm.. still some left.. kill off units now */
529     /* kill some security troops */
530     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
531     while (NULL != (lp = nxtitemp(&ni))) {
532         if (lp->lnd_own != sp->sct_own)
533             continue;
534         if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
535             continue;
536         if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
537             continue;
538
539         mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
540         lnd_dies_fighting_che(lp);
541         if (mc <= 0)
542             return;
543     }
544
545     /* Hmm.. everyone dead.. too bad */
546 }
547
548 static void
549 lnd_dies_fighting_che(struct lndstr *lp)
550 {
551     lp->lnd_effic = 0;
552     lnd_submil(lp, 1000);       /* Remove 'em all */
553     wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
554        prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
555     makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
556     lp->lnd_own = 0;
557 }