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
45 static void take_casualties(struct sctstr *, int);
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)
66 /* che due to civilian unrest */
67 n = 10 - (random() % 20);
68 che_civ = 3 + (civ * n / 500);
71 else if (che_civ * 3 > civ)
73 if (che + che_civ > CHE_MAX)
74 che_civ = CHE_MAX - che;
77 /* che due to uw unrest */
78 n = 10 + (random() % 30);
79 che_uw = 5 + (uw * n / 500);
82 if (che + che_uw > CHE_MAX)
83 che_uw = CHE_MAX - che_uw;
86 if (che_civ + che_uw > 0) {
89 sp->sct_che_target = sp->sct_own;
92 sp->sct_item[I_CIVIL] = civ;
94 sp->sct_item[I_UW] = uw;
96 logerror("(#%d) %d che fired up in %s",
97 sp->sct_own, che, ownxy(sp));
103 * summary of effects.
104 * if there are no military in the sector, che recruit from
105 * populace if pop loyalty is > 10. They spread subversion otherwise,
106 * trying to lower pop loyalty.
107 * if che outnumber military, they stay and shoot it out, kill the
109 * if che are outnumbered by less than 5 to 1, they blow up stuff,
110 * killing innocent civilians (never uw's) and damaging commodities.
111 * if che are outnumbered by more than 5 to 1, they try to leave the
112 * sector for a nearby sector with fewer military.
114 * if the military lose any attacks, the pop loyalty in the sector
115 * gets worse, representing military defeat.
116 * military can "catch" che's after bombing attacks, or after they move.
117 * If military catch them, then they get to shoot it out with a portion
118 * of the che's depending on the # of mil in the sector. Chance to contact
119 * is around 10% per every equal number of mil:che ratio in the sector.
120 * "contact" is by 20% of the military in the sector, and odds are equal.
122 * Without a doubt this routine should be broken up, if only for readabilty.
125 guerrilla(struct sctstr *sp)
156 civ = sp->sct_item[I_CIVIL];
157 uw = sp->sct_item[I_UW];
158 victim = sp->sct_own;
159 actor = sp->sct_oldown;
161 mil = sp->sct_item[I_MILIT];
163 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
165 while (NULL != (lp = nxtitemp(&ni))) {
166 if (lp->lnd_own != sp->sct_own)
169 mil += lp->lnd_item[I_MILIT];
171 /* Security troops can now kill up to 1/2 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) {
211 /*logerror("own %d != target %d", sp->sct_own, target); */
217 odds = (double)che / (mil + che);
218 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
220 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
224 } else if (che > mil && mil > 0) {
225 /*logerror("guerrilla shootout with military"); */
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);
246 /*logerror("(#%d) mil beat che in %s", sp->sct_own, */
251 /*logerror("(#%d) che beat mil in %s", sp->sct_own, */
254 take_casualties(sp, mc);
255 } else if (ratio < 5) {
257 * guerrillas have to resort to blowing things up.
258 * Note this disrupts work in the sector.
261 n = (random() % 10) + (random() % che);
264 tmp = sp->sct_work - n;
269 "Production %s disrupted by terrorists in %s\n",
270 effadv(n), ownxy(sp));
271 sect_damage(sp, n / 10, 0);
272 /*logerror("(#%d) che blew up %s for %d", sp->sct_own, */
277 /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own, */
278 /*che, mil, ownxy(sp)); */
281 if (mil > 0 && che > 0) {
283 * we only get here if we haven't had combat previously.
284 * Chance to catch them.
285 * 20% of mil involved in attacking the che's.
287 if (chance(ratio * 0.10)) {
289 odds = (double)che / (n + che);
290 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
291 while (che > 0 && n > 0) {
300 take_casualties(sp, mc);
302 /*logerror("Caught che; mc: %d, cc: %d", cc, mc); */
305 if (convert && sp->sct_loyal >= 50) {
307 /* new owner gets to keep the mobility there */
308 oldmob = sp->sct_mobil;
309 /* che won, and sector converts. */
310 if (sp->sct_own == sp->sct_oldown)
313 takeover(sp, sp->sct_oldown);
314 sp->sct_mobil = oldmob;
319 if (civ > ITEM_MAX) {
323 sp->sct_item[I_CIVIL] = civ;
324 sp->sct_item[I_UW] = uw;
325 sp->sct_item[I_MILIT] = n;
329 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
330 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
332 if (recruit && che > 0) {
333 /* loyalty drops during recruitment efforts */
336 n += (random() % 5) + 1;
338 n += (random() % 10) + 4;
342 if (sp->sct_oldown != sp->sct_own || n > 100) {
343 n = civ * (random() % 3) / 200;
344 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
345 if (n + che > CHE_MAX)
349 sp->sct_item[I_CIVIL] = civ;
351 n = uw * (random() % 3) / 200;
352 if (n + che > CHE_MAX)
356 sp->sct_item[I_UW] = uw;
359 if (move && che > 0) {
360 struct sctstr *nicest_sp = 0;
365 /* search adjacent sectors for a nice one */
366 for (n = 1; n <= 6; n++) {
367 nsp = getsectp(sp->sct_x + diroff[n][0],
368 sp->sct_y + diroff[n][1]);
369 if (dchr[nsp->sct_type].d_mob0 < 0)
371 if (nsp->sct_own != target)
373 if (nsp->sct_che > 0) {
374 if (nsp->sct_che_target != target)
376 if (nsp->sct_che + che > CHE_MAX)
379 val = nsp->sct_item[I_MILIT];
380 /* don't give che more precise info than spy */
381 val = roundintby(val, 10);
382 /* inject a modicum of indeterminism; also
383 * avoids che preferring certain directions */
384 val += random() % 10 - 5;
390 /* if we found a nice sector, go there */
391 if (nicest_sp != 0) {
392 nicest_sp->sct_che += che;
393 nicest_sp->sct_che_target = target;
399 sp->sct_che_target = target;
402 sp->sct_che_target = 0;
404 if (mc > 0 || cc > 0) {
406 "Guerrilla warfare in %s\n",
407 xyas(sp->sct_x, sp->sct_y, target));
408 if (sp->sct_own == target)
409 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
412 " rebels murder %d military\n", mc);
413 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
415 if (sp->sct_own != victim)
416 wu(0, victim, "Partisans take over %s!\n",
417 xyas(sp->sct_x, sp->sct_y, victim));
421 take_casualties(struct sctstr *sp, int mc)
425 int nunits = 0, each, deq;
429 /* casualties come out of mil first */
430 orig_mil = sp->sct_item[I_MILIT];
432 if (mc <= orig_mil) {
433 sp->sct_item[I_MILIT] = orig_mil - mc;
436 sp->sct_item[I_MILIT] = 0;
438 /* remaining casualites */
442 * Need to take total_casualties and divide
443 * them amongst the land units in the sector
444 * Do security troops first, then others.
445 * Try not to kill any unit.
447 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
448 while (NULL != (lp = nxtitemp(&ni))) {
450 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
457 each = (mc / nunits) + 2;
459 /* kill some security troops */
460 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
461 while (NULL != (lp = nxtitemp(&ni))) {
462 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
465 cantake = ((lp->lnd_effic - 40) / 100.0)
466 * lp->lnd_item[I_MILIT] * 2.0;
468 if (cantake >= each) {
469 deq = (each / (lp->lnd_item[I_MILIT] * 2.0)) * 100.0;
471 } else if (cantake > 0) {
472 deq = (cantake / (lp->lnd_item[I_MILIT] * 2.0)) * 100.0;
473 mc -= (deq / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
477 lp->lnd_effic -= deq;
478 lp->lnd_mobil -= deq / 2;
479 deq = lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
485 /* kill some normal troops */
486 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
487 while (NULL != (lp = nxtitemp(&ni))) {
488 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
491 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
493 if (cantake >= each) {
494 deq = ((double)each / (lp->lnd_item[I_MILIT] * 2.0)) * 100.0;
496 } else if (cantake > 0) {
497 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
498 mc -= (deq / 100.0) * lp->lnd_item[I_MILIT];
502 lp->lnd_effic -= deq;
503 lp->lnd_mobil -= deq / 2;
504 deq = lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
510 /* Hmm.. still some left.. kill off units now */
511 /* kill some normal troops */
512 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
513 while (NULL != (lp = nxtitemp(&ni))) {
514 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
517 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
519 lnd_submil(lp, 1000); /* Remove 'em all */
520 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
521 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
522 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
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 (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
535 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
537 lnd_submil(lp, 1000); /* Kill 'em all */
538 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
539 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
540 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
546 /* Hmm.. everyone dead.. too bad */