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