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 += lnd_getmil(lp);
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) {
185 lcp = &lchr[(int)lp->lnd_type];
186 mil += lnd_getmil(lp);
187 r = (((float)(lp->lnd_effic / 100) * (float)(lnd_getmil(lp))) /
191 che_kill = (roll(r) - 1);
196 "%s kills %d guerrilla%s in raid at %s!\n",
197 prland(lp), che_kill, splur(che_kill), ownxy(sp));
203 /* Security forces killed all the che */
206 sp->sct_che_target = 0;
210 target = sp->sct_che_target;
212 /* the deity can't be a target! */
215 tnat = getnatp(target);
216 if (tnat->nat_stat == STAT_UNUSED) {
217 /* target nation has dissolved: che's retire. */
218 logerror("%d Che targeted at country %d retiring", che, target);
220 sp->sct_che_target = 0;
221 sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
225 if (sp->sct_own != target) {
226 /*logerror("own %d != target %d", sp->sct_own, target); */
232 odds = (double)che / (mil + che);
233 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
235 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
239 } else if (che > mil && mil > 0) {
240 /*logerror("guerrilla shootout with military"); */
242 * shoot it out with the military, and kill them off.
243 * If loyalty bad enough, then take the sector over,
244 * and enlist 5% of civ as military force.
246 while (che > 0 && mil > 0) {
257 n = sp->sct_loyal - (random() % 15);
261 /*logerror("(#%d) mil beat che in %s", sp->sct_own, */
266 /*logerror("(#%d) che beat mil in %s", sp->sct_own, */
269 take_casualties(sp, mc);
270 } else if (ratio < 5) {
272 * guerrillas have to resort to blowing things up.
273 * Note this disrupts work in the sector.
276 n = (random() % 10) + (random() % che);
279 tmp = sp->sct_work - n;
284 "Production %s disrupted by terrorists in %s\n",
285 effadv(n), ownxy(sp));
286 sect_damage(sp, n / 10, 0);
287 /*logerror("(#%d) che blew up %s for %d", sp->sct_own, */
292 /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own, */
293 /*che, mil, ownxy(sp)); */
296 if (mil > 0 && che > 0) {
298 * we only get here if we haven't had combat previously.
299 * Chance to catch them.
300 * 20% of mil involved in attacking the che's.
302 if (chance(ratio * 0.10)) {
304 if ((n + che) == 0) {
305 logerror("n=%d che=%d\n", n, che);
309 odds = (double)che / (n + che);
310 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
311 while (che > 0 && n > 0) {
320 take_casualties(sp, mc);
322 /*logerror("Caught che; mc: %d, cc: %d", cc, mc); */
325 if (convert && sp->sct_loyal >= 50) {
327 /* new owner gets to keep the mobility there */
328 oldmob = sp->sct_mobil;
329 /* che won, and sector converts. */
330 if (sp->sct_own == sp->sct_oldown)
333 takeover(sp, sp->sct_oldown);
334 sp->sct_mobil = oldmob;
339 if (civ > ITEM_MAX) {
343 sp->sct_item[I_CIVIL] = civ;
344 sp->sct_item[I_UW] = uw;
345 sp->sct_item[I_MILIT] = n;
349 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
350 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
352 if (recruit && che > 0) {
353 /* loyalty drops during recruitment efforts */
356 n += (random() % 5) + 1;
358 n += (random() % 10) + 4;
362 if (sp->sct_oldown != sp->sct_own || n > 100) {
363 n = civ * (random() % 3) / 200;
364 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
365 if (n + che > CHE_MAX)
369 sp->sct_item[I_CIVIL] = civ;
371 n = uw * (random() % 3) / 200;
372 if (n + che > CHE_MAX)
376 sp->sct_item[I_UW] = uw;
379 if (move && che > 0) {
380 struct sctstr *nicest_sp = 0;
385 /* search adjacent sectors for a nice one */
386 for (n = 1; n <= 6; n++) {
387 nsp = getsectp(sp->sct_x + diroff[n][0],
388 sp->sct_y + diroff[n][1]);
389 if (dchr[nsp->sct_type].d_mcst == 0)
391 if (nsp->sct_own != target)
393 if (nsp->sct_che > 0) {
394 if (nsp->sct_che_target != target)
396 if (nsp->sct_che + che > CHE_MAX)
399 val = nsp->sct_item[I_MILIT];
400 /* don't give che more precise info than spy */
401 val = roundintby(val, 10);
402 /* inject a modicum of indeterminism; also
403 * avoids che preferring certain directions */
404 val += random() % 10 - 5;
410 /* if we found a nice sector, go there */
411 if (nicest_sp != 0) {
412 nicest_sp->sct_che += che;
413 nicest_sp->sct_che_target = target;
419 sp->sct_che_target = target;
422 sp->sct_che_target = 0;
424 if (mc > 0 || cc > 0) {
425 /* don't tell who won just to be mean */
427 "Guerrilla warfare in %s\n",
428 xyas(sp->sct_x, sp->sct_y, target));
429 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
430 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
435 take_casualties(struct sctstr *sp, int mc)
439 int nunits = 0, each, deq;
443 /* casualties come out of mil first */
444 orig_mil = sp->sct_item[I_MILIT];
446 if (mc <= orig_mil) {
447 sp->sct_item[I_MILIT] = orig_mil - mc;
450 sp->sct_item[I_MILIT] = 0;
452 /* remaining casualites */
456 * Need to take total_casualties and divide
457 * them amongst the land units in the sector
458 * Do security troops first, then others.
459 * Try not to kill any unit.
461 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
462 while (NULL != (lp = nxtitemp(&ni))) {
464 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
471 each = (mc / nunits) + 2;
473 /* kill some security troops */
474 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
475 while (NULL != (lp = nxtitemp(&ni))) {
476 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
479 cantake = (((float)(lp->lnd_effic - 40) / 100.0) *
480 (float)lnd_getmil(lp)) * 2;
481 /* (float)lchr[lp->lnd_type].l_mil)*2; */
483 if (cantake >= each) {
484 /* deq = (((float)each/(float)(lchr[lp->lnd_type].l_mil*2)) */
485 deq = (((float)each / (float)(lnd_getmil(lp) * 2))
488 } else if (cantake > 0) {
489 deq = (((float)cantake / (float)(lnd_getmil(lp) * 2)) * 100.0);
490 /* (float)(lchr[lp->lnd_type].l_mil*2)) * 100.0); */
491 mc -= (((float)deq / 100.0) * (float)lnd_getmil(lp)) * 2;
492 /* (float)lchr[lp->lnd_type].l_mil)*2; */
496 lp->lnd_effic -= deq;
497 lp->lnd_mobil -= deq / 2;
498 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
504 /* kill some normal troops */
505 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
506 while (NULL != (lp = nxtitemp(&ni))) {
507 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
510 cantake = (((float)(lp->lnd_effic - 40) / 100.0) *
511 (float)lnd_getmil(lp));
513 if (cantake >= each) {
514 deq = (((float)each / (float)(lnd_getmil(lp) * 2))
517 } else if (cantake > 0) {
518 deq = (((float)cantake / (float)lnd_getmil(lp))
520 mc -= (((float)deq / 100.0) * (float)lnd_getmil(lp));
523 lp->lnd_effic -= deq;
524 lp->lnd_mobil -= deq / 2;
525 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
531 /* Hmm.. still some left.. kill off units now */
532 /* kill some normal troops */
533 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
534 while (NULL != (lp = nxtitemp(&ni))) {
535 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
538 mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp));
540 lnd_submil(lp, 1000); /* Remove 'em all */
541 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
542 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
543 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
549 /* Hmm.. still some left.. kill off units now */
550 /* kill some security troops */
551 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
552 while (NULL != (lp = nxtitemp(&ni))) {
553 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
556 mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp)) * 2;
558 lnd_submil(lp, 1000); /* Kill 'em all */
559 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
560 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
561 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
567 /* Hmm.. everyone dead.. too bad */