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)
147 extern s_char *effadv();
171 s_char *nxtitemp(struct nstr_item *np, int owner);
173 extern double hap_fact();
179 if ((n = getvar(V_CHE, (s_char *)sp, EF_SECTOR)) <= 0)
182 if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
187 victim = sp->sct_own;
188 actor = sp->sct_oldown;
189 che = get_che_value(che_combo);
192 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
194 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
195 if (lp->lnd_own != sp->sct_own)
198 mil += lnd_getmil(lp);
200 /* Security troops can now kill up to 1/2 their complement each
201 update, before doing anything else. */
202 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY) {
206 lcp = &lchr[(int)lp->lnd_type];
207 mil += lnd_getmil(lp);
208 r = (((float)(lp->lnd_effic / 100) * (float)(lnd_getmil(lp))) /
212 che_kill = (roll(r) - 1);
217 "%s kills %d guerrilla%s in raid at %s!\n",
218 prland(lp), che_kill, splur(che_kill), ownxy(sp));
224 /* Security forces killed all the che */
226 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
230 target = get_che_cnum(che_combo);
232 /* the deity can't be a target! */
235 tnat = getnatp(target);
236 if ((tnat->nat_stat & STAT_INUSE) == 0) {
237 /* target nation has dissolved: che's retire. */
238 logerror("%d Che targeted at country %d retiring", che, target);
240 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
241 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
245 if (sp->sct_own != target) {
246 /*logerror("own %d != target %d", sp->sct_own, target); */
252 odds = (double)che / (mil + che);
253 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
255 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
259 } else if (che > mil && mil > 0) {
260 /*logerror("guerrilla shootout with military"); */
262 * shoot it out with the military, and kill them off.
263 * If loyalty bad enough, then take the sector over,
264 * and enlist 5% of civ as military force.
266 while (che > 0 && mil > 0) {
277 n = sp->sct_loyal - (random() % 15);
281 /*logerror("(#%d) mil beat che in %s", sp->sct_own, */
286 /*logerror("(#%d) che beat mil in %s", sp->sct_own, */
289 take_casualties(sp, mc);
290 } else if (ratio < 5) {
292 * guerrillas have to resort to blowing things up.
293 * Note this disrupts work in the sector.
296 n = (random() % 10) + (random() % che);
299 tmp = sp->sct_work - n;
304 "Production %s disrupted by terrorists in %s\n",
305 effadv(n), ownxy(sp));
306 sect_damage(sp, n / 10, 0);
307 /*logerror("(#%d) che blew up %s for %d", sp->sct_own, */
312 /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own, */
313 /*che, mil, ownxy(sp)); */
316 if (mil > 0 && che > 0) {
318 * we only get here if we haven't had combat previously.
319 * Chance to catch them.
320 * 20% of mil involved in attacking the che's.
322 if (chance(ratio * 0.10)) {
324 if ((n + che) == 0) {
325 logerror("n=%d che=%d\n", n, che);
329 odds = (double)che / (n + che);
330 odds /= hap_fact(tnat, getnatp(sp->sct_oldown));
331 while (che > 0 && n > 0) {
340 take_casualties(sp, mc);
342 /*logerror("Caught che; mc: %d, cc: %d", cc, mc); */
345 if (convert && sp->sct_loyal >= 50) {
347 /* new owner gets to keep the mobility there */
348 oldmob = sp->sct_mobil;
349 /* che won, and sector converts. */
350 if (sp->sct_own == sp->sct_oldown)
353 takeover(sp, sp->sct_oldown);
354 sp->sct_mobil = oldmob;
358 * so we can't keep losing money by having
361 if (sp->sct_type == SCT_CAPIT && sp->sct_newtype == SCT_CAPIT)
362 sp->sct_newtype = SCT_AGRI;
365 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
366 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
367 putvar(V_MILIT, n, (s_char *)sp, EF_SECTOR);
371 wu(0, sp->sct_own, "Sector %s has been retaken!\n",
372 xyas(sp->sct_x, sp->sct_y, sp->sct_own));
374 if (recruit && che > 0) {
375 /* loyalty drops during recruitment efforts */
378 n += (random() % 5) + 1;
380 n += (random() % 10) + 4;
384 if (sp->sct_oldown != sp->sct_own || n > 100) {
385 n = civ * (random() % 3) / 200;
386 n /= hap_fact(tnat, getnatp(sp->sct_oldown));
387 if (n + che > CHE_MAX)
391 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
393 n = uw * (random() % 3) / 200;
394 if (n + che > CHE_MAX)
398 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
401 if (move && che > 0) {
402 struct sctstr *maybe_sp = 0;
407 for (n = 1; n <= 6; n++) {
408 nsp = getsectp(sp->sct_x + diroff[n][0],
409 sp->sct_y + diroff[n][1]);
410 if (dchr[nsp->sct_type].d_mcst == 0)
412 if (nsp->sct_own != target)
414 if ((val = getvar(V_CHE, (s_char *)nsp, EF_SECTOR)) > 0) {
416 if (get_che_cnum(che_combo) != target)
418 if (get_che_value(che_combo) + che > CHE_MAX)
421 val = getvar(V_MILIT, (s_char *)nsp, EF_SECTOR);
428 * if n <= 6, we found a sector owned by TARGET which
429 * is a nice sector. Otherwise, we move to the first
430 * one we find ("maybe_sp").
433 che_combo = getvar(V_CHE, (s_char *)maybe_sp, EF_SECTOR);
434 che += get_che_value(che_combo);
435 set_che_value(che_combo, che);
436 set_che_cnum(che_combo, target);
437 putvar(V_CHE, (int)che_combo, (s_char *)maybe_sp, EF_SECTOR);
442 set_che_value(che_combo, che);
443 set_che_cnum(che_combo, target);
444 putvar(V_CHE, (int)che_combo, (s_char *)sp, EF_SECTOR);
446 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
447 if (mc > 0 || cc > 0) {
448 /* don't tell who won just to be mean */
450 "Guerrilla warfare in %s\n",
451 xyas(sp->sct_x, sp->sct_y, target));
452 wu(0, target, " body count: troops: %d, rebels: %d\n", mc, cc);
453 nreport(actor, N_FREEDOM_FIGHT, victim, 1);
458 take_casualties(struct sctstr *sp, int mc)
462 int nunits = 0, each, deq;
464 s_char *nxtitemp(struct nstr_item *np, int owner);
467 /* casualties come out of mil first */
468 orig_mil = getvar(V_MILIT, (s_char *)sp, EF_SECTOR);
470 if (mc <= orig_mil) {
471 putvar(V_MILIT, (orig_mil - mc), (s_char *)sp, EF_SECTOR);
474 putvar(V_MILIT, 0, (s_char *)sp, EF_SECTOR);
476 /* remaining casualites */
480 * Need to take total_casualties and divide
481 * them amongst the land units in the sector
482 * Do security troops first, then others.
483 * Try not to kill any unit.
485 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
486 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
488 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
495 each = (mc / nunits) + 2;
497 /* kill some security troops */
498 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
499 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
500 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
503 cantake = (((float)(lp->lnd_effic - 40) / 100.0) *
504 (float)lnd_getmil(lp)) * 2;
505 /* (float)lchr[lp->lnd_type].l_mil)*2; */
507 if (cantake >= each) {
508 /* deq = (((float)each/(float)(lchr[lp->lnd_type].l_mil*2)) */
509 deq = (((float)each / (float)(lnd_getmil(lp) * 2))
512 } else if (cantake > 0) {
513 deq = (((float)cantake / (float)(lnd_getmil(lp) * 2)) * 100.0);
514 /* (float)(lchr[lp->lnd_type].l_mil*2)) * 100.0); */
515 mc -= (((float)deq / 100.0) * (float)lnd_getmil(lp)) * 2;
516 /* (float)lchr[lp->lnd_type].l_mil)*2; */
520 lp->lnd_effic -= deq;
521 lp->lnd_mobil -= deq / 2;
522 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
528 /* kill some normal troops */
529 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
530 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
531 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
534 cantake = (((float)(lp->lnd_effic - 40) / 100.0) *
535 (float)lnd_getmil(lp));
537 if (cantake >= each) {
538 deq = (((float)each / (float)(lnd_getmil(lp) * 2))
541 } else if (cantake > 0) {
542 deq = (((float)cantake / (float)lnd_getmil(lp))
544 mc -= (((float)deq / 100.0) * (float)lnd_getmil(lp));
547 lp->lnd_effic -= deq;
548 lp->lnd_mobil -= deq / 2;
549 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
555 /* Hmm.. still some left.. kill off units now */
556 /* kill some normal troops */
557 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
558 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
559 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
562 mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp));
564 lnd_submil(lp, 1000); /* Remove 'em all */
565 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
566 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
567 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
573 /* Hmm.. still some left.. kill off units now */
574 /* kill some security troops */
575 snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
576 while (NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))) {
577 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
580 mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp)) * 2;
582 lnd_submil(lp, 1000); /* Kill 'em all */
583 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
584 prland(lp), xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
585 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
591 /* Hmm.. everyone dead.. too bad */