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