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