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
44 static void take_casualties(struct sctstr *, int);
45 static void lnd_dies_fighting_che(struct lndstr *);
48 revolt(struct sctstr *sp)
58 if (che != 0 && (sp->sct_che_target != sp->sct_own || che >= CHE_MAX))
60 civ = sp->sct_item[I_CIVIL];
61 uw = sp->sct_item[I_UW];
62 if (che > (civ + uw) * 3)
65 /* che due to civilian unrest */
66 n = 10 - (random() % 20);
67 che_civ = 3 + (civ * n / 500);
70 else if (che_civ * 3 > civ)
72 if (che + che_civ > CHE_MAX)
73 che_civ = CHE_MAX - che;
76 /* che due to uw unrest */
77 n = 10 + (random() % 30);
78 che_uw = 5 + (uw * n / 500);
81 if (che + che_uw > CHE_MAX)
82 che_uw = CHE_MAX - che_uw;
85 if (che_civ + che_uw > 0) {
88 sp->sct_che_target = sp->sct_own;
91 sp->sct_item[I_CIVIL] = civ;
93 sp->sct_item[I_UW] = uw;
99 * if there are no military in the sector, che recruit from
100 * populace if pop loyalty is > 10. They spread subversion otherwise,
101 * trying to lower pop loyalty.
102 * if che outnumber military, they stay and shoot it out, kill the
104 * if che are outnumbered by less than 5 to 1, they blow up stuff,
105 * killing innocent civilians (never uw's) and damaging commodities.
106 * if che are outnumbered by more than 5 to 1, they try to leave the
107 * sector for a nearby sector with fewer military.
109 * if the military lose any attacks, the pop loyalty in the sector
110 * gets worse, representing military defeat.
111 * military can "catch" che's after bombing attacks, or after they move.
112 * If military catch them, then they get to shoot it out with a portion
113 * of the che's depending on the # of mil in the sector. Chance to contact
114 * is around 10% per every equal number of mil:che ratio in the sector.
115 * "contact" is by 20% of the military in the sector, and odds are equal.
117 * Without a doubt this routine should be broken up, if only for readabilty.
120 guerrilla(struct sctstr *sp)
151 civ = sp->sct_item[I_CIVIL];
152 uw = sp->sct_item[I_UW];
153 victim = sp->sct_own;
154 actor = sp->sct_oldown;
156 mil = sp->sct_item[I_MILIT];
158 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
160 while (NULL != (lp = nxtitemp(&ni))) {
161 if (lp->lnd_own != sp->sct_own)
163 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
166 mil += lp->lnd_item[I_MILIT];
168 if (sp->sct_che_target != sp->sct_own)
171 /* Security troops can now kill up to 1/5 their complement each
172 update, before doing anything else. */
173 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
176 mil += lp->lnd_item[I_MILIT];
177 r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
178 che_kill = r < 1 ? 0 : roll(r);
183 "%s kills %d guerrilla%s in raid at %s!\n",
184 prland(lp), che_kill, splur(che_kill), ownxy(sp));
190 /* Security forces killed all the che */
193 sp->sct_che_target = 0;
197 target = sp->sct_che_target;
198 if (CANT_HAPPEN(target == 0))
200 tnat = getnatp(target);
201 if (tnat->nat_stat == STAT_UNUSED) {
202 /* target nation has dissolved: che's retire. */
203 logerror("%d Che targeted at country %d retiring", che, target);
205 sp->sct_che_target = 0;
206 sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
210 if (sp->sct_own != target) {
216 odds = (double)che / (mil + che);
217 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
219 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
223 } else if (che > mil && mil > 0) {
225 * shoot it out with the military, and kill them off.
226 * If loyalty bad enough, then take the sector over,
227 * and enlist 5% of civ as military force.
229 while (che > 0 && mil > 0) {
240 n = sp->sct_loyal - (random() % 15);
248 take_casualties(sp, mc);
249 } else if (ratio < 5) {
251 * guerrillas have to resort to blowing things up.
252 * Note this disrupts work in the sector.
254 n = (random() % 10) + (random() % che);
257 tmp = sp->sct_work - n;
262 "Production %s disrupted by terrorists in %s\n",
263 effadv(n), ownxy(sp));
264 sect_damage(sp, n / 10);
270 if (mil > 0 && che > 0) {
272 * we only get here if we haven't had combat previously.
273 * Chance to catch them.
274 * 20% of mil involved in attacking the che's.
276 if (chance(ratio * 0.10)) {
278 odds = (double)che / (n + che);
279 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
280 while (che > 0 && n > 0) {
289 take_casualties(sp, mc);
293 if (convert && sp->sct_loyal >= 50) {
295 /* new owner gets to keep the mobility there */
296 oldmob = sp->sct_mobil;
297 /* che won, and sector converts. */
298 if (sp->sct_own == sp->sct_oldown)
301 lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
302 0, sp->sct_x, sp->sct_y);
303 takeover(sp, sp->sct_oldown);
305 sp->sct_mobil = oldmob;
310 if (civ > ITEM_MAX) {
314 sp->sct_item[I_CIVIL] = civ;
315 sp->sct_item[I_UW] = uw;
316 sp->sct_item[I_MILIT] = n;
320 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
321 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
323 if (recruit && che > 0) {
324 /* loyalty drops during recruitment efforts */
327 n += (random() % 5) + 1;
329 n += (random() % 10) + 4;
333 if (sp->sct_oldown != sp->sct_own || n > 100) {
334 n = civ * (random() % 3) / 200;
335 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
336 if (n + che > CHE_MAX)
340 sp->sct_item[I_CIVIL] = civ;
342 n = uw * (random() % 3) / 200;
343 if (n + che > CHE_MAX)
347 sp->sct_item[I_UW] = uw;
350 if (move && che > 0) {
351 struct sctstr *nicest_sp = NULL;
356 /* search adjacent sectors for a nice one */
357 for (n = 1; n <= 6; n++) {
358 nsp = getsectp(sp->sct_x + diroff[n][0],
359 sp->sct_y + diroff[n][1]);
360 if (dchr[nsp->sct_type].d_mob0 < 0)
362 if (nsp->sct_own != target)
364 if (nsp->sct_che > 0) {
365 if (nsp->sct_che_target != target)
367 if (nsp->sct_che + che > CHE_MAX)
370 val = nsp->sct_item[I_MILIT];
371 /* don't give che more precise info than spy */
372 val = roundintby(val, 10);
373 /* inject a modicum of indeterminism; also
374 * avoids che preferring certain directions */
375 val += random() % 10 - 5;
381 /* if we found a nice sector, go there */
383 nicest_sp->sct_che += che;
384 nicest_sp->sct_che_target = target;
390 sp->sct_che_target = target;
393 sp->sct_che_target = 0;
395 if (mc > 0 || cc > 0) {
397 "Guerrilla warfare in %s\n",
398 xyas(sp->sct_x, sp->sct_y, target));
399 if (sp->sct_own == target)
400 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
402 wu(0, target, " rebels murder %d military\n", mc);
403 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
405 if (sp->sct_own != victim)
406 wu(0, victim, "Partisans take over %s!\n",
407 xyas(sp->sct_x, sp->sct_y, victim));
411 take_casualties(struct sctstr *sp, int mc)
415 int nunits = 0, each, deq;
419 /* casualties come out of mil first */
420 orig_mil = sp->sct_item[I_MILIT];
422 if (mc <= orig_mil) {
423 sp->sct_item[I_MILIT] = orig_mil - mc;
426 sp->sct_item[I_MILIT] = 0;
428 /* remaining casualites */
432 * Need to take total_casualties and divide
433 * them amongst the land units in the sector
434 * Do security troops first, then others.
435 * Try not to kill any unit.
437 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
438 while (NULL != (lp = nxtitemp(&ni))) {
439 if (lp->lnd_own != sp->sct_own)
441 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
444 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
451 each = (mc / nunits) + 2;
453 /* kill some security troops */
454 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
455 while (NULL != (lp = nxtitemp(&ni))) {
456 if (lp->lnd_own != sp->sct_own)
458 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
460 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
463 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
465 if (cantake >= each) {
466 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
468 } else if (cantake > 0) {
469 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
474 lp->lnd_effic -= deq;
475 lp->lnd_mobil -= deq / 2;
476 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
482 /* kill some normal troops */
483 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
484 while (NULL != (lp = nxtitemp(&ni))) {
485 if (lp->lnd_own != sp->sct_own)
487 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
489 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
492 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
494 if (cantake >= each) {
495 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
497 } else if (cantake > 0) {
498 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
503 lp->lnd_effic -= deq;
504 lp->lnd_mobil -= deq / 2;
505 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
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 || lp->lnd_land >= 0)
519 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
522 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
523 lnd_dies_fighting_che(lp);
528 /* Hmm.. still some left.. kill off units now */
529 /* kill some security troops */
530 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
531 while (NULL != (lp = nxtitemp(&ni))) {
532 if (lp->lnd_own != sp->sct_own)
534 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
536 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
539 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
540 lnd_dies_fighting_che(lp);
545 /* Hmm.. everyone dead.. too bad */
549 lnd_dies_fighting_che(struct lndstr *lp)
552 lnd_submil(lp, 1000); /* Remove 'em all */
553 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
554 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
555 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);