2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * 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
51 static void take_casualties(struct sctstr *, int);
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)
72 /* che due to civilian unrest */
73 n = 10 - (random() % 20);
74 che_civ = 3 + (civ * n / 500);
77 else if (che_civ * 3 > civ)
79 if (che + che_civ > CHE_MAX)
80 che_civ = CHE_MAX - che;
83 /* che due to uw unrest */
84 n = 10 + (random() % 30);
85 che_uw = 5 + (uw * n / 500);
88 if (che + che_uw > CHE_MAX)
89 che_uw = CHE_MAX - che_uw;
92 if (che_civ + che_uw > 0) {
95 sp->sct_che_target = sp->sct_own;
98 sp->sct_item[I_CIVIL] = civ;
100 sp->sct_item[I_UW] = uw;
102 logerror("(#%d) %d che fired up in %s",
103 sp->sct_own, che, ownxy(sp));
109 * summary of effects.
110 * if there are no military in the sector, che recruit from
111 * populace if pop loyalty is > 10. They spread subversion otherwise,
112 * trying to lower pop loyalty.
113 * if che outnumber military, they stay and shoot it out, kill the
115 * if che are outnumbered by less than 5 to 1, they blow up stuff,
116 * killing innocent civilians (never uw's) and damaging commodities.
117 * if che are outnumbered by more than 5 to 1, they try to leave the
118 * sector for a nearby sector with fewer military.
120 * if the military lose any attacks, the pop loyalty in the sector
121 * gets worse, representing military defeat.
122 * military can "catch" che's after bombing attacks, or after they move.
123 * If military catch them, then they get to shoot it out with a portion
124 * of the che's depending on the # of mil in the sector. Chance to contact
125 * is around 10% per every equal number of mil:che ratio in the sector.
126 * "contact" is by 20% of the military in the sector, and odds are equal.
128 * Without a doubt this routine should be broken up, if only for readabilty.
131 guerrilla(struct sctstr *sp)
162 civ = sp->sct_item[I_CIVIL];
163 uw = sp->sct_item[I_UW];
164 victim = sp->sct_own;
165 actor = sp->sct_oldown;
167 mil = sp->sct_item[I_MILIT];
169 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
171 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni))) {
172 if (lp->lnd_own != sp->sct_own)
175 mil += lnd_getmil(lp);
177 /* Security troops can now kill up to 1/2 their complement each
178 update, before doing anything else. */
179 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
183 lcp = &lchr[(int)lp->lnd_type];
184 mil += lnd_getmil(lp);
185 r = (((float)(lp->lnd_effic / 100) * (float)(lnd_getmil(lp))) /
189 che_kill = (roll(r) - 1);
194 "%s kills %d guerrilla%s in raid at %s!\n",
195 prland(lp), che_kill, splur(che_kill), ownxy(sp));
201 /* Security forces killed all the che */
204 sp->sct_che_target = 0;
208 target = sp->sct_che_target;
210 /* the deity can't be a target! */
213 tnat = getnatp(target);
214 if ((tnat->nat_stat & STAT_INUSE) == 0) {
215 /* target nation has dissolved: che's retire. */
216 logerror("%d Che targeted at country %d retiring", che, target);
218 sp->sct_che_target = 0;
219 sp->sct_item[I_CIVIL] = min(civ + che, ITEM_MAX);
223 if (sp->sct_own != target) {
224 /*logerror("own %d != target %d", sp->sct_own, target); */
230 odds = (double)che / (mil + che);
231 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
233 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
237 } else if (che > mil && mil > 0) {
238 /*logerror("guerrilla shootout with military"); */
240 * shoot it out with the military, and kill them off.
241 * If loyalty bad enough, then take the sector over,
242 * and enlist 5% of civ as military force.
244 while (che > 0 && mil > 0) {
255 n = sp->sct_loyal - (random() % 15);
259 /*logerror("(#%d) mil beat che in %s", sp->sct_own, */
264 /*logerror("(#%d) che beat mil in %s", sp->sct_own, */
267 take_casualties(sp, mc);
268 } else if (ratio < 5) {
270 * guerrillas have to resort to blowing things up.
271 * Note this disrupts work in the sector.
274 n = (random() % 10) + (random() % che);
277 tmp = sp->sct_work - n;
282 "Production %s disrupted by terrorists in %s\n",
283 effadv(n), ownxy(sp));
284 sect_damage(sp, n / 10, 0);
285 /*logerror("(#%d) che blew up %s for %d", sp->sct_own, */
290 /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own, */
291 /*che, mil, ownxy(sp)); */
294 if (mil > 0 && che > 0) {
296 * we only get here if we haven't had combat previously.
297 * Chance to catch them.
298 * 20% of mil involved in attacking the che's.
300 if (chance(ratio * 0.10)) {
302 if ((n + che) == 0) {
303 logerror("n=%d che=%d\n", n, che);
307 odds = (double)che / (n + che);
308 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
309 while (che > 0 && n > 0) {
318 take_casualties(sp, mc);
320 /*logerror("Caught che; mc: %d, cc: %d", cc, mc); */
323 if (convert && sp->sct_loyal >= 50) {
325 /* new owner gets to keep the mobility there */
326 oldmob = sp->sct_mobil;
327 /* che won, and sector converts. */
328 if (sp->sct_own == sp->sct_oldown)
331 takeover(sp, sp->sct_oldown);
332 sp->sct_mobil = oldmob;
336 * so we can't keep losing money by having
339 if (sp->sct_type == SCT_CAPIT && sp->sct_newtype == SCT_CAPIT)
340 sp->sct_newtype = SCT_AGRI;
343 if (civ > ITEM_MAX) {
347 sp->sct_item[I_CIVIL] = civ;
348 sp->sct_item[I_UW] = uw;
349 sp->sct_item[I_MILIT] = n;
353 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
354 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
356 if (recruit && che > 0) {
357 /* loyalty drops during recruitment efforts */
360 n += (random() % 5) + 1;
362 n += (random() % 10) + 4;
366 if (sp->sct_oldown != sp->sct_own || n > 100) {
367 n = civ * (random() % 3) / 200;
368 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
369 if (n + che > CHE_MAX)
373 sp->sct_item[I_CIVIL] = civ;
375 n = uw * (random() % 3) / 200;
376 if (n + che > CHE_MAX)
380 sp->sct_item[I_UW] = uw;
383 if (move && che > 0) {
384 struct sctstr *nicest_sp = 0;
389 /* search adjacent sectors for a nice one */
390 for (n = 1; n <= 6; n++) {
391 nsp = getsectp(sp->sct_x + diroff[n][0],
392 sp->sct_y + diroff[n][1]);
393 if (dchr[nsp->sct_type].d_mcst == 0)
395 if (nsp->sct_own != target)
397 if (nsp->sct_che > 0) {
398 if (nsp->sct_che_target != target)
400 if (nsp->sct_che + che > CHE_MAX)
403 val = nsp->sct_item[I_MILIT];
404 /* don't give che more precise info than spy */
405 val = roundintby(val, 10);
406 /* inject a modicum of indeterminism; also
407 * avoids che preferring certain directions */
408 val += random() % 10 - 5;
414 /* if we found a nice sector, go there */
415 if (nicest_sp != 0) {
416 nicest_sp->sct_che += che;
417 nicest_sp->sct_che_target = target;
423 sp->sct_che_target = target;
426 sp->sct_che_target = 0;
428 if (mc > 0 || cc > 0) {
429 /* don't tell who won just to be mean */
431 "Guerrilla warfare in %s\n",
432 xyas(sp->sct_x, sp->sct_y, target));
433 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
434 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
439 take_casualties(struct sctstr *sp, int mc)
443 int nunits = 0, each, deq;
447 /* casualties come out of mil first */
448 orig_mil = sp->sct_item[I_MILIT];
450 if (mc <= orig_mil) {
451 sp->sct_item[I_MILIT] = orig_mil - mc;
454 sp->sct_item[I_MILIT] = 0;
456 /* remaining casualites */
460 * Need to take total_casualties and divide
461 * them amongst the land units in the sector
462 * Do security troops first, then others.
463 * Try not to kill any unit.
465 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
466 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni))) {
468 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
475 each = (mc / nunits) + 2;
477 /* kill some security troops */
478 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
479 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni))) {
480 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
483 cantake = (((float)(lp->lnd_effic - 40) / 100.0) *
484 (float)lnd_getmil(lp)) * 2;
485 /* (float)lchr[lp->lnd_type].l_mil)*2; */
487 if (cantake >= each) {
488 /* deq = (((float)each/(float)(lchr[lp->lnd_type].l_mil*2)) */
489 deq = (((float)each / (float)(lnd_getmil(lp) * 2))
492 } else if (cantake > 0) {
493 deq = (((float)cantake / (float)(lnd_getmil(lp) * 2)) * 100.0);
494 /* (float)(lchr[lp->lnd_type].l_mil*2)) * 100.0); */
495 mc -= (((float)deq / 100.0) * (float)lnd_getmil(lp)) * 2;
496 /* (float)lchr[lp->lnd_type].l_mil)*2; */
500 lp->lnd_effic -= deq;
501 lp->lnd_mobil -= deq / 2;
502 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
508 /* kill some normal troops */
509 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
510 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni))) {
511 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
514 cantake = (((float)(lp->lnd_effic - 40) / 100.0) *
515 (float)lnd_getmil(lp));
517 if (cantake >= each) {
518 deq = (((float)each / (float)(lnd_getmil(lp) * 2))
521 } else if (cantake > 0) {
522 deq = (((float)cantake / (float)lnd_getmil(lp))
524 mc -= (((float)deq / 100.0) * (float)lnd_getmil(lp));
527 lp->lnd_effic -= deq;
528 lp->lnd_mobil -= deq / 2;
529 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
535 /* Hmm.. still some left.. kill off units now */
536 /* kill some normal troops */
537 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
538 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni))) {
539 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
542 mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp));
544 lnd_submil(lp, 1000); /* Remove 'em all */
545 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
546 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
547 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
553 /* Hmm.. still some left.. kill off units now */
554 /* kill some security troops */
555 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
556 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni))) {
557 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
560 mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp)) * 2;
562 lnd_submil(lp, 1000); /* Kill 'em all */
563 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
564 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
565 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
571 /* Hmm.. everyone dead.. too bad */