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