2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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
53 static void take_casualties(struct sctstr *, int);
56 revolt(struct sctstr *sp)
67 che_combo = getvar(V_CHE, (s_char *)sp, EF_SECTOR);
68 che = get_che_value(che_combo);
69 target = get_che_cnum(che_combo);
70 if (che_combo != 0 && (target != sp->sct_own || che >= CHE_MAX))
72 civ = getvar(V_CIVIL, (s_char *)sp, EF_SECTOR);
73 uw = getvar(V_UW, (s_char *)sp, EF_SECTOR);
74 if (che > (civ + uw) * 3)
78 /* che due to civilian unrest */
79 n = 10 - (random() % 20);
80 che_civ = 3 + (civ * n / 500);
83 else if (che_civ * 3 > civ)
85 if (che + che_civ > CHE_MAX)
86 che_civ = CHE_MAX - che;
89 /* che due to uw unrest */
90 n = 10 + (random() % 30);
91 che_uw = 5 + (uw * n / 500);
94 if (che + che_uw > CHE_MAX)
95 che_uw = CHE_MAX - che_uw;
98 if (che_civ + che_uw > 0) {
101 set_che_cnum(che_combo, sp->sct_own);
102 set_che_value(che_combo, che);
103 putvar(V_CHE, (int)che_combo, (s_char *)sp, EF_SECTOR);
105 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
107 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
109 logerror("(#%d) %d che fired up in %s",
110 sp->sct_own, che, ownxy(sp));
116 * summary of effects.
117 * if there are no military in the sector, che recruit from
118 * populace if pop loyalty is > 10. They spread subversion otherwise,
119 * trying to lower pop loyalty.
120 * if che outnumber military, they stay and shoot it out, kill the
122 * if che are outnumbered by less than 5 to 1, they blow up stuff,
123 * killing innocent civilians (never uw's) and damaging commodities.
124 * if che are outnumbered by more than 5 to 1, they try to leave the
125 * sector for a nearby sector with fewer military.
127 * if the military lose any attacks, the pop loyalty in the sector
128 * gets worse, representing military defeat.
129 * military can "catch" che's after bombing attacks, or after they move.
130 * If military catch them, then they get to shoot it out with a portion
131 * of the che's depending on the # of mil in the sector. Chance to contact
132 * is around 10% per every equal number of mil:che ratio in the sector.
133 * "contact" is by 20% of the military in the sector, and odds are equal.
135 * Without a doubt this routine should be broken up, if only for readabilty.
138 guerrilla(struct sctstr *sp)
169 if ((n = getvar(V_CHE, (s_char *)sp, EF_SECTOR)) <= 0)
172 if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
177 victim = sp->sct_own;
178 actor = sp->sct_oldown;
179 che = get_che_value(che_combo);
182 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
184 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
185 if (lp->lnd_own != sp->sct_own)
188 mil += lnd_getmil(lp);
190 /* Security troops can now kill up to 1/2 their complement each
191 update, before doing anything else. */
192 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
196 lcp = &lchr[(int)lp->lnd_type];
197 mil += lnd_getmil(lp);
198 r = (((float)(lp->lnd_effic / 100) * (float)(lnd_getmil(lp))) /
202 che_kill = (roll(r) - 1);
207 "%s kills %d guerrilla%s in raid at %s!\n",
208 prland(lp), che_kill, splur(che_kill), ownxy(sp));
214 /* Security forces killed all the che */
216 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
220 target = get_che_cnum(che_combo);
222 /* the deity can't be a target! */
225 tnat = getnatp(target);
226 if ((tnat->nat_stat & STAT_INUSE) == 0) {
227 /* target nation has dissolved: che's retire. */
228 logerror("%d Che targeted at country %d retiring", che, target);
230 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
231 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
235 if (sp->sct_own != target) {
236 /*logerror("own %d != target %d", sp->sct_own, target); */
242 odds = (double)che / (mil + che);
243 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
245 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
249 } else if (che > mil && mil > 0) {
250 /*logerror("guerrilla shootout with military"); */
252 * shoot it out with the military, and kill them off.
253 * If loyalty bad enough, then take the sector over,
254 * and enlist 5% of civ as military force.
256 while (che > 0 && mil > 0) {
267 n = sp->sct_loyal - (random() % 15);
271 /*logerror("(#%d) mil beat che in %s", sp->sct_own, */
276 /*logerror("(#%d) che beat mil in %s", sp->sct_own, */
279 take_casualties(sp, mc);
280 } else if (ratio < 5) {
282 * guerrillas have to resort to blowing things up.
283 * Note this disrupts work in the sector.
286 n = (random() % 10) + (random() % che);
289 tmp = sp->sct_work - n;
294 "Production %s disrupted by terrorists in %s\n",
295 effadv(n), ownxy(sp));
296 sect_damage(sp, n / 10, 0);
297 /*logerror("(#%d) che blew up %s for %d", sp->sct_own, */
302 /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own, */
303 /*che, mil, ownxy(sp)); */
306 if (mil > 0 && che > 0) {
308 * we only get here if we haven't had combat previously.
309 * Chance to catch them.
310 * 20% of mil involved in attacking the che's.
312 if (chance(ratio * 0.10)) {
314 if ((n + che) == 0) {
315 logerror("n=%d che=%d\n", n, che);
319 odds = (double)che / (n + che);
320 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
321 while (che > 0 && n > 0) {
330 take_casualties(sp, mc);
332 /*logerror("Caught che; mc: %d, cc: %d", cc, mc); */
335 if (convert && sp->sct_loyal >= 50) {
337 /* new owner gets to keep the mobility there */
338 oldmob = sp->sct_mobil;
339 /* che won, and sector converts. */
340 if (sp->sct_own == sp->sct_oldown)
343 takeover(sp, sp->sct_oldown);
344 sp->sct_mobil = oldmob;
348 * so we can't keep losing money by having
351 if (sp->sct_type == SCT_CAPIT && sp->sct_newtype == SCT_CAPIT)
352 sp->sct_newtype = SCT_AGRI;
355 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
356 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
357 putvar(V_MILIT, n, (s_char *)sp, EF_SECTOR);
361 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
362 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
364 if (recruit && che > 0) {
365 /* loyalty drops during recruitment efforts */
368 n += (random() % 5) + 1;
370 n += (random() % 10) + 4;
374 if (sp->sct_oldown != sp->sct_own || n > 100) {
375 n = civ * (random() % 3) / 200;
376 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
377 if (n + che > CHE_MAX)
381 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
383 n = uw * (random() % 3) / 200;
384 if (n + che > CHE_MAX)
388 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
391 if (move && che > 0) {
392 struct sctstr *nicest_sp = 0;
397 /* search adjacent sectors for a nice one */
398 for (n = 1; n <= 6; n++) {
399 nsp = getsectp(sp->sct_x + diroff[n][0],
400 sp->sct_y + diroff[n][1]);
401 if (dchr[nsp->sct_type].d_mcst == 0)
403 if (nsp->sct_own != target)
405 if ((val = getvar(V_CHE, (s_char *)nsp, EF_SECTOR)) > 0) {
407 if (get_che_cnum(che_combo) != target)
409 if (get_che_value(che_combo) + che > CHE_MAX)
412 val = getvar(V_MILIT, (s_char *)nsp, EF_SECTOR);
413 /* don't give che more precise info than spy */
414 val = roundintby(val, 10);
415 /* inject a modicum of indeterminism; also
416 * avoids che preferring certain directions */
417 val += random() % 10 - 5;
423 /* if we found a nice sector, go there */
424 if (nicest_sp != 0) {
425 che_combo = getvar(V_CHE, (s_char *)nicest_sp, EF_SECTOR);
426 che += get_che_value(che_combo);
427 set_che_value(che_combo, che);
428 set_che_cnum(che_combo, target);
429 putvar(V_CHE, (int)che_combo, (s_char *)nicest_sp, EF_SECTOR);
434 set_che_value(che_combo, che);
435 set_che_cnum(che_combo, target);
436 putvar(V_CHE, (int)che_combo, (s_char *)sp, EF_SECTOR);
438 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
439 if (mc > 0 || cc > 0) {
440 /* don't tell who won just to be mean */
442 "Guerrilla warfare in %s\n",
443 xyas(sp->sct_x, sp->sct_y, target));
444 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
445 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
450 take_casualties(struct sctstr *sp, int mc)
454 int nunits = 0, each, deq;
458 /* casualties come out of mil first */
459 orig_mil = getvar(V_MILIT, (s_char *)sp, EF_SECTOR);
461 if (mc <= orig_mil) {
462 putvar(V_MILIT, (orig_mil - mc), (s_char *)sp, EF_SECTOR);
465 putvar(V_MILIT, 0, (s_char *)sp, EF_SECTOR);
467 /* remaining casualites */
471 * Need to take total_casualties and divide
472 * them amongst the land units in the sector
473 * Do security troops first, then others.
474 * Try not to kill any unit.
476 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
477 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
479 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
486 each = (mc / nunits) + 2;
488 /* kill some security troops */
489 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
490 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
491 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
494 cantake = (((float)(lp->lnd_effic - 40) / 100.0) *
495 (float)lnd_getmil(lp)) * 2;
496 /* (float)lchr[lp->lnd_type].l_mil)*2; */
498 if (cantake >= each) {
499 /* deq = (((float)each/(float)(lchr[lp->lnd_type].l_mil*2)) */
500 deq = (((float)each / (float)(lnd_getmil(lp) * 2))
503 } else if (cantake > 0) {
504 deq = (((float)cantake / (float)(lnd_getmil(lp) * 2)) * 100.0);
505 /* (float)(lchr[lp->lnd_type].l_mil*2)) * 100.0); */
506 mc -= (((float)deq / 100.0) * (float)lnd_getmil(lp)) * 2;
507 /* (float)lchr[lp->lnd_type].l_mil)*2; */
511 lp->lnd_effic -= deq;
512 lp->lnd_mobil -= deq / 2;
513 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
519 /* kill some normal troops */
520 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
521 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
522 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
525 cantake = (((float)(lp->lnd_effic - 40) / 100.0) *
526 (float)lnd_getmil(lp));
528 if (cantake >= each) {
529 deq = (((float)each / (float)(lnd_getmil(lp) * 2))
532 } else if (cantake > 0) {
533 deq = (((float)cantake / (float)lnd_getmil(lp))
535 mc -= (((float)deq / 100.0) * (float)lnd_getmil(lp));
538 lp->lnd_effic -= deq;
539 lp->lnd_mobil -= deq / 2;
540 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
546 /* Hmm.. still some left.. kill off units now */
547 /* kill some normal troops */
548 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
549 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
550 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
553 mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp));
555 lnd_submil(lp, 1000); /* Remove 'em all */
556 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
557 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
558 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
564 /* Hmm.. still some left.. kill off units now */
565 /* kill some security troops */
566 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
567 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
568 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
571 mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp)) * 2;
573 lnd_submil(lp, 1000); /* Kill 'em all */
574 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
575 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
576 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
582 /* Hmm.. everyone dead.. too bad */