2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, 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-2012
47 static void take_casualties(struct sctstr *, int);
48 static void lnd_dies_fighting_che(struct lndstr *);
51 revolt(struct sctstr *sp)
61 if (che != 0 && (sp->sct_che_target != sp->sct_own || che >= CHE_MAX))
63 civ = sp->sct_item[I_CIVIL];
64 uw = sp->sct_item[I_UW];
65 if (che > (civ + uw) * 3)
68 /* che due to civilian unrest */
70 che_civ = 3 + (civ * n / 500);
73 else if (che_civ * 3 > civ)
75 if (che + che_civ > CHE_MAX)
76 che_civ = CHE_MAX - che;
79 /* che due to uw unrest */
81 che_uw = 5 + (uw * n / 500);
84 if (che + che_uw > CHE_MAX)
85 che_uw = CHE_MAX - che_uw;
88 if (che_civ + che_uw > 0) {
91 sp->sct_che_target = sp->sct_own;
94 sp->sct_item[I_CIVIL] = civ;
96 sp->sct_item[I_UW] = uw;
101 * summary of effects.
102 * if there are no military in the sector, che recruit from
103 * populace if pop loyalty is > 10. They spread subversion otherwise,
104 * trying to lower pop loyalty.
105 * if che outnumber military, they stay and shoot it out, kill the
107 * if che are outnumbered by less than 5 to 1, they blow up stuff,
108 * killing innocent civilians (never uw's) and damaging commodities.
109 * if che are outnumbered by more than 5 to 1, they try to leave the
110 * sector for a nearby sector with fewer military.
112 * if the military lose any attacks, the pop loyalty in the sector
113 * gets worse, representing military defeat.
114 * military can "catch" che's after bombing attacks, or after they move.
115 * If military catch them, then they get to shoot it out with a portion
116 * of the che's depending on the # of mil in the sector. Chance to contact
117 * is around 10% per every equal number of mil:che ratio in the sector.
118 * "contact" is by 20% of the military in the sector, and odds are equal.
120 * Without a doubt this routine should be broken up, if only for readabilty.
123 guerrilla(struct sctstr *sp)
154 civ = sp->sct_item[I_CIVIL];
155 uw = sp->sct_item[I_UW];
156 victim = sp->sct_own;
157 actor = sp->sct_oldown;
159 mil = sp->sct_item[I_MILIT];
161 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
163 while (NULL != (lp = nxtitemp(&ni))) {
164 if (lp->lnd_own != sp->sct_own)
166 if (lp->lnd_ship >= 0)
169 mil += lp->lnd_item[I_MILIT];
171 if (sp->sct_che_target != sp->sct_own)
174 /* Security troops can now kill up to 1/5 their complement each
175 update, before doing anything else. */
176 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
179 mil += lp->lnd_item[I_MILIT];
180 r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
181 che_kill = r < 1 ? 0 : roll(r);
186 "%s kills %d guerrilla%s in raid at %s!\n",
187 prland(lp), che_kill, splur(che_kill), ownxy(sp));
193 /* Security forces killed all the che */
196 sp->sct_che_target = 0;
200 target = sp->sct_che_target;
201 if (CANT_HAPPEN(target == 0))
203 tnat = getnatp(target);
204 if (tnat->nat_stat == STAT_UNUSED) {
205 /* target nation has dissolved: che's retire. */
206 logerror("%d Che targeted at country %d retiring", che, target);
208 sp->sct_che_target = 0;
209 sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
213 if (sp->sct_own != target) {
219 odds = (double)che / (mil + che);
220 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
222 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
226 } else if (che > mil && mil > 0) {
228 * shoot it out with the military, and kill them off.
229 * If loyalty bad enough, then take the sector over,
230 * and enlist 5% of civ as military force.
232 while (che > 0 && mil > 0) {
243 n = sp->sct_loyal - roll0(15);
251 take_casualties(sp, mc);
252 } else if (ratio < 5) {
254 * guerrillas have to resort to blowing things up.
255 * Note this disrupts work in the sector.
257 n = roll0(10) + roll0(che);
260 tmp = sp->sct_work - n;
265 "Production %s disrupted by terrorists in %s\n",
266 effadv(n), ownxy(sp));
267 sect_damage(sp, n / 10);
273 if (mil > 0 && che > 0) {
275 * we only get here if we haven't had combat previously.
276 * Chance to catch them.
277 * 20% of mil involved in attacking the che's.
279 if (chance(ratio * 0.10)) {
281 odds = (double)che / (n + che);
282 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
283 while (che > 0 && n > 0) {
292 take_casualties(sp, mc);
296 if (convert && sp->sct_loyal >= 50) {
298 /* new owner gets to keep the mobility there */
299 oldmob = sp->sct_mobil;
300 /* che won, and sector converts. */
301 if (sp->sct_own == sp->sct_oldown)
304 lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
305 0, sp->sct_x, sp->sct_y);
306 takeover(sp, sp->sct_oldown);
308 sp->sct_mobil = oldmob;
313 if (civ > ITEM_MAX) {
317 sp->sct_item[I_CIVIL] = civ;
318 sp->sct_item[I_UW] = uw;
319 sp->sct_item[I_MILIT] = n;
323 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
324 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
326 if (recruit && che > 0) {
327 /* loyalty drops during recruitment efforts */
336 if (sp->sct_oldown != sp->sct_own || n > 100) {
337 n = civ * roll0(3) / 200;
338 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
339 if (n + che > CHE_MAX)
343 sp->sct_item[I_CIVIL] = civ;
345 n = uw * roll0(3) / 200;
346 if (n + che > CHE_MAX)
350 sp->sct_item[I_UW] = uw;
353 if (move && che > 0) {
354 struct sctstr *nicest_sp = NULL;
359 /* search adjacent sectors for a nice one */
360 for (n = 1; n <= 6; n++) {
361 nsp = getsectp(sp->sct_x + diroff[n][0],
362 sp->sct_y + diroff[n][1]);
363 if (dchr[nsp->sct_type].d_mob0 < 0)
365 if (nsp->sct_own != target)
367 if (nsp->sct_che > 0) {
368 if (nsp->sct_che_target != target)
370 if (nsp->sct_che + che > CHE_MAX)
373 val = nsp->sct_item[I_MILIT];
374 /* don't give che more precise info than spy */
375 val = roundintby(val, 10);
376 /* inject a modicum of indeterminism; also
377 * avoids che preferring certain directions */
384 /* if we found a nice sector, go there */
386 nicest_sp->sct_che += che;
387 nicest_sp->sct_che_target = target;
393 sp->sct_che_target = target;
396 sp->sct_che_target = 0;
398 if (mc > 0 || cc > 0) {
400 "Guerrilla warfare in %s\n",
401 xyas(sp->sct_x, sp->sct_y, target));
402 if (sp->sct_own == target)
403 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
405 wu(0, target, " rebels murder %d military\n", mc);
406 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
408 if (sp->sct_own != victim)
409 wu(0, victim, "Partisans take over %s!\n",
410 xyas(sp->sct_x, sp->sct_y, victim));
414 take_casualties(struct sctstr *sp, int mc)
418 int nunits = 0, each, deq;
422 /* casualties come out of mil first */
423 orig_mil = sp->sct_item[I_MILIT];
425 if (mc <= orig_mil) {
426 sp->sct_item[I_MILIT] = orig_mil - mc;
429 sp->sct_item[I_MILIT] = 0;
431 /* remaining casualites */
435 * Need to take total_casualties and divide
436 * them amongst the land units in the sector
437 * Do security troops first, then others.
438 * Try not to kill any unit.
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)
447 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
454 each = (mc / nunits) + 2;
456 /* kill some security troops */
457 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
458 while (NULL != (lp = nxtitemp(&ni))) {
459 if (lp->lnd_own != sp->sct_own)
461 if (lp->lnd_ship >= 0)
463 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
466 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
468 if (cantake >= each) {
469 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
471 } else if (cantake > 0) {
472 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
477 lp->lnd_effic -= deq;
478 lp->lnd_mobil -= deq / 2;
479 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
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];
497 if (cantake >= each) {
498 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
500 } else if (cantake > 0) {
501 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
506 lp->lnd_effic -= deq;
507 lp->lnd_mobil -= deq / 2;
508 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
514 /* Hmm.. still some left.. kill off units now */
515 /* kill some normal troops */
516 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
517 while (NULL != (lp = nxtitemp(&ni))) {
518 if (lp->lnd_own != sp->sct_own)
520 if (lp->lnd_ship >= 0)
522 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
525 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
526 lnd_dies_fighting_che(lp);
531 /* Hmm.. still some left.. kill off units now */
532 /* kill some security troops */
533 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
534 while (NULL != (lp = nxtitemp(&ni))) {
535 if (lp->lnd_own != sp->sct_own)
537 if (lp->lnd_ship >= 0)
539 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
542 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
543 lnd_dies_fighting_che(lp);
548 /* Hmm.. everyone dead.. too bad */
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));