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
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-2011
46 static void take_casualties(struct sctstr *, int);
47 static void lnd_dies_fighting_che(struct lndstr *);
50 revolt(struct sctstr *sp)
60 if (che != 0 && (sp->sct_che_target != sp->sct_own || che >= CHE_MAX))
62 civ = sp->sct_item[I_CIVIL];
63 uw = sp->sct_item[I_UW];
64 if (che > (civ + uw) * 3)
67 /* che due to civilian unrest */
68 n = 10 - (random() % 20);
69 che_civ = 3 + (civ * n / 500);
72 else if (che_civ * 3 > civ)
74 if (che + che_civ > CHE_MAX)
75 che_civ = CHE_MAX - che;
78 /* che due to uw unrest */
79 n = 10 + (random() % 30);
80 che_uw = 5 + (uw * n / 500);
83 if (che + che_uw > CHE_MAX)
84 che_uw = CHE_MAX - che_uw;
87 if (che_civ + che_uw > 0) {
90 sp->sct_che_target = sp->sct_own;
93 sp->sct_item[I_CIVIL] = civ;
95 sp->sct_item[I_UW] = uw;
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
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.
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.
119 * Without a doubt this routine should be broken up, if only for readabilty.
122 guerrilla(struct sctstr *sp)
153 civ = sp->sct_item[I_CIVIL];
154 uw = sp->sct_item[I_UW];
155 victim = sp->sct_own;
156 actor = sp->sct_oldown;
158 mil = sp->sct_item[I_MILIT];
160 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
162 while (NULL != (lp = nxtitemp(&ni))) {
163 if (lp->lnd_own != sp->sct_own)
165 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
168 mil += lp->lnd_item[I_MILIT];
170 if (sp->sct_che_target != sp->sct_own)
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) {
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);
185 "%s kills %d guerrilla%s in raid at %s!\n",
186 prland(lp), che_kill, splur(che_kill), ownxy(sp));
192 /* Security forces killed all the che */
195 sp->sct_che_target = 0;
199 target = sp->sct_che_target;
200 if (CANT_HAPPEN(target == 0))
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);
207 sp->sct_che_target = 0;
208 sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
212 if (sp->sct_own != target) {
218 odds = (double)che / (mil + che);
219 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
221 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
225 } else if (che > mil && mil > 0) {
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.
231 while (che > 0 && mil > 0) {
242 n = sp->sct_loyal - (random() % 15);
250 take_casualties(sp, mc);
251 } else if (ratio < 5) {
253 * guerrillas have to resort to blowing things up.
254 * Note this disrupts work in the sector.
256 n = (random() % 10) + (random() % che);
259 tmp = sp->sct_work - n;
264 "Production %s disrupted by terrorists in %s\n",
265 effadv(n), ownxy(sp));
266 sect_damage(sp, n / 10);
272 if (mil > 0 && che > 0) {
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.
278 if (chance(ratio * 0.10)) {
280 odds = (double)che / (n + che);
281 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
282 while (che > 0 && n > 0) {
291 take_casualties(sp, mc);
295 if (convert && sp->sct_loyal >= 50) {
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)
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);
307 sp->sct_mobil = oldmob;
312 if (civ > ITEM_MAX) {
316 sp->sct_item[I_CIVIL] = civ;
317 sp->sct_item[I_UW] = uw;
318 sp->sct_item[I_MILIT] = n;
322 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
323 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
325 if (recruit && che > 0) {
326 /* loyalty drops during recruitment efforts */
329 n += (random() % 5) + 1;
331 n += (random() % 10) + 4;
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)
342 sp->sct_item[I_CIVIL] = civ;
344 n = uw * (random() % 3) / 200;
345 if (n + che > CHE_MAX)
349 sp->sct_item[I_UW] = uw;
352 if (move && che > 0) {
353 struct sctstr *nicest_sp = NULL;
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)
364 if (nsp->sct_own != target)
366 if (nsp->sct_che > 0) {
367 if (nsp->sct_che_target != target)
369 if (nsp->sct_che + che > CHE_MAX)
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;
383 /* if we found a nice sector, go there */
385 nicest_sp->sct_che += che;
386 nicest_sp->sct_che_target = target;
392 sp->sct_che_target = target;
395 sp->sct_che_target = 0;
397 if (mc > 0 || cc > 0) {
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);
404 wu(0, target, " rebels murder %d military\n", mc);
405 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
407 if (sp->sct_own != victim)
408 wu(0, victim, "Partisans take over %s!\n",
409 xyas(sp->sct_x, sp->sct_y, victim));
413 take_casualties(struct sctstr *sp, int mc)
417 int nunits = 0, each, deq;
421 /* casualties come out of mil first */
422 orig_mil = sp->sct_item[I_MILIT];
424 if (mc <= orig_mil) {
425 sp->sct_item[I_MILIT] = orig_mil - mc;
428 sp->sct_item[I_MILIT] = 0;
430 /* remaining casualites */
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.
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)
443 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
446 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
453 each = (mc / nunits) + 2;
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)
460 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
462 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
465 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
467 if (cantake >= each) {
468 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
470 } else if (cantake > 0) {
471 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
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);
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)
489 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
491 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
494 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
496 if (cantake >= each) {
497 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
499 } else if (cantake > 0) {
500 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
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);
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)
519 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
521 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
524 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
525 lnd_dies_fighting_che(lp);
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)
536 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
538 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
541 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
542 lnd_dies_fighting_che(lp);
547 /* Hmm.. everyone dead.. too bad */
551 lnd_dies_fighting_che(struct lndstr *lp)
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);
565 /* Unload lp's land unit cargo */
566 for (i = lnd_first_on_land(lp); i >= 0; i = lnd_next_on_unit(i)) {
568 if (CANT_HAPPEN(!clp))
570 lnd_carrier_change(clp, EF_LAND, clp->lnd_land, -1);
574 /* Destroy lp's plane cargo */
575 for (i = pln_first_on_land(lp); i >= 0; i = pln_next_on_unit(i)) {
577 if (CANT_HAPPEN(!cpp))
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));
586 j = nuk_on_plane(cpp);
589 if (CANT_HAPPEN(!cnp))
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));