2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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)
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;
95 logerror("(#%d) %d che fired up in %s",
96 sp->sct_own, che, ownxy(sp));
102 * summary of effects.
103 * if there are no military in the sector, che recruit from
104 * populace if pop loyalty is > 10. They spread subversion otherwise,
105 * trying to lower pop loyalty.
106 * if che outnumber military, they stay and shoot it out, kill the
108 * if che are outnumbered by less than 5 to 1, they blow up stuff,
109 * killing innocent civilians (never uw's) and damaging commodities.
110 * if che are outnumbered by more than 5 to 1, they try to leave the
111 * sector for a nearby sector with fewer military.
113 * if the military lose any attacks, the pop loyalty in the sector
114 * gets worse, representing military defeat.
115 * military can "catch" che's after bombing attacks, or after they move.
116 * If military catch them, then they get to shoot it out with a portion
117 * of the che's depending on the # of mil in the sector. Chance to contact
118 * is around 10% per every equal number of mil:che ratio in the sector.
119 * "contact" is by 20% of the military in the sector, and odds are equal.
121 * Without a doubt this routine should be broken up, if only for readabilty.
124 guerrilla(struct sctstr *sp)
155 civ = sp->sct_item[I_CIVIL];
156 uw = sp->sct_item[I_UW];
157 victim = sp->sct_own;
158 actor = sp->sct_oldown;
160 mil = sp->sct_item[I_MILIT];
162 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
164 while (NULL != (lp = nxtitemp(&ni))) {
165 if (lp->lnd_own != sp->sct_own)
168 mil += lp->lnd_item[I_MILIT];
170 if (sp->sct_che_target != sp->sct_own)
173 /* Security troops can now kill up to 1/5 their complement each
174 update, before doing anything else. */
175 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
178 mil += lp->lnd_item[I_MILIT];
179 r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
180 che_kill = r < 1 ? 0 : roll(r);
185 "%s kills %d guerrilla%s in raid at %s!\n",
186 prland(lp), che_kill, splur(che_kill), ownxy(sp));
192 /* Security forces killed all the che */
195 sp->sct_che_target = 0;
199 target = sp->sct_che_target;
200 if (CANT_HAPPEN(target == 0))
202 tnat = getnatp(target);
203 if (tnat->nat_stat == STAT_UNUSED) {
204 /* target nation has dissolved: che's retire. */
205 logerror("%d Che targeted at country %d retiring", che, target);
207 sp->sct_che_target = 0;
208 sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
212 if (sp->sct_own != target) {
218 odds = (double)che / (mil + che);
219 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
221 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
225 } else if (che > mil && mil > 0) {
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);
250 take_casualties(sp, mc);
251 } else if (ratio < 5) {
253 * guerrillas have to resort to blowing things up.
254 * Note this disrupts work in the sector.
256 n = (random() % 10) + (random() % che);
259 tmp = sp->sct_work - n;
264 "Production %s disrupted by terrorists in %s\n",
265 effadv(n), ownxy(sp));
266 sect_damage(sp, n / 10);
272 if (mil > 0 && che > 0) {
274 * we only get here if we haven't had combat previously.
275 * Chance to catch them.
276 * 20% of mil involved in attacking the che's.
278 if (chance(ratio * 0.10)) {
280 odds = (double)che / (n + che);
281 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
282 while (che > 0 && n > 0) {
291 take_casualties(sp, mc);
295 if (convert && sp->sct_loyal >= 50) {
297 /* new owner gets to keep the mobility there */
298 oldmob = sp->sct_mobil;
299 /* che won, and sector converts. */
300 if (sp->sct_own == sp->sct_oldown)
303 lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
304 0, sp->sct_x, sp->sct_y);
305 takeover(sp, sp->sct_oldown);
307 sp->sct_mobil = oldmob;
312 if (civ > ITEM_MAX) {
316 sp->sct_item[I_CIVIL] = civ;
317 sp->sct_item[I_UW] = uw;
318 sp->sct_item[I_MILIT] = n;
322 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
323 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
325 if (recruit && che > 0) {
326 /* loyalty drops during recruitment efforts */
329 n += (random() % 5) + 1;
331 n += (random() % 10) + 4;
335 if (sp->sct_oldown != sp->sct_own || n > 100) {
336 n = civ * (random() % 3) / 200;
337 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
338 if (n + che > CHE_MAX)
342 sp->sct_item[I_CIVIL] = civ;
344 n = uw * (random() % 3) / 200;
345 if (n + che > CHE_MAX)
349 sp->sct_item[I_UW] = uw;
352 if (move && che > 0) {
353 struct sctstr *nicest_sp = NULL;
358 /* search adjacent sectors for a nice one */
359 for (n = 1; n <= 6; n++) {
360 nsp = getsectp(sp->sct_x + diroff[n][0],
361 sp->sct_y + diroff[n][1]);
362 if (dchr[nsp->sct_type].d_mob0 < 0)
364 if (nsp->sct_own != target)
366 if (nsp->sct_che > 0) {
367 if (nsp->sct_che_target != target)
369 if (nsp->sct_che + che > CHE_MAX)
372 val = nsp->sct_item[I_MILIT];
373 /* don't give che more precise info than spy */
374 val = roundintby(val, 10);
375 /* inject a modicum of indeterminism; also
376 * avoids che preferring certain directions */
377 val += random() % 10 - 5;
383 /* if we found a nice sector, go there */
385 nicest_sp->sct_che += che;
386 nicest_sp->sct_che_target = target;
392 sp->sct_che_target = target;
395 sp->sct_che_target = 0;
397 if (mc > 0 || cc > 0) {
399 "Guerrilla warfare in %s\n",
400 xyas(sp->sct_x, sp->sct_y, target));
401 if (sp->sct_own == target)
402 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
405 " rebels murder %d military\n", mc);
406 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
408 if (sp->sct_own != victim)
409 wu(0, victim, "Partisans take over %s!\n",
410 xyas(sp->sct_x, sp->sct_y, victim));
414 take_casualties(struct sctstr *sp, int mc)
418 int nunits = 0, each, deq;
422 /* casualties come out of mil first */
423 orig_mil = sp->sct_item[I_MILIT];
425 if (mc <= orig_mil) {
426 sp->sct_item[I_MILIT] = orig_mil - mc;
429 sp->sct_item[I_MILIT] = 0;
431 /* remaining casualites */
435 * Need to take total_casualties and divide
436 * them amongst the land units in the sector
437 * Do security troops first, then others.
438 * Try not to kill any unit.
440 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
441 while (NULL != (lp = nxtitemp(&ni))) {
443 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
450 each = (mc / nunits) + 2;
452 /* kill some security troops */
453 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
454 while (NULL != (lp = nxtitemp(&ni))) {
455 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
458 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
460 if (cantake >= each) {
461 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
463 } else if (cantake > 0) {
464 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
469 lp->lnd_effic -= deq;
470 lp->lnd_mobil -= deq / 2;
471 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
477 /* kill some normal troops */
478 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
479 while (NULL != (lp = nxtitemp(&ni))) {
480 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
483 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
485 if (cantake >= each) {
486 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
488 } else if (cantake > 0) {
489 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
494 lp->lnd_effic -= deq;
495 lp->lnd_mobil -= deq / 2;
496 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
502 /* Hmm.. still some left.. kill off units now */
503 /* kill some normal troops */
504 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
505 while (NULL != (lp = nxtitemp(&ni))) {
506 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
509 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
511 lnd_submil(lp, 1000); /* Remove 'em all */
512 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
513 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
514 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
520 /* Hmm.. still some left.. kill off units now */
521 /* kill some security troops */
522 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
523 while (NULL != (lp = nxtitemp(&ni))) {
524 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
527 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
529 lnd_submil(lp, 1000); /* Kill 'em all */
530 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
531 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
532 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
538 /* Hmm.. everyone dead.. too bad */