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