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