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