]> git.pond.sub.org Git - empserver/blob - src/lib/update/revolt.c
Indented with src/scripts/indent-emp.
[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 #define get_che_cnum(x)         ((x) >> 8)
54 #define set_che_cnum(x, cn)     ((x) = ((x) & 0xff) | ((cn) << 8))
55 #define get_che_value(x)        ((x) & 0xff)
56 #define set_che_value(x, n)     ((x) = ((x) & 0xff00) | (n))
57
58 #define CHE_MAX                 255
59
60 void
61 revolt(struct sctstr *sp)
62 {
63     int che_civ;
64     int che_uw;
65     int civ;
66     int uw;
67     u_short che_combo;
68     int che;
69     int n;
70     int target;
71
72     che_combo = getvar(V_CHE, (s_char *)sp, EF_SECTOR);
73     che = get_che_value(che_combo);
74     target = get_che_cnum(che_combo);
75     if (che_combo != 0 && (target != sp->sct_own || che >= CHE_MAX))
76         return;
77     civ = getvar(V_CIVIL, (s_char *)sp, EF_SECTOR);
78     uw = getvar(V_UW, (s_char *)sp, EF_SECTOR);
79     if (che > (civ + uw) * 3)
80         return;
81     che_uw = 0;
82     che_civ = 0;
83     /* che due to civilian unrest */
84     n = 10 - (random() % 20);
85     che_civ = 3 + (civ * n / 500);
86     if (che_civ < 0)
87         che_civ = 0;
88     else if (che_civ * 3 > civ)
89         che_civ = civ / 3;
90     if (che + che_civ > CHE_MAX)
91         che_civ = CHE_MAX - che;
92     che += che_civ;
93     if (che < CHE_MAX) {
94         /* che due to uw unrest */
95         n = 10 + (random() % 30);
96         che_uw = 5 + (uw * n / 500);
97         if (che_uw > uw)
98             che_uw = uw;
99         if (che + che_uw > CHE_MAX)
100             che_uw = CHE_MAX - che_uw;
101         che += che_uw;
102     }
103     if (che_civ + che_uw > 0) {
104         civ -= che_civ;
105         uw -= che_uw;
106         set_che_cnum(che_combo, sp->sct_own);
107         set_che_value(che_combo, che);
108         putvar(V_CHE, (int)che_combo, (s_char *)sp, EF_SECTOR);
109         if (che_civ > 0)
110             putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
111         if (che_uw > 0)
112             putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
113 #ifdef DEBUG
114         logerror("(#%d) %d che fired up in %s",
115                  sp->sct_own, che, ownxy(sp));
116 #endif
117     }
118 }
119
120 /*
121  * summary of effects.
122  * if there are no military in the sector, che recruit from
123  *   populace if pop loyalty is > 10.  They spread subversion otherwise,
124  *   trying to lower pop loyalty.
125  * if che outnumber military, they stay and shoot it out, kill the
126  *   military.
127  * if che are outnumbered by less than 5 to 1, they blow up stuff,
128  *   killing innocent civilians (never uw's) and damaging commodities.
129  * if che are outnumbered by more than 5 to 1, they try to leave the
130  *   sector for a nearby sector with fewer military.
131  *
132  * if the military lose any attacks, the pop loyalty in the sector
133  *   gets worse, representing military defeat.
134  * military can "catch" che's after bombing attacks, or after they move.
135  *   If military catch them, then they get to shoot it out with a portion
136  *   of the che's depending on the # of mil in the sector.  Chance to contact
137  *   is around 10% per every equal number of mil:che ratio in the sector.
138  *   "contact" is by 20% of the military in the sector, and odds are equal.
139  *
140  * Without a doubt this routine should be broken up, if only for readabilty.
141  */
142 void
143 guerrilla(struct sctstr *sp)
144 {
145     extern s_char *effadv();
146     struct sctstr *nsp;
147     int recruit;
148     int move;
149     int ratio;
150     int che;
151     int mil;
152     int cc, mc;
153     double odds;
154     int civ;
155     int n;
156     int uw;
157     natid target;
158     struct natstr *tnat;
159     int convert;
160     natid actor;
161     natid victim;
162     u_short che_combo;
163     int vec[I_MAX + 1];
164     int tmp;
165     int min_mil;
166     int val;
167     int oldmob;
168     struct lndstr *lp;
169     s_char *nxtitemp(struct nstr_item *np, int owner);
170     struct nstr_item ni;
171     extern double hap_fact();
172
173     mc = cc = 0;
174     recruit = 0;
175     convert = 0;
176     move = 0;
177     if ((n = getvar(V_CHE, (s_char *)sp, EF_SECTOR)) <= 0)
178         return;
179     che_combo = n;
180     if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
181         return;
182     civ = vec[I_CIVIL];
183
184     uw = vec[I_UW];
185     victim = sp->sct_own;
186     actor = sp->sct_oldown;
187     che = get_che_value(che_combo);
188
189     mil = vec[I_MILIT];
190     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
191
192     while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
193         if (lp->lnd_own != sp->sct_own)
194             continue;
195
196         mil += lnd_getmil(lp);
197
198         /* Security troops can now kill up to 1/2 their complement each
199            update, before doing anything else. */
200         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
201             int che_kill, r;
202             struct lchrstr *lcp;
203
204             lcp = &lchr[(int)lp->lnd_type];
205             mil += lnd_getmil(lp);
206             r = (((float)(lp->lnd_effic / 100) * (float)(lnd_getmil(lp))) /
207                  2);
208             if (r < 2)
209                 r = 2;
210             che_kill = (roll(r) - 1);
211             if (che_kill > che)
212                 che_kill = che;
213             if (che_kill) {
214                 wu(0, sp->sct_own,
215                    "%s kills %d guerrilla%s in raid at %s!\n",
216                    prland(lp), che_kill, splur(che_kill), ownxy(sp));
217                 che -= che_kill;
218             }
219         }
220     }
221
222     /* Security forces killed all the che */
223     if (che <= 0) {
224         putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
225         return;
226     }
227
228     target = get_che_cnum(che_combo);
229     if (target == 0) {
230         /* the deity can't be a target! */
231         return;
232     }
233     tnat = getnatp(target);
234     if ((tnat->nat_stat & STAT_INUSE) == 0) {
235         /* target nation has dissolved: che's retire.  */
236         logerror("%d Che targeted at country %d retiring", che, target);
237         civ += che;
238         putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
239         putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
240         return;
241     }
242
243     if (sp->sct_own != target) {
244         /*logerror("own %d != target %d", sp->sct_own, target); */
245         move++;
246         goto domove;
247     }
248
249     ratio = mil / che;
250     odds = (double)che / (mil + che);
251     odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
252     if (mil == 0) {
253         wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
254            ownxy(sp));
255         recruit++;
256         convert++;
257     } else if (che > mil && mil > 0) {
258         /*logerror("guerrilla shootout with military"); */
259         /*
260          * shoot it out with the military, and kill them off.
261          * If loyalty bad enough, then take the sector over,
262          * and enlist 5% of civ as military force.
263          */
264         while (che > 0 && mil > 0) {
265             if (chance(odds)) {
266                 mc++;
267                 mil--;
268             } else {
269                 cc++;
270                 che--;
271             }
272         }
273         if (mil > 0) {
274             /* military won.  */
275             n = sp->sct_loyal - (random() % 15);
276             if (n < 0)
277                 n = 0;
278             sp->sct_loyal = n;
279             /*logerror("(#%d) mil beat che in %s", sp->sct_own, */
280             /*ownxy(sp)); */
281         } else {
282             convert++;
283             recruit++;
284             /*logerror("(#%d) che beat mil in %s", sp->sct_own, */
285             /*ownxy(sp)); */
286         }
287         take_casualties(sp, mc);
288     } else if (ratio < 5) {
289         /*
290          * guerrillas have to resort to blowing things up.
291          * Note this disrupts work in the sector.
292          */
293         n = 0;
294         n = (random() % 10) + (random() % che);
295         if (n > 100)
296             n = 100;
297         tmp = sp->sct_work - n;
298         if (tmp < 0)
299             tmp = 0;
300         sp->sct_work = tmp;
301         wu(0, sp->sct_own,
302            "Production %s disrupted by terrorists in %s\n",
303            effadv(n), ownxy(sp));
304         sect_damage(sp, n / 10, 0);
305         /*logerror("(#%d) che blew up %s for %d", sp->sct_own, */
306         /*ownxy(sp), n); */
307         recruit++;
308     } else {
309         /* ratio >= 5 */
310         /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own, */
311         /*che, mil, ownxy(sp)); */
312         move++;
313     }
314     if (mil > 0 && che > 0) {
315         /*
316          * we only get here if we haven't had combat previously.
317          * Chance to catch them.
318          * 20% of mil involved in attacking the che's.
319          */
320         if (chance(ratio * 0.10)) {
321             n = (mil / 5) + 1;
322             if ((n + che) == 0) {
323                 logerror("n=%d che=%d\n", n, che);
324                 if (che == 0)
325                     return;
326             }
327             odds = (double)che / (n + che);
328             odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
329             while (che > 0 && n > 0) {
330                 if (chance(odds)) {
331                     mc++;
332                     n--;
333                 } else {
334                     cc++;
335                     che--;
336                 }
337             }
338             take_casualties(sp, mc);
339             recruit = 0;
340             /*logerror("Caught che; mc: %d, cc: %d", cc, mc); */
341         }
342     }
343     if (convert && sp->sct_loyal >= 50) {
344         register int n;
345         /* new owner gets to keep the mobility there */
346         oldmob = sp->sct_mobil;
347         /* che won, and sector converts. */
348         if (sp->sct_own == sp->sct_oldown)
349             sp->sct_oldown = 0;
350         else
351             takeover(sp, sp->sct_oldown);
352         sp->sct_mobil = oldmob;
353         civ += uw;
354         uw = 0;
355         /*
356          * so we can't keep losing money by having
357          * our cap retaken
358          */
359         if (sp->sct_type == SCT_CAPIT && sp->sct_newtype == SCT_CAPIT)
360             sp->sct_newtype = SCT_AGRI;
361         n = civ / 20;
362         civ -= n;
363         putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
364         putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
365         putvar(V_MILIT, n, (s_char *)sp, EF_SECTOR);
366         move++;
367         recruit = 0;
368         if (sp->sct_own)
369             wu(0, sp->sct_own, "Sector %s has been retaken!\n",
370                xyas(sp->sct_x, sp->sct_y, sp->sct_own));
371     }
372     if (recruit && che > 0) {
373         /* loyalty drops during recruitment efforts */
374         n = sp->sct_loyal;
375         if (n < 30)
376             n += (random() % 5) + 1;
377         else if (n < 70)
378             n += (random() % 10) + 4;
379         if (n > 127)
380             n = 127;
381         sp->sct_loyal = n;
382         if (sp->sct_oldown != sp->sct_own || n > 100) {
383             n = civ * (random() % 3) / 200;
384             n /= hap_fact(tnat, getnatp(sp->sct_oldown));
385             if (n + che > CHE_MAX)
386                 n = CHE_MAX - che;
387             che += n;
388             civ -= n;
389             putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
390         }
391         n = uw * (random() % 3) / 200;
392         if (n + che > CHE_MAX)
393             n = CHE_MAX - che;
394         che += n;
395         uw -= n;
396         putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
397     }
398   domove:
399     if (move && che > 0) {
400         struct sctstr *maybe_sp = 0;
401         if (convert)
402             min_mil = 999;
403         else
404             min_mil = mil;
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             if (val >= min_mil)
421                 continue;
422             maybe_sp = nsp;
423             min_mil = val;
424         }
425         /*
426          * if n <= 6, we found a sector owned by TARGET which
427          * is a nice sector.  Otherwise, we move to the first
428          * one we find ("maybe_sp").
429          */
430         if (maybe_sp != 0) {
431             che_combo = getvar(V_CHE, (s_char *)maybe_sp, EF_SECTOR);
432             che += get_che_value(che_combo);
433             set_che_value(che_combo, che);
434             set_che_cnum(che_combo, target);
435             putvar(V_CHE, (int)che_combo, (s_char *)maybe_sp, EF_SECTOR);
436             che = 0;
437         }
438     }
439     if (che > 0) {
440         set_che_value(che_combo, che);
441         set_che_cnum(che_combo, target);
442         putvar(V_CHE, (int)che_combo, (s_char *)sp, EF_SECTOR);
443     } else
444         putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
445     if (mc > 0 || cc > 0) {
446         /* don't tell who won just to be mean */
447         wu(0, target,
448            "Guerrilla warfare in %s\n",
449            xyas(sp->sct_x, sp->sct_y, target));
450         wu(0, target, "  body count: troops: %d, rebels: %d\n", mc, cc);
451         nreport(actor, N_FREEDOM_FIGHT, victim, 1);
452     }
453 }
454
455 void
456 take_casualties(struct sctstr *sp, int mc)
457 {
458     int orig_mil;
459     int cantake;
460     int nunits = 0, each, deq;
461     struct lndstr *lp;
462     s_char *nxtitemp(struct nstr_item *np, int owner);
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 }