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