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