]> git.pond.sub.org Git - empserver/blob - src/lib/update/revolt.c
Don't let embarked land units fight a che revolt
[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         if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
168             continue;
169
170         mil += lp->lnd_item[I_MILIT];
171
172         if (sp->sct_che_target != sp->sct_own)
173             continue;
174
175         /* Security troops can now kill up to 1/5 their complement each
176            update, before doing anything else. */
177         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
178             int che_kill, r;
179
180             mil += lp->lnd_item[I_MILIT];
181             r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
182             che_kill = r < 1 ? 0 : roll(r);
183             if (che_kill > che)
184                 che_kill = che;
185             if (che_kill) {
186                 wu(0, sp->sct_own,
187                    "%s kills %d guerrilla%s in raid at %s!\n",
188                    prland(lp), che_kill, splur(che_kill), ownxy(sp));
189                 che -= che_kill;
190             }
191         }
192     }
193
194     /* Security forces killed all the che */
195     if (che <= 0) {
196         sp->sct_che = 0;
197         sp->sct_che_target = 0;
198         return;
199     }
200
201     target = sp->sct_che_target;
202     if (CANT_HAPPEN(target == 0))
203         return;
204     tnat = getnatp(target);
205     if (tnat->nat_stat == STAT_UNUSED) {
206         /* target nation has dissolved: che's retire.  */
207         logerror("%d Che targeted at country %d retiring", che, target);
208         sp->sct_che = 0;
209         sp->sct_che_target = 0;
210         sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
211         return;
212     }
213
214     if (sp->sct_own != target) {
215         move++;
216         goto domove;
217     }
218
219     ratio = mil / che;
220     odds = (double)che / (mil + che);
221     odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
222     if (mil == 0) {
223         wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
224            ownxy(sp));
225         recruit++;
226         convert++;
227     } else if (che > mil && mil > 0) {
228         /*
229          * shoot it out with the military, and kill them off.
230          * If loyalty bad enough, then take the sector over,
231          * and enlist 5% of civ as military force.
232          */
233         while (che > 0 && mil > 0) {
234             if (chance(odds)) {
235                 mc++;
236                 mil--;
237             } else {
238                 cc++;
239                 che--;
240             }
241         }
242         if (mil > 0) {
243             /* military won.  */
244             n = sp->sct_loyal - (random() % 15);
245             if (n < 0)
246                 n = 0;
247             sp->sct_loyal = n;
248         } else {
249             convert++;
250             recruit++;
251         }
252         take_casualties(sp, mc);
253     } else if (ratio < 5) {
254         /*
255          * guerrillas have to resort to blowing things up.
256          * Note this disrupts work in the sector.
257          */
258         n = (random() % 10) + (random() % che);
259         if (n > 100)
260             n = 100;
261         tmp = sp->sct_work - n;
262         if (tmp < 0)
263             tmp = 0;
264         sp->sct_work = tmp;
265         wu(0, sp->sct_own,
266            "Production %s disrupted by terrorists in %s\n",
267            effadv(n), ownxy(sp));
268         sect_damage(sp, n / 10);
269         recruit++;
270     } else {
271         /* ratio >= 5 */
272         move++;
273     }
274     if (mil > 0 && che > 0) {
275         /*
276          * we only get here if we haven't had combat previously.
277          * Chance to catch them.
278          * 20% of mil involved in attacking the che's.
279          */
280         if (chance(ratio * 0.10)) {
281             n = (mil / 5) + 1;
282             odds = (double)che / (n + che);
283             odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
284             while (che > 0 && n > 0) {
285                 if (chance(odds)) {
286                     mc++;
287                     n--;
288                 } else {
289                     cc++;
290                     che--;
291                 }
292             }
293             take_casualties(sp, mc);
294             recruit = 0;
295         }
296     }
297     if (convert && sp->sct_loyal >= 50) {
298         int n;
299         /* new owner gets to keep the mobility there */
300         oldmob = sp->sct_mobil;
301         /* che won, and sector converts. */
302         if (sp->sct_own == sp->sct_oldown)
303             sp->sct_oldown = 0;
304         else {
305             lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
306                            0, sp->sct_x, sp->sct_y);
307             takeover(sp, sp->sct_oldown);
308         }
309         sp->sct_mobil = oldmob;
310         civ += uw;
311         uw = 0;
312         n = civ / 20;
313         civ -= n;
314         if (civ > ITEM_MAX) {
315             uw = civ - ITEM_MAX;
316             civ = ITEM_MAX;
317         }
318         sp->sct_item[I_CIVIL] = civ;
319         sp->sct_item[I_UW] = uw;
320         sp->sct_item[I_MILIT] = n;
321         move++;
322         recruit = 0;
323         if (sp->sct_own)
324             wu(0, sp->sct_own, "Sector %s has been retaken!\n",
325                xyas(sp->sct_x, sp->sct_y, sp->sct_own));
326     }
327     if (recruit && che > 0) {
328         /* loyalty drops during recruitment efforts */
329         n = sp->sct_loyal;
330         if (n < 30)
331             n += (random() % 5) + 1;
332         else if (n < 70)
333             n += (random() % 10) + 4;
334         if (n > 127)
335             n = 127;
336         sp->sct_loyal = n;
337         if (sp->sct_oldown != sp->sct_own || n > 100) {
338             n = civ * (random() % 3) / 200;
339             n /= hap_fact(tnat, getnatp(sp->sct_oldown));
340             if (n + che > CHE_MAX)
341                 n = CHE_MAX - che;
342             che += n;
343             civ -= n;
344             sp->sct_item[I_CIVIL] = civ;
345         }
346         n = uw * (random() % 3) / 200;
347         if (n + che > CHE_MAX)
348             n = CHE_MAX - che;
349         che += n;
350         uw -= n;
351         sp->sct_item[I_UW] = uw;
352     }
353   domove:
354     if (move && che > 0) {
355         struct sctstr *nicest_sp = NULL;
356         if (convert)
357             min_mil = 999;
358         else
359             min_mil = mil;
360         /* search adjacent sectors for a nice one */
361         for (n = 1; n <= 6; n++) {
362             nsp = getsectp(sp->sct_x + diroff[n][0],
363                            sp->sct_y + diroff[n][1]);
364             if (dchr[nsp->sct_type].d_mob0 < 0)
365                 continue;
366             if (nsp->sct_own != target)
367                 continue;
368             if (nsp->sct_che > 0) {
369                 if (nsp->sct_che_target != target)
370                     continue;
371                 if (nsp->sct_che + che > CHE_MAX)
372                     continue;
373             }
374             val = nsp->sct_item[I_MILIT];
375             /* don't give che more precise info than spy */
376             val = roundintby(val, 10);
377             /* inject a modicum of indeterminism; also
378              * avoids che preferring certain directions */
379             val += random() % 10 - 5;
380             if (val >= min_mil)
381                 continue;
382             nicest_sp = nsp;
383             min_mil = val;
384         }
385         /* if we found a nice sector, go there */
386         if (nicest_sp) {
387             nicest_sp->sct_che += che;
388             nicest_sp->sct_che_target = target;
389             che = 0;
390         }
391     }
392     if (che > 0) {
393         sp->sct_che = che;
394         sp->sct_che_target = target;
395     } else {
396         sp->sct_che = 0;
397         sp->sct_che_target = 0;
398     }
399     if (mc > 0 || cc > 0) {
400         wu(0, target,
401            "Guerrilla warfare in %s\n",
402            xyas(sp->sct_x, sp->sct_y, target));
403         if (sp->sct_own == target)
404             wu(0, target, "  body count: troops: %d, rebels: %d\n", mc, cc);
405         else
406             wu(0, target,
407                "  rebels murder %d military\n", mc);
408         nreport(actor, N_FREEDOM_FIGHT, victim, 1);
409     }
410     if (sp->sct_own != victim)
411         wu(0, victim, "Partisans take over %s!\n",
412            xyas(sp->sct_x, sp->sct_y, victim));
413 }
414
415 static void
416 take_casualties(struct sctstr *sp, int mc)
417 {
418     int orig_mil;
419     int cantake;
420     int nunits = 0, each, deq;
421     struct lndstr *lp;
422     struct nstr_item ni;
423
424     /* casualties come out of mil first */
425     orig_mil = sp->sct_item[I_MILIT];
426
427     if (mc <= orig_mil) {
428         sp->sct_item[I_MILIT] = orig_mil - mc;
429         return;
430     }
431     sp->sct_item[I_MILIT] = 0;
432
433     /* remaining casualites */
434     mc -= orig_mil;
435
436     /*
437      * Need to take total_casualties and divide
438      * them amongst the land units in the sector
439      * Do security troops first, then others.
440      * Try not to kill any unit.
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 || lp->lnd_land >= 0)
447             continue;
448         nunits++;
449         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
450             nunits++;
451     }
452
453     if (nunits == 0)
454         return;
455
456     each = (mc / nunits) + 2;
457
458     /* kill some security troops */
459     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
460     while (NULL != (lp = nxtitemp(&ni))) {
461         if (lp->lnd_own != sp->sct_own)
462             continue;
463         if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
464             continue;
465         if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
466             continue;
467
468         cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
469
470         if (cantake >= each) {
471             deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
472             mc -= 2 * each;
473         } else if (cantake > 0) {
474             deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
475             mc -= 2 * cantake;
476         } else
477             deq = 0;
478
479         lp->lnd_effic -= deq;
480         lp->lnd_mobil -= deq / 2;
481         deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
482         lnd_submil(lp, deq);
483         if (mc <= 0)
484             return;
485     }
486
487     /* kill some normal troops */
488     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
489     while (NULL != (lp = nxtitemp(&ni))) {
490         if (lp->lnd_own != sp->sct_own)
491             continue;
492         if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
493             continue;
494         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
495             continue;
496
497         cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
498
499         if (cantake >= each) {
500             deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
501             mc -= each;
502         } else if (cantake > 0) {
503             deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
504             mc -= cantake;
505         } else
506             deq = 0;
507
508         lp->lnd_effic -= deq;
509         lp->lnd_mobil -= deq / 2;
510         deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
511         lnd_submil(lp, deq);
512         if (mc <= 0)
513             return;
514     }
515
516     /* Hmm.. still some left.. kill off units now */
517     /* kill some normal troops */
518     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
519     while (NULL != (lp = nxtitemp(&ni))) {
520         if (lp->lnd_own != sp->sct_own)
521             continue;
522         if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
523             continue;
524         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
525             continue;
526
527         mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
528         lp->lnd_effic = 0;
529         lnd_submil(lp, 1000);   /* Remove 'em all */
530         wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
531            prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
532         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
533         lp->lnd_own = 0;
534         if (mc <= 0)
535             return;
536     }
537
538     /* Hmm.. still some left.. kill off units now */
539     /* kill some security troops */
540     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
541     while (NULL != (lp = nxtitemp(&ni))) {
542         if (lp->lnd_own != sp->sct_own)
543             continue;
544         if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
545             continue;
546         if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
547             continue;
548
549         mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
550         lp->lnd_effic = 0;
551         lnd_submil(lp, 1000);   /* Kill 'em all */
552         wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
553            prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
554         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
555         lp->lnd_own = 0;
556         if (mc <= 0)
557             return;
558     }
559
560     /* Hmm.. everyone dead.. too bad */
561 }