2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * revolt.c: Have disloyal populace revolt!
30 * Known contributors to this file:
32 * Steve McClure, 1997-2000
53 static void take_casualties(struct sctstr *, int);
56 revolt(struct sctstr *sp)
66 if (che != 0 && (sp->sct_che_target != sp->sct_own || che >= CHE_MAX))
68 civ = sp->sct_item[I_CIVIL];
69 uw = sp->sct_item[I_UW];
70 if (che > (civ + uw) * 3)
74 /* che due to civilian unrest */
75 n = 10 - (random() % 20);
76 che_civ = 3 + (civ * n / 500);
79 else if (che_civ * 3 > civ)
81 if (che + che_civ > CHE_MAX)
82 che_civ = CHE_MAX - che;
85 /* che due to uw unrest */
86 n = 10 + (random() % 30);
87 che_uw = 5 + (uw * n / 500);
90 if (che + che_uw > CHE_MAX)
91 che_uw = CHE_MAX - che_uw;
94 if (che_civ + che_uw > 0) {
97 sp->sct_che_target = sp->sct_own;
100 sp->sct_item[I_CIVIL] = civ;
102 sp->sct_item[I_UW] = uw;
104 logerror("(#%d) %d che fired up in %s",
105 sp->sct_own, che, ownxy(sp));
111 * summary of effects.
112 * if there are no military in the sector, che recruit from
113 * populace if pop loyalty is > 10. They spread subversion otherwise,
114 * trying to lower pop loyalty.
115 * if che outnumber military, they stay and shoot it out, kill the
117 * if che are outnumbered by less than 5 to 1, they blow up stuff,
118 * killing innocent civilians (never uw's) and damaging commodities.
119 * if che are outnumbered by more than 5 to 1, they try to leave the
120 * sector for a nearby sector with fewer military.
122 * if the military lose any attacks, the pop loyalty in the sector
123 * gets worse, representing military defeat.
124 * military can "catch" che's after bombing attacks, or after they move.
125 * If military catch them, then they get to shoot it out with a portion
126 * of the che's depending on the # of mil in the sector. Chance to contact
127 * is around 10% per every equal number of mil:che ratio in the sector.
128 * "contact" is by 20% of the military in the sector, and odds are equal.
130 * Without a doubt this routine should be broken up, if only for readabilty.
133 guerrilla(struct sctstr *sp)
164 civ = sp->sct_item[I_CIVIL];
165 uw = sp->sct_item[I_UW];
166 victim = sp->sct_own;
167 actor = sp->sct_oldown;
169 mil = sp->sct_item[I_MILIT];
171 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
173 while (NULL != (lp = nxtitemp(&ni))) {
174 if (lp->lnd_own != sp->sct_own)
177 mil += lp->lnd_item[I_MILIT];
179 /* Security troops can now kill up to 1/2 their complement each
180 update, before doing anything else. */
181 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
184 mil += lp->lnd_item[I_MILIT];
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) {
219 /*logerror("own %d != target %d", sp->sct_own, target); */
225 odds = (double)che / (mil + che);
226 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
228 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
232 } else if (che > mil && mil > 0) {
233 /*logerror("guerrilla shootout with military"); */
235 * shoot it out with the military, and kill them off.
236 * If loyalty bad enough, then take the sector over,
237 * and enlist 5% of civ as military force.
239 while (che > 0 && mil > 0) {
250 n = sp->sct_loyal - (random() % 15);
254 /*logerror("(#%d) mil beat che in %s", sp->sct_own, */
259 /*logerror("(#%d) che beat mil in %s", sp->sct_own, */
262 take_casualties(sp, mc);
263 } else if (ratio < 5) {
265 * guerrillas have to resort to blowing things up.
266 * Note this disrupts work in the sector.
269 n = (random() % 10) + (random() % che);
272 tmp = sp->sct_work - n;
277 "Production %s disrupted by terrorists in %s\n",
278 effadv(n), ownxy(sp));
279 sect_damage(sp, n / 10, 0);
280 /*logerror("(#%d) che blew up %s for %d", sp->sct_own, */
285 /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own, */
286 /*che, mil, ownxy(sp)); */
289 if (mil > 0 && che > 0) {
291 * we only get here if we haven't had combat previously.
292 * Chance to catch them.
293 * 20% of mil involved in attacking the che's.
295 if (chance(ratio * 0.10)) {
297 odds = (double)che / (n + che);
298 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
299 while (che > 0 && n > 0) {
308 take_casualties(sp, mc);
310 /*logerror("Caught che; mc: %d, cc: %d", cc, mc); */
313 if (convert && sp->sct_loyal >= 50) {
315 /* new owner gets to keep the mobility there */
316 oldmob = sp->sct_mobil;
317 /* che won, and sector converts. */
318 if (sp->sct_own == sp->sct_oldown)
321 takeover(sp, sp->sct_oldown);
322 sp->sct_mobil = oldmob;
327 if (civ > ITEM_MAX) {
331 sp->sct_item[I_CIVIL] = civ;
332 sp->sct_item[I_UW] = uw;
333 sp->sct_item[I_MILIT] = n;
337 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
338 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
340 if (recruit && che > 0) {
341 /* loyalty drops during recruitment efforts */
344 n += (random() % 5) + 1;
346 n += (random() % 10) + 4;
350 if (sp->sct_oldown != sp->sct_own || n > 100) {
351 n = civ * (random() % 3) / 200;
352 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
353 if (n + che > CHE_MAX)
357 sp->sct_item[I_CIVIL] = civ;
359 n = uw * (random() % 3) / 200;
360 if (n + che > CHE_MAX)
364 sp->sct_item[I_UW] = uw;
367 if (move && che > 0) {
368 struct sctstr *nicest_sp = 0;
373 /* search adjacent sectors for a nice one */
374 for (n = 1; n <= 6; n++) {
375 nsp = getsectp(sp->sct_x + diroff[n][0],
376 sp->sct_y + diroff[n][1]);
377 if (dchr[nsp->sct_type].d_mob0 < 0)
379 if (nsp->sct_own != target)
381 if (nsp->sct_che > 0) {
382 if (nsp->sct_che_target != target)
384 if (nsp->sct_che + che > CHE_MAX)
387 val = nsp->sct_item[I_MILIT];
388 /* don't give che more precise info than spy */
389 val = roundintby(val, 10);
390 /* inject a modicum of indeterminism; also
391 * avoids che preferring certain directions */
392 val += random() % 10 - 5;
398 /* if we found a nice sector, go there */
399 if (nicest_sp != 0) {
400 nicest_sp->sct_che += che;
401 nicest_sp->sct_che_target = target;
407 sp->sct_che_target = target;
410 sp->sct_che_target = 0;
412 if (mc > 0 || cc > 0) {
414 "Guerrilla warfare in %s\n",
415 xyas(sp->sct_x, sp->sct_y, target));
416 if (sp->sct_own == target)
417 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
420 " rebels murder %d military\n", mc);
421 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
423 if (sp->sct_own != victim)
424 wu(0, victim, "Partisans take over %s!\n",
425 xyas(sp->sct_x, sp->sct_y, victim));
429 take_casualties(struct sctstr *sp, int mc)
433 int nunits = 0, each, deq;
437 /* casualties come out of mil first */
438 orig_mil = sp->sct_item[I_MILIT];
440 if (mc <= orig_mil) {
441 sp->sct_item[I_MILIT] = orig_mil - mc;
444 sp->sct_item[I_MILIT] = 0;
446 /* remaining casualites */
450 * Need to take total_casualties and divide
451 * them amongst the land units in the sector
452 * Do security troops first, then others.
453 * Try not to kill any unit.
455 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
456 while (NULL != (lp = nxtitemp(&ni))) {
458 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
465 each = (mc / nunits) + 2;
467 /* kill some security troops */
468 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
469 while (NULL != (lp = nxtitemp(&ni))) {
470 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
473 cantake = ((lp->lnd_effic - 40) / 100.0)
474 * lp->lnd_item[I_MILIT] * 2.0;
476 if (cantake >= each) {
477 deq = (each / (lp->lnd_item[I_MILIT] * 2.0)) * 100.0;
479 } else if (cantake > 0) {
480 deq = (cantake / (lp->lnd_item[I_MILIT] * 2.0)) * 100.0;
481 mc -= (deq / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
485 lp->lnd_effic -= deq;
486 lp->lnd_mobil -= deq / 2;
487 deq = lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
493 /* kill some normal troops */
494 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
495 while (NULL != (lp = nxtitemp(&ni))) {
496 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
499 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
501 if (cantake >= each) {
502 deq = ((double)each / (lp->lnd_item[I_MILIT] * 2.0)) * 100.0;
504 } else if (cantake > 0) {
505 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
506 mc -= (deq / 100.0) * lp->lnd_item[I_MILIT];
510 lp->lnd_effic -= deq;
511 lp->lnd_mobil -= deq / 2;
512 deq = lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
518 /* Hmm.. still some left.. kill off units now */
519 /* kill some normal troops */
520 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
521 while (NULL != (lp = nxtitemp(&ni))) {
522 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
525 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
527 lnd_submil(lp, 1000); /* Remove 'em all */
528 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
529 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
530 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
536 /* Hmm.. still some left.. kill off units now */
537 /* kill some security troops */
538 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
539 while (NULL != (lp = nxtitemp(&ni))) {
540 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
543 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
545 lnd_submil(lp, 1000); /* Kill 'em all */
546 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
547 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
548 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
554 /* Hmm.. everyone dead.. too bad */