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