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-2012
46 #include "prototypes.h"
50 static void 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)
157 civ = sp->sct_item[I_CIVIL];
158 uw = sp->sct_item[I_UW];
159 victim = sp->sct_own;
160 actor = sp->sct_oldown;
162 mil = sp->sct_item[I_MILIT];
164 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
166 while (NULL != (lp = nxtitemp(&ni))) {
167 if (lp->lnd_own != sp->sct_own)
169 if (lp->lnd_ship >= 0)
172 mil += lp->lnd_item[I_MILIT];
174 if (sp->sct_che_target != sp->sct_own)
177 /* Security troops can now kill up to 1/5 their complement each
178 update, before doing anything else. */
179 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
182 mil += lp->lnd_item[I_MILIT];
183 r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
184 che_kill = r < 1 ? 0 : roll(r);
189 "%s kills %d guerrilla%s in raid at %s!\n",
190 prland(lp), che_kill, splur(che_kill), ownxy(sp));
196 /* Security forces killed all the che */
199 sp->sct_che_target = 0;
203 target = sp->sct_che_target;
204 if (CANT_HAPPEN(target == 0))
206 tnat = getnatp(target);
207 if (tnat->nat_stat == STAT_UNUSED) {
208 /* target nation has dissolved: che's retire. */
209 logerror("%d Che targeted at country %d retiring", che, target);
211 sp->sct_che_target = 0;
212 sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
216 if (sp->sct_own != target) {
222 odds = (double)che / (mil + che);
223 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
225 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
229 } else if (che > mil && mil > 0) {
231 * shoot it out with the military, and kill them off.
232 * If loyalty bad enough, then take the sector over,
233 * and enlist 5% of civ as military force.
235 while (che > 0 && mil > 0) {
246 n = sp->sct_loyal - roll0(15);
254 take_casualties(sp, mc);
255 } else if (ratio < 5) {
257 * guerrillas have to resort to blowing things up.
258 * Note this disrupts work in the sector.
260 n = roll0(10) + roll0(che);
263 tmp = sp->sct_work - n;
268 "Production %s disrupted by terrorists in %s\n",
269 effadv(n), ownxy(sp));
270 sect_damage(sp, n / 10);
276 if (mil > 0 && che > 0) {
278 * we only get here if we haven't had combat previously.
279 * Chance to catch them.
280 * 20% of mil involved in attacking the che's.
282 if (chance(ratio * 0.10)) {
284 odds = (double)che / (n + che);
285 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
286 while (che > 0 && n > 0) {
295 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 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)
421 int nunits = 0, each, deq;
425 /* casualties come out of mil first */
426 orig_mil = sp->sct_item[I_MILIT];
428 if (mc <= orig_mil) {
429 sp->sct_item[I_MILIT] = orig_mil - mc;
432 sp->sct_item[I_MILIT] = 0;
434 /* remaining casualites */
438 * Need to take total_casualties and divide
439 * them amongst the land units in the sector
440 * Do security troops first, then others.
441 * Try not to kill any unit.
443 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
444 while (NULL != (lp = nxtitemp(&ni))) {
445 if (lp->lnd_own != sp->sct_own)
447 if (lp->lnd_ship >= 0)
450 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
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];
471 if (cantake >= each) {
472 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
474 } else if (cantake > 0) {
475 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
480 lp->lnd_effic -= deq;
481 lp->lnd_mobil -= deq / 2;
482 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
488 /* kill some normal troops */
489 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
490 while (NULL != (lp = nxtitemp(&ni))) {
491 if (lp->lnd_own != sp->sct_own)
493 if (lp->lnd_ship >= 0)
495 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
498 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
500 if (cantake >= each) {
501 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
503 } else if (cantake > 0) {
504 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
509 lp->lnd_effic -= deq;
510 lp->lnd_mobil -= deq / 2;
511 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
517 /* Hmm.. still some left.. kill off units now */
518 /* kill some normal troops */
519 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
520 while (NULL != (lp = nxtitemp(&ni))) {
521 if (lp->lnd_own != sp->sct_own)
523 if (lp->lnd_ship >= 0)
525 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
528 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
529 lnd_dies_fighting_che(lp);
534 /* Hmm.. still some left.. kill off units now */
535 /* kill some security troops */
536 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
537 while (NULL != (lp = nxtitemp(&ni))) {
538 if (lp->lnd_own != sp->sct_own)
540 if (lp->lnd_ship >= 0)
542 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
545 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
546 lnd_dies_fighting_che(lp);
551 /* Hmm.. everyone dead.. too bad */
555 lnd_dies_fighting_che(struct lndstr *lp)
563 lnd_submil(lp, 1000); /* Remove 'em all */
564 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
565 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
566 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
569 /* Take dead lp off its carrier */
570 if (lp->lnd_land >= 0) {
571 lnd_carrier_change(lp, EF_LAND, lp->lnd_land, -1);
575 /* Unload lp's land unit cargo */
576 for (i = lnd_first_on_land(lp); i >= 0; i = lnd_next_on_unit(i)) {
578 if (CANT_HAPPEN(!clp))
580 lnd_carrier_change(clp, EF_LAND, clp->lnd_land, -1);
584 /* Destroy lp's plane cargo */
585 for (i = pln_first_on_land(lp); i >= 0; i = pln_next_on_unit(i)) {
587 if (CANT_HAPPEN(!cpp))
589 pln_carrier_change(cpp, EF_LAND, cpp->pln_land, -1);
590 makelost(EF_PLANE, cpp->pln_own, i, cpp->pln_x, cpp->pln_y);
591 wu(0, cpp->pln_own, "%s lost!\n", prplane(cpp));
596 j = nuk_on_plane(cpp);
599 if (CANT_HAPPEN(!cnp))
601 nuk_carrier_change(cnp, EF_PLANE, cnp->nuk_plane, -1);
602 makelost(EF_NUKE, cnp->nuk_own, j, cnp->nuk_x, cnp->nuk_y);
603 wu(0, cnp->nuk_own, "%s lost!\n", prnuke(cnp));