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