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)
167 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
170 mil += lp->lnd_item[I_MILIT];
172 if (sp->sct_che_target != sp->sct_own)
175 /* Security troops can now kill up to 1/5 their complement each
176 update, before doing anything else. */
177 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
180 mil += lp->lnd_item[I_MILIT];
181 r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
182 che_kill = r < 1 ? 0 : roll(r);
187 "%s kills %d guerrilla%s in raid at %s!\n",
188 prland(lp), che_kill, splur(che_kill), ownxy(sp));
194 /* Security forces killed all the che */
197 sp->sct_che_target = 0;
201 target = sp->sct_che_target;
202 if (CANT_HAPPEN(target == 0))
204 tnat = getnatp(target);
205 if (tnat->nat_stat == STAT_UNUSED) {
206 /* target nation has dissolved: che's retire. */
207 logerror("%d Che targeted at country %d retiring", che, target);
209 sp->sct_che_target = 0;
210 sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
214 if (sp->sct_own != target) {
220 odds = (double)che / (mil + che);
221 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
223 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
227 } else if (che > mil && mil > 0) {
229 * shoot it out with the military, and kill them off.
230 * If loyalty bad enough, then take the sector over,
231 * and enlist 5% of civ as military force.
233 while (che > 0 && mil > 0) {
244 n = sp->sct_loyal - (random() % 15);
252 take_casualties(sp, mc);
253 } else if (ratio < 5) {
255 * guerrillas have to resort to blowing things up.
256 * Note this disrupts work in the sector.
258 n = (random() % 10) + (random() % che);
261 tmp = sp->sct_work - n;
266 "Production %s disrupted by terrorists in %s\n",
267 effadv(n), ownxy(sp));
268 sect_damage(sp, n / 10);
274 if (mil > 0 && che > 0) {
276 * we only get here if we haven't had combat previously.
277 * Chance to catch them.
278 * 20% of mil involved in attacking the che's.
280 if (chance(ratio * 0.10)) {
282 odds = (double)che / (n + che);
283 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
284 while (che > 0 && n > 0) {
293 take_casualties(sp, mc);
297 if (convert && sp->sct_loyal >= 50) {
299 /* new owner gets to keep the mobility there */
300 oldmob = sp->sct_mobil;
301 /* che won, and sector converts. */
302 if (sp->sct_own == sp->sct_oldown)
305 lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
306 0, sp->sct_x, sp->sct_y);
307 takeover(sp, sp->sct_oldown);
309 sp->sct_mobil = oldmob;
314 if (civ > ITEM_MAX) {
318 sp->sct_item[I_CIVIL] = civ;
319 sp->sct_item[I_UW] = uw;
320 sp->sct_item[I_MILIT] = n;
324 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
325 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
327 if (recruit && che > 0) {
328 /* loyalty drops during recruitment efforts */
331 n += (random() % 5) + 1;
333 n += (random() % 10) + 4;
337 if (sp->sct_oldown != sp->sct_own || n > 100) {
338 n = civ * (random() % 3) / 200;
339 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
340 if (n + che > CHE_MAX)
344 sp->sct_item[I_CIVIL] = civ;
346 n = uw * (random() % 3) / 200;
347 if (n + che > CHE_MAX)
351 sp->sct_item[I_UW] = uw;
354 if (move && che > 0) {
355 struct sctstr *nicest_sp = NULL;
360 /* search adjacent sectors for a nice one */
361 for (n = 1; n <= 6; n++) {
362 nsp = getsectp(sp->sct_x + diroff[n][0],
363 sp->sct_y + diroff[n][1]);
364 if (dchr[nsp->sct_type].d_mob0 < 0)
366 if (nsp->sct_own != target)
368 if (nsp->sct_che > 0) {
369 if (nsp->sct_che_target != target)
371 if (nsp->sct_che + che > CHE_MAX)
374 val = nsp->sct_item[I_MILIT];
375 /* don't give che more precise info than spy */
376 val = roundintby(val, 10);
377 /* inject a modicum of indeterminism; also
378 * avoids che preferring certain directions */
379 val += random() % 10 - 5;
385 /* if we found a nice sector, go there */
387 nicest_sp->sct_che += che;
388 nicest_sp->sct_che_target = target;
394 sp->sct_che_target = target;
397 sp->sct_che_target = 0;
399 if (mc > 0 || cc > 0) {
401 "Guerrilla warfare in %s\n",
402 xyas(sp->sct_x, sp->sct_y, target));
403 if (sp->sct_own == target)
404 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
407 " rebels murder %d military\n", mc);
408 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
410 if (sp->sct_own != victim)
411 wu(0, victim, "Partisans take over %s!\n",
412 xyas(sp->sct_x, sp->sct_y, victim));
416 take_casualties(struct sctstr *sp, int mc)
420 int nunits = 0, each, deq;
424 /* casualties come out of mil first */
425 orig_mil = sp->sct_item[I_MILIT];
427 if (mc <= orig_mil) {
428 sp->sct_item[I_MILIT] = orig_mil - mc;
431 sp->sct_item[I_MILIT] = 0;
433 /* remaining casualites */
437 * Need to take total_casualties and divide
438 * them amongst the land units in the sector
439 * Do security troops first, then others.
440 * Try not to kill any unit.
442 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
443 while (NULL != (lp = nxtitemp(&ni))) {
444 if (lp->lnd_own != sp->sct_own)
446 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
449 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
456 each = (mc / nunits) + 2;
458 /* kill some security troops */
459 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
460 while (NULL != (lp = nxtitemp(&ni))) {
461 if (lp->lnd_own != sp->sct_own)
463 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
465 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
468 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
470 if (cantake >= each) {
471 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
473 } else if (cantake > 0) {
474 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
479 lp->lnd_effic -= deq;
480 lp->lnd_mobil -= deq / 2;
481 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
487 /* kill some normal troops */
488 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
489 while (NULL != (lp = nxtitemp(&ni))) {
490 if (lp->lnd_own != sp->sct_own)
492 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
494 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
497 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
499 if (cantake >= each) {
500 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
502 } else if (cantake > 0) {
503 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
508 lp->lnd_effic -= deq;
509 lp->lnd_mobil -= deq / 2;
510 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
516 /* Hmm.. still some left.. kill off units now */
517 /* kill some normal troops */
518 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
519 while (NULL != (lp = nxtitemp(&ni))) {
520 if (lp->lnd_own != sp->sct_own)
522 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
524 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
527 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
529 lnd_submil(lp, 1000); /* Remove '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.. still some left.. kill off units now */
539 /* kill some security troops */
540 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
541 while (NULL != (lp = nxtitemp(&ni))) {
542 if (lp->lnd_own != sp->sct_own)
544 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
546 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
549 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
551 lnd_submil(lp, 1000); /* Kill 'em all */
552 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
553 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
554 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
560 /* Hmm.. everyone dead.. too bad */