2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, 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-2016
46 #include "prototypes.h"
50 static int take_casualties(struct sctstr *, int);
51 static void lnd_dies_fighting_che(struct lndstr *);
54 revolt(struct sctstr *sp)
64 if (che != 0 && (sp->sct_che_target != sp->sct_own || che >= CHE_MAX))
66 civ = sp->sct_item[I_CIVIL];
67 uw = sp->sct_item[I_UW];
68 if (che > (civ + uw) * 3)
71 /* che due to civilian unrest */
73 che_civ = 3 + (civ * n / 500);
76 else if (che_civ * 3 > civ)
78 if (che + che_civ > CHE_MAX)
79 che_civ = CHE_MAX - che;
82 /* che due to uw unrest */
84 che_uw = 5 + (uw * n / 500);
87 if (che + che_uw > CHE_MAX)
88 che_uw = CHE_MAX - che_uw;
91 if (che_civ + che_uw > 0) {
94 sp->sct_che_target = sp->sct_own;
97 sp->sct_item[I_CIVIL] = civ;
99 sp->sct_item[I_UW] = uw;
104 * summary of effects.
105 * if there are no military in the sector, che recruit from
106 * populace if pop loyalty is > 10. They spread subversion otherwise,
107 * trying to lower pop loyalty.
108 * if che outnumber military, they stay and shoot it out, kill the
110 * if che are outnumbered by less than 5 to 1, they blow up stuff,
111 * killing innocent civilians (never uw's) and damaging commodities.
112 * if che are outnumbered by more than 5 to 1, they try to leave the
113 * sector for a nearby sector with fewer military.
115 * if the military lose any attacks, the pop loyalty in the sector
116 * gets worse, representing military defeat.
117 * military can "catch" che's after bombing attacks, or after they move.
118 * If military catch them, then they get to shoot it out with a portion
119 * of the che's depending on the # of mil in the sector. Chance to contact
120 * is around 10% per every equal number of mil:che ratio in the sector.
121 * "contact" is by 20% of the military in the sector, and odds are equal.
123 * Without a doubt this routine should be broken up, if only for readabilty.
126 guerrilla(struct sctstr *sp)
158 civ = sp->sct_item[I_CIVIL];
159 uw = sp->sct_item[I_UW];
160 victim = sp->sct_own;
161 actor = sp->sct_oldown;
163 mil = sp->sct_item[I_MILIT];
166 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
168 while (NULL != (lp = nxtitemp(&ni))) {
169 if (lp->lnd_own != sp->sct_own)
171 if (lp->lnd_ship >= 0)
174 mil += lp->lnd_item[I_MILIT];
176 if (sp->sct_che_target != sp->sct_own)
179 /* Security troops can now kill up to 1/5 their complement each
180 update, before doing anything else. */
181 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
184 security_bonus += lp->lnd_item[I_MILIT] * 3;
185 r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
186 che_kill = r < 1 ? 0 : roll(r);
191 "%s kills %d guerrilla%s in raid at %s!\n",
192 prland(lp), che_kill, splur(che_kill), ownxy(sp));
198 /* Security forces killed all the che */
201 sp->sct_che_target = 0;
205 target = sp->sct_che_target;
206 if (CANT_HAPPEN(target == 0))
208 tnat = getnatp(target);
209 if (tnat->nat_stat == STAT_UNUSED) {
210 /* target nation has dissolved: che's retire. */
211 logerror("%d Che targeted at country %d retiring", che, target);
213 sp->sct_che_target = 0;
214 sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
218 if (sp->sct_own != target) {
223 ratio = (mil + security_bonus) / che;
224 odds = (double)che / (mil + security_bonus + che);
225 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
227 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
231 } else if (che > mil && mil > 0) {
233 * shoot it out with the military, and kill them off.
234 * If loyalty bad enough, then take the sector over,
235 * and enlist 5% of civ as military force.
237 while (che > cc && mil > mc) {
245 n = sp->sct_loyal - roll0(15);
253 mc = take_casualties(sp, mc);
256 } else if (ratio < 5) {
258 * guerrillas have to resort to blowing things up.
259 * Note this disrupts work in the sector.
261 n = roll0(10) + roll0(che);
264 tmp = sp->sct_work - n;
269 "Production %s disrupted by terrorists in %s\n",
270 effadv(n), ownxy(sp));
271 sect_damage(sp, n / 10);
277 if (mil > 0 && che > 0) {
279 * we only get here if we haven't had combat previously.
280 * Chance to catch them.
281 * 20% of mil involved in attacking the che's.
283 if (chance(ratio * 0.10)) {
285 odds = (double)che / (n + security_bonus / 5 + che);
286 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
287 while (che > cc && n > mc) {
293 mc = take_casualties(sp, mc);
299 if (convert && sp->sct_loyal >= 50) {
301 /* new owner gets to keep the mobility there */
302 oldmob = sp->sct_mobil;
303 /* che won, and sector converts. */
304 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);
311 sp->sct_mobil = oldmob;
316 if (civ > ITEM_MAX) {
320 sp->sct_item[I_CIVIL] = civ;
321 sp->sct_item[I_UW] = uw;
322 sp->sct_item[I_MILIT] = n;
326 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
327 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
329 if (recruit && che > 0) {
330 /* loyalty drops during recruitment efforts */
339 if (sp->sct_oldown != sp->sct_own || n > 100) {
340 n = civ * roll0(3) / 200;
341 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
342 if (n + che > CHE_MAX)
346 sp->sct_item[I_CIVIL] = civ;
348 n = uw * roll0(3) / 200;
349 if (n + che > CHE_MAX)
353 sp->sct_item[I_UW] = uw;
356 if (move && che > 0) {
357 struct sctstr *nicest_sp = NULL;
362 /* search adjacent sectors for a nice one */
363 /* TODO consider land units in addition to mil */
364 for (n = 1; n <= 6; n++) {
365 nsp = getsectp(sp->sct_x + diroff[n][0],
366 sp->sct_y + diroff[n][1]);
367 if (dchr[nsp->sct_type].d_mob0 < 0)
369 if (nsp->sct_own != target)
371 if (nsp->sct_che > 0) {
372 if (nsp->sct_che_target != target)
374 if (nsp->sct_che + che > CHE_MAX)
377 val = nsp->sct_item[I_MILIT];
378 /* don't give che more precise info than spy */
379 val = roundintby(val, 10);
380 /* inject a modicum of indeterminism; also
381 * avoids che preferring certain directions */
388 /* if we found a nice sector, go there */
390 nicest_sp->sct_che += che;
391 nicest_sp->sct_che_target = target;
397 sp->sct_che_target = target;
400 sp->sct_che_target = 0;
402 if (mc > 0 || cc > 0) {
404 "Guerrilla warfare in %s\n",
405 xyas(sp->sct_x, sp->sct_y, target));
406 if (sp->sct_own == target)
407 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
409 wu(0, target, " rebels murder %d military\n", mc);
410 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
412 if (sp->sct_own != victim)
413 wu(0, victim, "Partisans take over %s!\n",
414 xyas(sp->sct_x, sp->sct_y, victim));
418 take_casualties(struct sctstr *sp, int mc)
422 int nunits = 0, each, deq, dam;
426 /* casualties come out of mil first */
427 orig_mil = sp->sct_item[I_MILIT];
429 if (mc <= orig_mil) {
430 sp->sct_item[I_MILIT] = orig_mil - mc;
433 sp->sct_item[I_MILIT] = 0;
435 /* remaining casualites */
440 * Need to take total_casualties and divide
441 * them amongst the land units in the sector
442 * Do security troops first, then others.
443 * Try not to kill any unit.
445 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
446 while (NULL != (lp = nxtitemp(&ni))) {
447 if (lp->lnd_own != sp->sct_own)
449 if (lp->lnd_ship >= 0)
454 if (CANT_HAPPEN(!nunits))
457 each = (mc / nunits) + 2;
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 (lp->lnd_own != sp->sct_own)
464 if (lp->lnd_ship >= 0)
466 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
469 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
470 cantake = MIN(lp->lnd_item[I_MILIT], cantake);
471 deq = MIN(cantake, each);
477 dam = ((double)deq / lp->lnd_item[I_MILIT]) * 100.0;
478 lp->lnd_effic -= dam;
479 lp->lnd_mobil -= dam / 2;
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 (lp->lnd_own != sp->sct_own)
490 if (lp->lnd_ship >= 0)
492 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
495 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
496 cantake = MIN(lp->lnd_item[I_MILIT], cantake);
497 deq = MIN(cantake, each);
503 dam = ((double)deq / lp->lnd_item[I_MILIT]) * 100.0;
504 lp->lnd_effic -= dam;
505 lp->lnd_mobil -= dam / 2;
511 /* Hmm.. still some left.. kill off units now */
512 /* kill some normal troops */
513 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
514 while (NULL != (lp = nxtitemp(&ni))) {
515 if (lp->lnd_own != sp->sct_own)
517 if (lp->lnd_ship >= 0)
519 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
522 mc -= lp->lnd_item[I_MILIT];
523 taken += lp->lnd_item[I_MILIT];
524 lnd_dies_fighting_che(lp);
529 /* Hmm.. still some left.. kill off units now */
530 /* kill some security troops */
531 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
532 while (NULL != (lp = nxtitemp(&ni))) {
533 if (lp->lnd_own != sp->sct_own)
535 if (lp->lnd_ship >= 0)
537 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
540 mc -= lp->lnd_item[I_MILIT];
541 taken += lp->lnd_item[I_MILIT];
542 lnd_dies_fighting_che(lp);
552 lnd_dies_fighting_che(struct lndstr *lp)
560 lnd_submil(lp, 1000); /* Remove 'em all */
561 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
562 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
563 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
566 /* Take dead lp off its carrier */
567 if (lp->lnd_land >= 0) {
568 lnd_carrier_change(lp, EF_LAND, lp->lnd_land, -1);
572 /* Unload lp's land unit cargo */
573 for (i = lnd_first_on_land(lp); i >= 0; i = lnd_next_on_unit(i)) {
575 if (CANT_HAPPEN(!clp))
577 lnd_carrier_change(clp, EF_LAND, clp->lnd_land, -1);
581 /* Destroy lp's plane cargo */
582 for (i = pln_first_on_land(lp); i >= 0; i = pln_next_on_unit(i)) {
584 if (CANT_HAPPEN(!cpp))
586 pln_carrier_change(cpp, EF_LAND, cpp->pln_land, -1);
587 makelost(EF_PLANE, cpp->pln_own, i, cpp->pln_x, cpp->pln_y);
588 wu(0, cpp->pln_own, "%s lost!\n", prplane(cpp));
593 j = nuk_on_plane(cpp);
596 if (CANT_HAPPEN(!cnp))
598 nuk_carrier_change(cnp, EF_PLANE, cnp->nuk_plane, -1);
599 makelost(EF_NUKE, cnp->nuk_own, j, cnp->nuk_x, cnp->nuk_y);
600 wu(0, cnp->nuk_own, "%s lost!\n", prnuke(cnp));