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