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);
55 #define get_che_cnum(x) ((x) >> 8)
56 #define set_che_cnum(x, cn) ((x) = ((x) & 0xff) | ((cn) << 8))
57 #define get_che_value(x) ((x) & 0xff)
58 #define set_che_value(x, n) ((x) = ((x) & 0xff00) | (n))
63 revolt(struct sctstr *sp)
74 che_combo = getvar(V_CHE, (s_char *)sp, EF_SECTOR);
75 che = get_che_value(che_combo);
76 target = get_che_cnum(che_combo);
77 if (che_combo != 0 && (target != sp->sct_own || che >= CHE_MAX))
79 civ = getvar(V_CIVIL, (s_char *)sp, EF_SECTOR);
80 uw = getvar(V_UW, (s_char *)sp, EF_SECTOR);
81 if (che > (civ + uw) * 3)
85 /* che due to civilian unrest */
86 n = 10 - (random() % 20);
87 che_civ = 3 + (civ * n / 500);
90 else if (che_civ * 3 > civ)
92 if (che + che_civ > CHE_MAX)
93 che_civ = CHE_MAX - che;
96 /* che due to uw unrest */
97 n = 10 + (random() % 30);
98 che_uw = 5 + (uw * n / 500);
101 if (che + che_uw > CHE_MAX)
102 che_uw = CHE_MAX - che_uw;
105 if (che_civ + che_uw > 0) {
108 set_che_cnum(che_combo, sp->sct_own);
109 set_che_value(che_combo, che);
110 putvar(V_CHE, (int)che_combo, (s_char *)sp, EF_SECTOR);
112 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
114 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
116 logerror("(#%d) %d che fired up in %s",
117 sp->sct_own, che, ownxy(sp));
123 * summary of effects.
124 * if there are no military in the sector, che recruit from
125 * populace if pop loyalty is > 10. They spread subversion otherwise,
126 * trying to lower pop loyalty.
127 * if che outnumber military, they stay and shoot it out, kill the
129 * if che are outnumbered by less than 5 to 1, they blow up stuff,
130 * killing innocent civilians (never uw's) and damaging commodities.
131 * if che are outnumbered by more than 5 to 1, they try to leave the
132 * sector for a nearby sector with fewer military.
134 * if the military lose any attacks, the pop loyalty in the sector
135 * gets worse, representing military defeat.
136 * military can "catch" che's after bombing attacks, or after they move.
137 * If military catch them, then they get to shoot it out with a portion
138 * of the che's depending on the # of mil in the sector. Chance to contact
139 * is around 10% per every equal number of mil:che ratio in the sector.
140 * "contact" is by 20% of the military in the sector, and odds are equal.
142 * Without a doubt this routine should be broken up, if only for readabilty.
145 guerrilla(struct sctstr *sp)
176 if ((n = getvar(V_CHE, (s_char *)sp, EF_SECTOR)) <= 0)
179 if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
184 victim = sp->sct_own;
185 actor = sp->sct_oldown;
186 che = get_che_value(che_combo);
189 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
191 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
192 if (lp->lnd_own != sp->sct_own)
195 mil += lnd_getmil(lp);
197 /* Security troops can now kill up to 1/2 their complement each
198 update, before doing anything else. */
199 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
203 lcp = &lchr[(int)lp->lnd_type];
204 mil += lnd_getmil(lp);
205 r = (((float)(lp->lnd_effic / 100) * (float)(lnd_getmil(lp))) /
209 che_kill = (roll(r) - 1);
214 "%s kills %d guerrilla%s in raid at %s!\n",
215 prland(lp), che_kill, splur(che_kill), ownxy(sp));
221 /* Security forces killed all the che */
223 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
227 target = get_che_cnum(che_combo);
229 /* the deity can't be a target! */
232 tnat = getnatp(target);
233 if ((tnat->nat_stat & STAT_INUSE) == 0) {
234 /* target nation has dissolved: che's retire. */
235 logerror("%d Che targeted at country %d retiring", che, target);
237 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
238 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
242 if (sp->sct_own != target) {
243 /*logerror("own %d != target %d", sp->sct_own, target); */
249 odds = (double)che / (mil + che);
250 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
252 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
256 } else if (che > mil && mil > 0) {
257 /*logerror("guerrilla shootout with military"); */
259 * shoot it out with the military, and kill them off.
260 * If loyalty bad enough, then take the sector over,
261 * and enlist 5% of civ as military force.
263 while (che > 0 && mil > 0) {
274 n = sp->sct_loyal - (random() % 15);
278 /*logerror("(#%d) mil beat che in %s", sp->sct_own, */
283 /*logerror("(#%d) che beat mil in %s", sp->sct_own, */
286 take_casualties(sp, mc);
287 } else if (ratio < 5) {
289 * guerrillas have to resort to blowing things up.
290 * Note this disrupts work in the sector.
293 n = (random() % 10) + (random() % che);
296 tmp = sp->sct_work - n;
301 "Production %s disrupted by terrorists in %s\n",
302 effadv(n), ownxy(sp));
303 sect_damage(sp, n / 10, 0);
304 /*logerror("(#%d) che blew up %s for %d", sp->sct_own, */
309 /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own, */
310 /*che, mil, ownxy(sp)); */
313 if (mil > 0 && che > 0) {
315 * we only get here if we haven't had combat previously.
316 * Chance to catch them.
317 * 20% of mil involved in attacking the che's.
319 if (chance(ratio * 0.10)) {
321 if ((n + che) == 0) {
322 logerror("n=%d che=%d\n", n, che);
326 odds = (double)che / (n + che);
327 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
328 while (che > 0 && n > 0) {
337 take_casualties(sp, mc);
339 /*logerror("Caught che; mc: %d, cc: %d", cc, mc); */
342 if (convert && sp->sct_loyal >= 50) {
344 /* new owner gets to keep the mobility there */
345 oldmob = sp->sct_mobil;
346 /* che won, and sector converts. */
347 if (sp->sct_own == sp->sct_oldown)
350 takeover(sp, sp->sct_oldown);
351 sp->sct_mobil = oldmob;
355 * so we can't keep losing money by having
358 if (sp->sct_type == SCT_CAPIT && sp->sct_newtype == SCT_CAPIT)
359 sp->sct_newtype = SCT_AGRI;
362 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
363 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
364 putvar(V_MILIT, n, (s_char *)sp, EF_SECTOR);
368 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
369 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
371 if (recruit && che > 0) {
372 /* loyalty drops during recruitment efforts */
375 n += (random() % 5) + 1;
377 n += (random() % 10) + 4;
381 if (sp->sct_oldown != sp->sct_own || n > 100) {
382 n = civ * (random() % 3) / 200;
383 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
384 if (n + che > CHE_MAX)
388 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
390 n = uw * (random() % 3) / 200;
391 if (n + che > CHE_MAX)
395 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
398 if (move && che > 0) {
399 struct sctstr *nicest_sp = 0;
404 /* search adjacent sectors for a nice one */
405 for (n = 1; n <= 6; n++) {
406 nsp = getsectp(sp->sct_x + diroff[n][0],
407 sp->sct_y + diroff[n][1]);
408 if (dchr[nsp->sct_type].d_mcst == 0)
410 if (nsp->sct_own != target)
412 if ((val = getvar(V_CHE, (s_char *)nsp, EF_SECTOR)) > 0) {
414 if (get_che_cnum(che_combo) != target)
416 if (get_che_value(che_combo) + che > CHE_MAX)
419 val = getvar(V_MILIT, (s_char *)nsp, EF_SECTOR);
420 /* don't give che more precise info than spy */
421 val = roundintby(val, 10);
422 /* inject a modicum of indeterminism; also
423 * avoids che preferring certain directions */
424 val += random() % 10 - 5;
430 /* if we found a nice sector, go there */
431 if (nicest_sp != 0) {
432 che_combo = getvar(V_CHE, (s_char *)nicest_sp, EF_SECTOR);
433 che += get_che_value(che_combo);
434 set_che_value(che_combo, che);
435 set_che_cnum(che_combo, target);
436 putvar(V_CHE, (int)che_combo, (s_char *)nicest_sp, EF_SECTOR);
441 set_che_value(che_combo, che);
442 set_che_cnum(che_combo, target);
443 putvar(V_CHE, (int)che_combo, (s_char *)sp, EF_SECTOR);
445 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
446 if (mc > 0 || cc > 0) {
447 /* don't tell who won just to be mean */
449 "Guerrilla warfare in %s\n",
450 xyas(sp->sct_x, sp->sct_y, target));
451 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
452 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
457 take_casualties(struct sctstr *sp, int mc)
461 int nunits = 0, each, deq;
465 /* casualties come out of mil first */
466 orig_mil = getvar(V_MILIT, (s_char *)sp, EF_SECTOR);
468 if (mc <= orig_mil) {
469 putvar(V_MILIT, (orig_mil - mc), (s_char *)sp, EF_SECTOR);
472 putvar(V_MILIT, 0, (s_char *)sp, EF_SECTOR);
474 /* remaining casualites */
478 * Need to take total_casualties and divide
479 * them amongst the land units in the sector
480 * Do security troops first, then others.
481 * Try not to kill any unit.
483 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
484 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
486 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
493 each = (mc / nunits) + 2;
495 /* kill some security troops */
496 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
497 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
498 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
501 cantake = (((float)(lp->lnd_effic - 40) / 100.0) *
502 (float)lnd_getmil(lp)) * 2;
503 /* (float)lchr[lp->lnd_type].l_mil)*2; */
505 if (cantake >= each) {
506 /* deq = (((float)each/(float)(lchr[lp->lnd_type].l_mil*2)) */
507 deq = (((float)each / (float)(lnd_getmil(lp) * 2))
510 } else if (cantake > 0) {
511 deq = (((float)cantake / (float)(lnd_getmil(lp) * 2)) * 100.0);
512 /* (float)(lchr[lp->lnd_type].l_mil*2)) * 100.0); */
513 mc -= (((float)deq / 100.0) * (float)lnd_getmil(lp)) * 2;
514 /* (float)lchr[lp->lnd_type].l_mil)*2; */
518 lp->lnd_effic -= deq;
519 lp->lnd_mobil -= deq / 2;
520 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
526 /* kill some normal troops */
527 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
528 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
529 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
532 cantake = (((float)(lp->lnd_effic - 40) / 100.0) *
533 (float)lnd_getmil(lp));
535 if (cantake >= each) {
536 deq = (((float)each / (float)(lnd_getmil(lp) * 2))
539 } else if (cantake > 0) {
540 deq = (((float)cantake / (float)lnd_getmil(lp))
542 mc -= (((float)deq / 100.0) * (float)lnd_getmil(lp));
545 lp->lnd_effic -= deq;
546 lp->lnd_mobil -= deq / 2;
547 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
553 /* Hmm.. still some left.. kill off units now */
554 /* kill some normal troops */
555 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
556 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
557 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
560 mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp));
562 lnd_submil(lp, 1000); /* Remove 'em all */
563 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
564 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
565 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
571 /* Hmm.. still some left.. kill off units now */
572 /* kill some security troops */
573 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
574 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
575 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
578 mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp)) * 2;
580 lnd_submil(lp, 1000); /* Kill 'em all */
581 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
582 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
583 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
589 /* Hmm.. everyone dead.. too bad */