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 #define get_che_cnum(x) ((x) >> 8)
54 #define set_che_cnum(x, cn) ((x) = ((x) & 0xff) | ((cn) << 8))
55 #define get_che_value(x) ((x) & 0xff)
56 #define set_che_value(x, n) ((x) = ((x) & 0xff00) | (n))
61 revolt(struct sctstr *sp)
72 che_combo = getvar(V_CHE, (s_char *)sp, EF_SECTOR);
73 che = get_che_value(che_combo);
74 target = get_che_cnum(che_combo);
75 if (che_combo != 0 && (target != sp->sct_own || che >= CHE_MAX))
77 civ = getvar(V_CIVIL, (s_char *)sp, EF_SECTOR);
78 uw = getvar(V_UW, (s_char *)sp, EF_SECTOR);
79 if (che > (civ + uw) * 3)
83 /* che due to civilian unrest */
84 n = 10 - (random() % 20);
85 che_civ = 3 + (civ * n / 500);
88 else if (che_civ * 3 > civ)
90 if (che + che_civ > CHE_MAX)
91 che_civ = CHE_MAX - che;
94 /* che due to uw unrest */
95 n = 10 + (random() % 30);
96 che_uw = 5 + (uw * n / 500);
99 if (che + che_uw > CHE_MAX)
100 che_uw = CHE_MAX - che_uw;
103 if (che_civ + che_uw > 0) {
106 set_che_cnum(che_combo, sp->sct_own);
107 set_che_value(che_combo, che);
108 putvar(V_CHE, (int)che_combo, (s_char *)sp, EF_SECTOR);
110 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
112 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
114 logerror("(#%d) %d che fired up in %s",
115 sp->sct_own, che, ownxy(sp));
121 * summary of effects.
122 * if there are no military in the sector, che recruit from
123 * populace if pop loyalty is > 10. They spread subversion otherwise,
124 * trying to lower pop loyalty.
125 * if che outnumber military, they stay and shoot it out, kill the
127 * if che are outnumbered by less than 5 to 1, they blow up stuff,
128 * killing innocent civilians (never uw's) and damaging commodities.
129 * if che are outnumbered by more than 5 to 1, they try to leave the
130 * sector for a nearby sector with fewer military.
132 * if the military lose any attacks, the pop loyalty in the sector
133 * gets worse, representing military defeat.
134 * military can "catch" che's after bombing attacks, or after they move.
135 * If military catch them, then they get to shoot it out with a portion
136 * of the che's depending on the # of mil in the sector. Chance to contact
137 * is around 10% per every equal number of mil:che ratio in the sector.
138 * "contact" is by 20% of the military in the sector, and odds are equal.
140 * Without a doubt this routine should be broken up, if only for readabilty.
143 guerrilla(struct sctstr *sp)
145 extern s_char *effadv();
169 s_char *nxtitemp(struct nstr_item *np, int owner);
171 extern double hap_fact();
177 if ((n = getvar(V_CHE, (s_char *)sp, EF_SECTOR)) <= 0)
180 if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
185 victim = sp->sct_own;
186 actor = sp->sct_oldown;
187 che = get_che_value(che_combo);
190 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
192 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
193 if (lp->lnd_own != sp->sct_own)
196 mil += lnd_getmil(lp);
198 /* Security troops can now kill up to 1/2 their complement each
199 update, before doing anything else. */
200 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
204 lcp = &lchr[(int)lp->lnd_type];
205 mil += lnd_getmil(lp);
206 r = (((float)(lp->lnd_effic / 100) * (float)(lnd_getmil(lp))) /
210 che_kill = (roll(r) - 1);
215 "%s kills %d guerrilla%s in raid at %s!\n",
216 prland(lp), che_kill, splur(che_kill), ownxy(sp));
222 /* Security forces killed all the che */
224 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
228 target = get_che_cnum(che_combo);
230 /* the deity can't be a target! */
233 tnat = getnatp(target);
234 if ((tnat->nat_stat & STAT_INUSE) == 0) {
235 /* target nation has dissolved: che's retire. */
236 logerror("%d Che targeted at country %d retiring", che, target);
238 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
239 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
243 if (sp->sct_own != target) {
244 /*logerror("own %d != target %d", sp->sct_own, target); */
250 odds = (double)che / (mil + che);
251 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
253 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
257 } else if (che > mil && mil > 0) {
258 /*logerror("guerrilla shootout with military"); */
260 * shoot it out with the military, and kill them off.
261 * If loyalty bad enough, then take the sector over,
262 * and enlist 5% of civ as military force.
264 while (che > 0 && mil > 0) {
275 n = sp->sct_loyal - (random() % 15);
279 /*logerror("(#%d) mil beat che in %s", sp->sct_own, */
284 /*logerror("(#%d) che beat mil in %s", sp->sct_own, */
287 take_casualties(sp, mc);
288 } else if (ratio < 5) {
290 * guerrillas have to resort to blowing things up.
291 * Note this disrupts work in the sector.
294 n = (random() % 10) + (random() % che);
297 tmp = sp->sct_work - n;
302 "Production %s disrupted by terrorists in %s\n",
303 effadv(n), ownxy(sp));
304 sect_damage(sp, n / 10, 0);
305 /*logerror("(#%d) che blew up %s for %d", sp->sct_own, */
310 /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own, */
311 /*che, mil, ownxy(sp)); */
314 if (mil > 0 && che > 0) {
316 * we only get here if we haven't had combat previously.
317 * Chance to catch them.
318 * 20% of mil involved in attacking the che's.
320 if (chance(ratio * 0.10)) {
322 if ((n + che) == 0) {
323 logerror("n=%d che=%d\n", n, che);
327 odds = (double)che / (n + che);
328 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
329 while (che > 0 && n > 0) {
338 take_casualties(sp, mc);
340 /*logerror("Caught che; mc: %d, cc: %d", cc, mc); */
343 if (convert && sp->sct_loyal >= 50) {
345 /* new owner gets to keep the mobility there */
346 oldmob = sp->sct_mobil;
347 /* che won, and sector converts. */
348 if (sp->sct_own == sp->sct_oldown)
351 takeover(sp, sp->sct_oldown);
352 sp->sct_mobil = oldmob;
356 * so we can't keep losing money by having
359 if (sp->sct_type == SCT_CAPIT && sp->sct_newtype == SCT_CAPIT)
360 sp->sct_newtype = SCT_AGRI;
363 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
364 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
365 putvar(V_MILIT, n, (s_char *)sp, EF_SECTOR);
369 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
370 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
372 if (recruit && che > 0) {
373 /* loyalty drops during recruitment efforts */
376 n += (random() % 5) + 1;
378 n += (random() % 10) + 4;
382 if (sp->sct_oldown != sp->sct_own || n > 100) {
383 n = civ * (random() % 3) / 200;
384 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
385 if (n + che > CHE_MAX)
389 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
391 n = uw * (random() % 3) / 200;
392 if (n + che > CHE_MAX)
396 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
399 if (move && che > 0) {
400 struct sctstr *maybe_sp = 0;
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);
426 * if n <= 6, we found a sector owned by TARGET which
427 * is a nice sector. Otherwise, we move to the first
428 * one we find ("maybe_sp").
431 che_combo = getvar(V_CHE, (s_char *)maybe_sp, EF_SECTOR);
432 che += get_che_value(che_combo);
433 set_che_value(che_combo, che);
434 set_che_cnum(che_combo, target);
435 putvar(V_CHE, (int)che_combo, (s_char *)maybe_sp, EF_SECTOR);
440 set_che_value(che_combo, che);
441 set_che_cnum(che_combo, target);
442 putvar(V_CHE, (int)che_combo, (s_char *)sp, EF_SECTOR);
444 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
445 if (mc > 0 || cc > 0) {
446 /* don't tell who won just to be mean */
448 "Guerrilla warfare in %s\n",
449 xyas(sp->sct_x, sp->sct_y, target));
450 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
451 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
456 take_casualties(struct sctstr *sp, int mc)
460 int nunits = 0, each, deq;
462 s_char *nxtitemp(struct nstr_item *np, int owner);
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 */