2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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.
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/>.
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.
27 * revolt.c: Have disloyal populace revolt!
29 * Known contributors to this file:
31 * Steve McClure, 1997-2000
32 * Markus Armbruster, 2004-2018
46 #include "prototypes.h"
50 static int take_casualties(struct sctstr *, int);
51 static int take_casualties_from_lands(struct sctstr *, int, int, int);
52 static void lnd_dies_fighting_che(struct lndstr *);
55 revolt(struct sctstr *sp)
65 if (che != 0 && (sp->sct_che_target != sp->sct_own || che >= CHE_MAX))
67 civ = sp->sct_item[I_CIVIL];
68 uw = sp->sct_item[I_UW];
69 if (che > (civ + uw) * 3)
72 /* che due to civilian unrest */
74 che_civ = 3 + (civ * n / 500);
77 else if (che_civ * 3 > civ)
79 if (che + che_civ > CHE_MAX)
80 che_civ = CHE_MAX - che;
83 /* che due to uw unrest */
85 che_uw = 5 + (uw * n / 500);
88 if (che + che_uw > CHE_MAX)
89 che_uw = CHE_MAX - che_uw;
92 if (che_civ + che_uw > 0) {
95 sp->sct_che_target = sp->sct_own;
98 sp->sct_item[I_CIVIL] = civ;
100 sp->sct_item[I_UW] = uw;
105 * summary of effects.
106 * if there are no military in the sector, che recruit from
107 * populace if pop loyalty is > 10. They spread subversion otherwise,
108 * trying to lower pop loyalty.
109 * if che outnumber military, they stay and shoot it out, kill the
111 * if che are outnumbered by less than 5 to 1, they blow up stuff,
112 * killing innocent civilians (never uw's) and damaging commodities.
113 * if che are outnumbered by more than 5 to 1, they try to leave the
114 * sector for a nearby sector with fewer military.
116 * if the military lose any attacks, the pop loyalty in the sector
117 * gets worse, representing military defeat.
118 * military can "catch" che's after bombing attacks, or after they move.
119 * If military catch them, then they get to shoot it out with a portion
120 * of the che's depending on the # of mil in the sector. Chance to contact
121 * is around 10% per every equal number of mil:che ratio in the sector.
122 * "contact" is by 20% of the military in the sector, and odds are equal.
124 * Without a doubt this routine should be broken up, if only for readabilty.
127 guerrilla(struct sctstr *sp)
135 double security_bonus;
159 civ = sp->sct_item[I_CIVIL];
160 uw = sp->sct_item[I_UW];
161 victim = sp->sct_own;
162 actor = sp->sct_oldown;
164 mil = sp->sct_item[I_MILIT];
167 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
169 while (NULL != (lp = nxtitemp(&ni))) {
170 if (lp->lnd_own != sp->sct_own)
172 if (lp->lnd_ship >= 0)
175 mil += lp->lnd_item[I_MILIT];
177 if (sp->sct_che_target != sp->sct_own)
180 /* Security troops can now kill up to 1/5 their complement each
181 update, before doing anything else. */
182 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
185 security_bonus += 3 * lp->lnd_item[I_MILIT] * lp->lnd_effic / 100.0;
186 r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
187 che_kill = r < 1 ? 0 : roll(r);
192 "%s kills %d guerrilla%s in raid at %s!\n",
193 prland(lp), che_kill, splur(che_kill), ownxy(sp));
199 /* Security forces killed all the che */
202 sp->sct_che_target = 0;
206 target = sp->sct_che_target;
207 if (CANT_HAPPEN(target == 0))
209 tnat = getnatp(target);
210 if (tnat->nat_stat == STAT_UNUSED) {
211 /* target nation has dissolved: che's retire. */
212 logerror("%d Che targeted at country %d retiring", che, target);
214 sp->sct_che_target = 0;
215 sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
219 if (sp->sct_own != target) {
224 ratio = (mil + security_bonus) / che;
225 odds = (double)che / (mil + security_bonus + che);
226 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
228 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
232 } else if (che > mil && mil > 0) {
234 * shoot it out with the military, and kill them off.
235 * If loyalty bad enough, then take the sector over,
236 * and enlist 5% of civ as military force.
238 while (che > cc && mil > mc) {
246 n = sp->sct_loyal - roll0(15);
254 mc = take_casualties(sp, mc);
257 } else if (ratio < 5) {
259 * guerrillas have to resort to blowing things up.
260 * Note this disrupts work in the sector.
262 n = roll0(10) + roll0(che);
265 tmp = sp->sct_work - n;
270 "Production %s disrupted by terrorists in %s\n",
271 effadv(n), ownxy(sp));
272 sect_damage(sp, n / 10);
278 if (mil > 0 && che > 0) {
280 * we only get here if we haven't had combat previously.
281 * Chance to catch them.
282 * 20% of mil involved in attacking the che's.
284 if (chance(ratio * 0.10)) {
286 odds = (double)che / (n + security_bonus / 5 + che);
287 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
288 while (che > cc && n > mc) {
294 mc = take_casualties(sp, mc);
300 if (convert && sp->sct_loyal >= 50) {
302 /* new owner gets to keep the mobility there */
303 oldmob = sp->sct_mobil;
304 /* che won, and sector converts. */
305 if (sp->sct_own == sp->sct_oldown)
307 lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
308 0, sp->sct_x, sp->sct_y);
309 takeover(sp, sp->sct_oldown);
310 sp->sct_mobil = oldmob;
315 if (civ > ITEM_MAX) {
319 sp->sct_item[I_CIVIL] = civ;
320 sp->sct_item[I_UW] = uw;
321 sp->sct_item[I_MILIT] = n;
325 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
326 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
328 if (recruit && che > 0) {
329 /* loyalty drops during recruitment efforts */
338 if (sp->sct_oldown != sp->sct_own || n > 100) {
339 n = civ * roll0(3) / 200;
340 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
341 if (n + che > CHE_MAX)
345 sp->sct_item[I_CIVIL] = civ;
347 n = uw * roll0(3) / 200;
348 if (n + che > CHE_MAX)
352 sp->sct_item[I_UW] = uw;
355 if (move && che > 0) {
356 struct sctstr *nicest_sp = NULL;
361 /* search adjacent sectors for a nice one */
362 /* TODO consider land units in addition to mil */
363 for (n = 1; n <= 6; n++) {
364 nsp = getsectp(sp->sct_x + diroff[n][0],
365 sp->sct_y + diroff[n][1]);
366 if (dchr[nsp->sct_type].d_mob0 < 0)
368 if (nsp->sct_own != target)
370 if (nsp->sct_che > 0) {
371 if (nsp->sct_che_target != target)
373 if (nsp->sct_che + che > CHE_MAX)
376 val = nsp->sct_item[I_MILIT];
377 /* don't give che more precise info than spy */
378 val = roundintby(val, 10);
379 /* inject a modicum of indeterminism; also
380 * avoids che preferring certain directions */
387 /* if we found a nice sector, go there */
389 nicest_sp->sct_che += che;
390 nicest_sp->sct_che_target = target;
396 sp->sct_che_target = target;
399 sp->sct_che_target = 0;
401 if (mc > 0 || cc > 0) {
403 "Guerrilla warfare in %s\n",
404 xyas(sp->sct_x, sp->sct_y, target));
405 if (sp->sct_own == target)
406 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
408 wu(0, target, " rebels murder %d military\n", mc);
409 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
411 if (sp->sct_own != victim)
412 wu(0, victim, "Partisans take over %s!\n",
413 xyas(sp->sct_x, sp->sct_y, victim));
417 take_casualties(struct sctstr *sp, int mc)
420 int nunits = 0, each;
424 /* casualties come out of mil first */
425 orig_mil = sp->sct_item[I_MILIT];
427 if (mc <= orig_mil) {
428 sp->sct_item[I_MILIT] = orig_mil - mc;
431 sp->sct_item[I_MILIT] = 0;
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 * TODO Spread proportionally to mil instead of evenly
440 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
441 while (NULL != (lp = nxtitemp(&ni))) {
442 if (lp->lnd_own != sp->sct_own)
444 if (lp->lnd_ship >= 0)
446 if (!lp->lnd_item[I_MILIT])
451 if (CANT_HAPPEN(!nunits))
455 each = (mc - taken) / nunits + 2;
457 /* kill some security troops */
458 taken += take_casualties_from_lands(sp, mc - taken, each, 1);
460 /* kill some normal troops */
461 taken += take_casualties_from_lands(sp, mc - taken, each, 0);
463 /* Hmm.. still some left.. kill off units now */
464 /* kill some normal troops */
465 taken += take_casualties_from_lands(sp, mc - taken, -1, 0);
467 /* Hmm.. still some left.. kill off units now */
468 /* kill some security troops */
469 taken += take_casualties_from_lands(sp, mc - taken, -1, 1);
471 CANT_HAPPEN(taken < mc);
476 take_casualties_from_lands(struct sctstr *sp, int cas, int each,
482 int cantake, deq, taken;
485 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
486 while (taken < cas && (lp = nxtitemp(&ni))) {
487 if (lp->lnd_own != sp->sct_own)
489 if (lp->lnd_ship >= 0)
491 if (!lp->lnd_item[I_MILIT])
493 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY) == !!security)
496 eff_per_cas = 100.0 / lchr[lp->lnd_type].l_item[I_MILIT];
497 cantake = lp->lnd_item[I_MILIT];
499 cantake = MIN(cantake, each);
500 cantake = MIN(cantake,
501 (int)((lp->lnd_effic - 40) / eff_per_cas));
503 deq = MIN(cantake, cas - taken);
508 lp->lnd_effic -= deq * eff_per_cas;
509 lp->lnd_mobil -= deq * eff_per_cas / 2;
511 if (lp->lnd_effic < LAND_MINEFF) {
512 CANT_HAPPEN(each >= 0);
513 taken += lp->lnd_item[I_MILIT];
514 lnd_dies_fighting_che(lp);
522 lnd_dies_fighting_che(struct lndstr *lp)
530 lnd_submil(lp, 1000); /* Remove 'em all */
531 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
532 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
533 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
536 /* Take dead lp off its carrier */
537 if (lp->lnd_land >= 0) {
538 lnd_carrier_change(lp, EF_LAND, lp->lnd_land, -1);
542 /* Unload lp's land unit cargo */
543 for (i = lnd_first_on_land(lp); i >= 0; i = lnd_next_on_unit(i)) {
545 if (CANT_HAPPEN(!clp))
547 lnd_carrier_change(clp, EF_LAND, clp->lnd_land, -1);
551 /* Destroy lp's plane cargo */
552 for (i = pln_first_on_land(lp); i >= 0; i = pln_next_on_unit(i)) {
554 if (CANT_HAPPEN(!cpp))
556 pln_carrier_change(cpp, EF_LAND, cpp->pln_land, -1);
557 makelost(EF_PLANE, cpp->pln_own, i, cpp->pln_x, cpp->pln_y);
558 wu(0, cpp->pln_own, "%s lost!\n", prplane(cpp));
563 j = nuk_on_plane(cpp);
566 if (CANT_HAPPEN(!cnp))
568 nuk_carrier_change(cnp, EF_PLANE, cnp->nuk_plane, -1);
569 makelost(EF_NUKE, cnp->nuk_own, j, cnp->nuk_x, cnp->nuk_y);
570 wu(0, cnp->nuk_own, "%s lost!\n", prnuke(cnp));