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