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))) / 2);
209 che_kill = (roll(r) - 1);
214 "%s kills %d guerrilla%s in raid at %s!\n",
216 che_kill, splur(che_kill),
223 /* Security forces killed all the che */
225 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
229 target = get_che_cnum(che_combo);
231 /* the deity can't be a target! */
234 tnat = getnatp(target);
235 if ((tnat->nat_stat & STAT_INUSE) == 0) {
236 /* target nation has dissolved: che's retire. */
237 logerror("%d Che targeted at country %d retiring", che, target);
239 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
240 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
244 if (sp->sct_own != target) {
245 /*logerror("own %d != target %d", sp->sct_own, target);*/
251 odds = (double) che / (mil+che);
252 odds /= hap_fact(tnat,getnatp(sp->sct_oldown));
254 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
258 } else if (che > mil && mil > 0) {
259 /*logerror("guerrilla shootout with military");*/
261 * shoot it out with the military, and kill them off.
262 * If loyalty bad enough, then take the sector over,
263 * and enlist 5% of civ as military force.
265 while (che > 0 && mil > 0) {
276 n = sp->sct_loyal - (random() % 15);
280 /*logerror("(#%d) mil beat che in %s", sp->sct_own,*/
285 /*logerror("(#%d) che beat mil in %s", sp->sct_own,*/
288 take_casualties(sp,mc);
289 } else if (ratio < 5) {
291 * guerrillas have to resort to blowing things up.
292 * Note this disrupts work in the sector.
295 n = (random() % 10) + (random() % che);
298 tmp = sp->sct_work - n;
303 "Production %s disrupted by terrorists in %s\n",
304 effadv(n), ownxy(sp));
305 sect_damage(sp, n/10, 0);
306 /*logerror("(#%d) che blew up %s for %d", sp->sct_own,*/
311 /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own,*/
312 /*che, mil, ownxy(sp));*/
315 if (mil > 0 && che > 0) {
317 * we only get here if we haven't had combat previously.
318 * Chance to catch them.
319 * 20% of mil involved in attacking the che's.
321 if (chance(ratio*0.10)) {
324 logerror("n=%d che=%d\n",n,che);
328 odds = (double) che / (n + che);
329 odds /= hap_fact(tnat,getnatp(sp->sct_oldown));
330 while (che > 0 && n > 0) {
339 take_casualties(sp,mc);
341 /*logerror("Caught che; mc: %d, cc: %d", cc, mc);*/
344 if (convert && sp->sct_loyal >= 50) {
346 /* new owner gets to keep the mobility there */
347 oldmob = sp->sct_mobil;
348 /* che won, and sector converts. */
349 if (sp->sct_own == sp->sct_oldown)
352 takeover(sp, sp->sct_oldown);
353 sp->sct_mobil = oldmob;
357 * so we can't keep losing money by having
360 if (sp->sct_type == SCT_CAPIT &&
361 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);
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/
514 (float)(lnd_getmil(lp)*2)) * 100.0);
515 /* (float)(lchr[lp->lnd_type].l_mil*2)) * 100.0);*/
516 mc -= (((float)deq/100.0)*
517 (float)lnd_getmil(lp))*2;
518 /* (float)lchr[lp->lnd_type].l_mil)*2;*/
522 lp->lnd_effic -= deq;
523 lp->lnd_mobil -= deq/2;
524 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
529 /* kill some normal troops */
530 snxtitem_xy(&ni, EF_LAND, sp->sct_x,sp->sct_y);
531 while(NULL != (lp=(struct lndstr *)nxtitemp(&ni, 0))){
532 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
535 cantake = (((float)(lp->lnd_effic-40)/100.0)*
536 (float)lnd_getmil(lp));
538 if (cantake >= each){
539 deq = (((float)each/(float)(lnd_getmil(lp)*2))
542 }else if (cantake > 0){
543 deq = (((float)cantake/(float)lnd_getmil(lp))
545 mc -= (((float)deq/100.0)*
546 (float)lnd_getmil(lp));
549 lp->lnd_effic -= deq;
550 lp->lnd_mobil -= deq/2;
551 deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
556 /* Hmm.. still some left.. kill off units now */
557 /* kill some normal troops */
558 snxtitem_xy(&ni, EF_LAND, sp->sct_x,sp->sct_y);
559 while(NULL != (lp=(struct lndstr *)nxtitemp(&ni, 0))){
560 if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
563 mc -= (((float)lp->lnd_effic/100.0) *
564 (float)lnd_getmil(lp));
566 lnd_submil(lp, 1000); /* Remove 'em all */
567 wu(0,lp->lnd_own,"%s dies fighting guerrillas in %s\n",
569 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
570 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
575 /* Hmm.. still some left.. kill off units now */
576 /* kill some security troops */
577 snxtitem_xy(&ni, EF_LAND, sp->sct_x,sp->sct_y);
578 while(NULL != (lp = (struct lndstr *)nxtitemp(&ni, 0))){
579 if (!(lchr[(int)lp->lnd_type].l_flags & L_SECURITY))
582 mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp)) * 2;
584 lnd_submil(lp, 1000); /* Kill 'em all */
585 wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
587 xyas(lp->lnd_x, lp->lnd_y, lp->lnd_own));
588 makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
594 /* Hmm.. everyone dead.. too bad */