2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * revolt.c: Have disloyal populace revolt!
29 * Known contributors to this file:
31 * Steve McClure, 1997-2000
44 static void take_casualties(struct sctstr *, int);
47 revolt(struct sctstr *sp)
57 if (che != 0 && (sp->sct_che_target != sp->sct_own || che >= CHE_MAX))
59 civ = sp->sct_item[I_CIVIL];
60 uw = sp->sct_item[I_UW];
61 if (che > (civ + uw) * 3)
64 /* che due to civilian unrest */
65 n = 10 - (random() % 20);
66 che_civ = 3 + (civ * n / 500);
69 else if (che_civ * 3 > civ)
71 if (che + che_civ > CHE_MAX)
72 che_civ = CHE_MAX - che;
75 /* che due to uw unrest */
76 n = 10 + (random() % 30);
77 che_uw = 5 + (uw * n / 500);
80 if (che + che_uw > CHE_MAX)
81 che_uw = CHE_MAX - che_uw;
84 if (che_civ + che_uw > 0) {
87 sp->sct_che_target = sp->sct_own;
90 sp->sct_item[I_CIVIL] = civ;
92 sp->sct_item[I_UW] = uw;
98 * if there are no military in the sector, che recruit from
99 * populace if pop loyalty is > 10. They spread subversion otherwise,
100 * trying to lower pop loyalty.
101 * if che outnumber military, they stay and shoot it out, kill the
103 * if che are outnumbered by less than 5 to 1, they blow up stuff,
104 * killing innocent civilians (never uw's) and damaging commodities.
105 * if che are outnumbered by more than 5 to 1, they try to leave the
106 * sector for a nearby sector with fewer military.
108 * if the military lose any attacks, the pop loyalty in the sector
109 * gets worse, representing military defeat.
110 * military can "catch" che's after bombing attacks, or after they move.
111 * If military catch them, then they get to shoot it out with a portion
112 * of the che's depending on the # of mil in the sector. Chance to contact
113 * is around 10% per every equal number of mil:che ratio in the sector.
114 * "contact" is by 20% of the military in the sector, and odds are equal.
116 * Without a doubt this routine should be broken up, if only for readabilty.
119 guerrilla(struct sctstr *sp)
150 civ = sp->sct_item[I_CIVIL];
151 uw = sp->sct_item[I_UW];
152 victim = sp->sct_own;
153 actor = sp->sct_oldown;
155 mil = sp->sct_item[I_MILIT];
157 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
159 while (NULL != (lp = nxtitemp(&ni))) {
160 if (lp->lnd_own != sp->sct_own)
162 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
165 mil += lp->lnd_item[I_MILIT];
167 if (sp->sct_che_target != sp->sct_own)
170 /* Security troops can now kill up to 1/5 their complement each
171 update, before doing anything else. */
172 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
175 mil += lp->lnd_item[I_MILIT];
176 r = (lp->lnd_item[I_MILIT] * lp->lnd_effic) / 500;
177 che_kill = r < 1 ? 0 : roll(r);
182 "%s kills %d guerrilla%s in raid at %s!\n",
183 prland(lp), che_kill, splur(che_kill), ownxy(sp));
189 /* Security forces killed all the che */
192 sp->sct_che_target = 0;
196 target = sp->sct_che_target;
197 if (CANT_HAPPEN(target == 0))
199 tnat = getnatp(target);
200 if (tnat->nat_stat == STAT_UNUSED) {
201 /* target nation has dissolved: che's retire. */
202 logerror("%d Che targeted at country %d retiring", che, target);
204 sp->sct_che_target = 0;
205 sp->sct_item[I_CIVIL] = MIN(civ + che, ITEM_MAX);
209 if (sp->sct_own != target) {
215 odds = (double)che / (mil + che);
216 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
218 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
222 } else if (che > mil && mil > 0) {
224 * shoot it out with the military, and kill them off.
225 * If loyalty bad enough, then take the sector over,
226 * and enlist 5% of civ as military force.
228 while (che > 0 && mil > 0) {
239 n = sp->sct_loyal - (random() % 15);
247 take_casualties(sp, mc);
248 } else if (ratio < 5) {
250 * guerrillas have to resort to blowing things up.
251 * Note this disrupts work in the sector.
253 n = (random() % 10) + (random() % che);
256 tmp = sp->sct_work - n;
261 "Production %s disrupted by terrorists in %s\n",
262 effadv(n), ownxy(sp));
263 sect_damage(sp, n / 10);
269 if (mil > 0 && che > 0) {
271 * we only get here if we haven't had combat previously.
272 * Chance to catch them.
273 * 20% of mil involved in attacking the che's.
275 if (chance(ratio * 0.10)) {
277 odds = (double)che / (n + che);
278 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
279 while (che > 0 && n > 0) {
288 take_casualties(sp, mc);
292 if (convert && sp->sct_loyal >= 50) {
294 /* new owner gets to keep the mobility there */
295 oldmob = sp->sct_mobil;
296 /* che won, and sector converts. */
297 if (sp->sct_own == sp->sct_oldown)
300 lost_and_found(EF_SECTOR, sp->sct_own, sp->sct_oldown,
301 0, sp->sct_x, sp->sct_y);
302 takeover(sp, sp->sct_oldown);
304 sp->sct_mobil = oldmob;
309 if (civ > ITEM_MAX) {
313 sp->sct_item[I_CIVIL] = civ;
314 sp->sct_item[I_UW] = uw;
315 sp->sct_item[I_MILIT] = n;
319 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
320 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
322 if (recruit && che > 0) {
323 /* loyalty drops during recruitment efforts */
326 n += (random() % 5) + 1;
328 n += (random() % 10) + 4;
332 if (sp->sct_oldown != sp->sct_own || n > 100) {
333 n = civ * (random() % 3) / 200;
334 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
335 if (n + che > CHE_MAX)
339 sp->sct_item[I_CIVIL] = civ;
341 n = uw * (random() % 3) / 200;
342 if (n + che > CHE_MAX)
346 sp->sct_item[I_UW] = uw;
349 if (move && che > 0) {
350 struct sctstr *nicest_sp = NULL;
355 /* search adjacent sectors for a nice one */
356 for (n = 1; n <= 6; n++) {
357 nsp = getsectp(sp->sct_x + diroff[n][0],
358 sp->sct_y + diroff[n][1]);
359 if (dchr[nsp->sct_type].d_mob0 < 0)
361 if (nsp->sct_own != target)
363 if (nsp->sct_che > 0) {
364 if (nsp->sct_che_target != target)
366 if (nsp->sct_che + che > CHE_MAX)
369 val = nsp->sct_item[I_MILIT];
370 /* don't give che more precise info than spy */
371 val = roundintby(val, 10);
372 /* inject a modicum of indeterminism; also
373 * avoids che preferring certain directions */
374 val += random() % 10 - 5;
380 /* if we found a nice sector, go there */
382 nicest_sp->sct_che += che;
383 nicest_sp->sct_che_target = target;
389 sp->sct_che_target = target;
392 sp->sct_che_target = 0;
394 if (mc > 0 || cc > 0) {
396 "Guerrilla warfare in %s\n",
397 xyas(sp->sct_x, sp->sct_y, target));
398 if (sp->sct_own == target)
399 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
401 wu(0, target, " rebels murder %d military\n", mc);
402 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
404 if (sp->sct_own != victim)
405 wu(0, victim, "Partisans take over %s!\n",
406 xyas(sp->sct_x, sp->sct_y, victim));
410 take_casualties(struct sctstr *sp, int mc)
414 int nunits = 0, each, deq;
418 /* casualties come out of mil first */
419 orig_mil = sp->sct_item[I_MILIT];
421 if (mc <= orig_mil) {
422 sp->sct_item[I_MILIT] = orig_mil - mc;
425 sp->sct_item[I_MILIT] = 0;
427 /* remaining casualites */
431 * Need to take total_casualties and divide
432 * them amongst the land units in the sector
433 * Do security troops first, then others.
434 * Try not to kill any unit.
436 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
437 while (NULL != (lp = nxtitemp(&ni))) {
438 if (lp->lnd_own != sp->sct_own)
440 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
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 (lp->lnd_own != sp->sct_own)
457 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
459 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
462 cantake = ((lp->lnd_effic - 40) / 100.0) * lp->lnd_item[I_MILIT];
464 if (cantake >= each) {
465 deq = ((double)each / lp->lnd_item[I_MILIT]) * 100.0;
467 } else if (cantake > 0) {
468 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
473 lp->lnd_effic -= deq;
474 lp->lnd_mobil -= deq / 2;
475 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (deq / 100.0);
481 /* kill some normal troops */
482 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
483 while (NULL != (lp = nxtitemp(&ni))) {
484 if (lp->lnd_own != sp->sct_own)
486 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
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]) * 100.0;
496 } else if (cantake > 0) {
497 deq = ((double)cantake / lp->lnd_item[I_MILIT]) * 100.0;
502 lp->lnd_effic -= deq;
503 lp->lnd_mobil -= deq / 2;
504 deq = lchr[(int)lp->lnd_type].l_item[I_MILIT] * (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 (lp->lnd_own != sp->sct_own)
516 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
518 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
521 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT];
523 lnd_submil(lp, 1000); /* Remove 'em all */
524 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
525 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
526 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
532 /* Hmm.. still some left.. kill off units now */
533 /* kill some security troops */
534 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
535 while (NULL != (lp = nxtitemp(&ni))) {
536 if (lp->lnd_own != sp->sct_own)
538 if (lp->lnd_ship >= 0 || lp->lnd_land >= 0)
540 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
543 mc -= (lp->lnd_effic / 100.0) * lp->lnd_item[I_MILIT] * 2.0;
545 lnd_submil(lp, 1000); /* Kill 'em all */
546 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
547 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
548 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
554 /* Hmm.. everyone dead.. too bad */