]> git.pond.sub.org Git - empserver/blob - src/lib/update/revolt.c
Import of Empire 4.2.12
[empserver] / src / lib / update / revolt.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  revolt.c: Have disloyal populace revolt!
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 1997-2000
33  */
34
35 #include "misc.h"
36 #include "var.h"
37 #include "sect.h"
38 #include "nat.h"
39 #include "news.h"
40 #include "var.h"
41 #include "file.h"
42 #include "path.h"
43 #include "xy.h"
44 #include "land.h"
45 #include "nsc.h"
46 #include "plane.h"
47 #include "update.h"
48 #include "common.h"
49 #include "gen.h"
50 #include "lost.h"
51 #include "subs.h"
52
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))
57
58 #define CHE_MAX                 255
59
60 void
61 revolt(struct sctstr *sp)
62 {
63     int che_civ;
64     int che_uw;
65     int civ;
66     int uw;
67     u_short     che_combo;
68     int che;
69     int n;
70     int target;
71     
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))
76         return;
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)
80         return;
81     che_uw = 0;
82     che_civ = 0;
83     /* che due to civilian unrest */
84     n = 10 - (random() % 20);
85     che_civ = 3 + (civ * n/500);
86     if (che_civ < 0)
87         che_civ = 0;
88     else if (che_civ * 3 > civ)
89         che_civ = civ / 3;
90     if (che + che_civ > CHE_MAX)
91         che_civ = CHE_MAX - che;
92     che += che_civ;
93     if (che < CHE_MAX) {
94         /* che due to uw unrest */
95         n = 10 + (random() % 30);
96         che_uw = 5 + (uw * n/500);
97         if (che_uw > uw)
98             che_uw = uw;
99         if (che + che_uw > CHE_MAX)
100             che_uw = CHE_MAX - che_uw;
101         che += che_uw;
102     }
103     if (che_civ + che_uw > 0) {
104         civ -= che_civ;
105         uw -= che_uw;
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);
109         if (che_civ > 0)
110             putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
111         if (che_uw > 0)
112             putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
113 #ifdef DEBUG
114         logerror("(#%d) %d che fired up in %s",
115                  sp->sct_own, che, ownxy(sp));
116 #endif
117     }
118 }
119
120 /*
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
126  *   military.
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.
131  *
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.
139  *
140  * Without a doubt this routine should be broken up, if only for readabilty.
141  */
142 void
143 guerrilla(struct sctstr *sp)
144 {
145     extern      s_char *effadv();
146     struct      sctstr *nsp;
147     int recruit;
148     int move;
149     int ratio;
150     int che;
151     int mil;
152     int cc, mc;
153     double      odds;
154     int civ;
155     int n;
156     int uw;
157     natid       target;
158     struct      natstr *tnat;
159     int convert;
160     natid       actor;
161     natid       victim;
162     u_short     che_combo;
163     int vec[I_MAX+1];
164     int tmp;
165     int min_mil;
166     int val;
167     int oldmob;
168     struct      lndstr *lp;
169     s_char      *nxtitemp(struct nstr_item *np, int owner);
170     struct      nstr_item ni;
171     extern  double  hap_fact();
172     
173     mc = cc = 0;
174     recruit = 0;
175     convert = 0;
176     move = 0;
177     if ((n = getvar(V_CHE, (s_char *)sp, EF_SECTOR)) <= 0)
178                 return;
179     che_combo = n;
180     if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
181                 return;
182     civ = vec[I_CIVIL];
183     
184     uw = vec[I_UW];
185     victim = sp->sct_own;
186     actor = sp->sct_oldown;
187     che = get_che_value(che_combo);
188     
189     mil = vec[I_MILIT];
190     snxtitem_xy(&ni, EF_LAND, sp->sct_x,sp->sct_y);
191
192     while (NULL != (lp=(struct lndstr *)nxtitemp(&ni, 0))){
193                 if (lp->lnd_own != sp->sct_own)
194                         continue;
195                 
196                 mil += lnd_getmil(lp);
197                 
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){
201                         int     che_kill, r;
202                         struct lchrstr *lcp;
203                         
204                         lcp = &lchr[(int)lp->lnd_type];
205                         mil += lnd_getmil(lp);
206                         r = (((float)(lp->lnd_effic / 100) * (float)(lnd_getmil(lp))) / 2);
207                         if (r < 2)
208                                 r = 2;
209                         che_kill = (roll(r) - 1);
210                         if (che_kill > che)
211                            che_kill = che;
212                         if (che_kill) {
213                            wu(0, sp->sct_own, 
214                               "%s kills %d guerrilla%s in raid at %s!\n",
215                               prland(lp),
216                               che_kill, splur(che_kill),
217                               ownxy(sp));
218                            che -= che_kill;
219                         }
220                 }
221     }
222     
223     /* Security forces killed all the che */
224     if (che <= 0) {
225                 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
226                 return;
227     }
228     
229     target = get_che_cnum(che_combo);
230     if (target == 0) {
231                 /* the deity can't be a target! */
232                 return;
233     }
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);
238                 civ += che;
239                 putvar(V_CHE, 0, (s_char *)sp, EF_SECTOR);
240                 putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
241                 return;
242     }
243         
244     if (sp->sct_own != target) {
245                 /*logerror("own %d != target %d", sp->sct_own, target);*/
246                 move++;
247                 goto domove;
248     }
249     
250     ratio = mil / che;
251     odds = (double) che / (mil+che);
252     odds /= hap_fact(tnat,getnatp(sp->sct_oldown));
253     if (mil == 0) {
254                 wu(0, sp->sct_own, "Revolutionary subversion reported in %s!\n",
255                    ownxy(sp));
256                 recruit++;
257                 convert++;
258     } else if (che > mil && mil > 0) {
259                 /*logerror("guerrilla shootout with military");*/
260                 /*
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.
264                  */
265                 while (che > 0 && mil > 0) {
266                         if (chance(odds)) {
267                                 mc++;
268                                 mil--;
269                         } else {
270                                 cc++;
271                                 che--;
272                         }
273                 }
274                 if (mil > 0) {
275                         /* military won.  */
276                         n = sp->sct_loyal - (random() % 15);
277                         if (n < 0)
278                                 n = 0;
279                         sp->sct_loyal = n;
280                         /*logerror("(#%d) mil beat che in %s", sp->sct_own,*/
281                         /*ownxy(sp));*/
282                 } else {
283                         convert++;
284                         recruit++;
285                         /*logerror("(#%d) che beat mil in %s", sp->sct_own,*/
286                         /*ownxy(sp));*/
287                 }
288                 take_casualties(sp,mc);
289     } else if (ratio < 5) {
290                 /*
291                  * guerrillas have to resort to blowing things up.
292                  * Note this disrupts work in the sector.
293                  */
294                 n = 0;
295                 n = (random() % 10) + (random() % che);
296                 if (n > 100)
297                         n = 100;
298                 tmp = sp->sct_work - n;
299                 if (tmp < 0)
300                         tmp = 0;
301                 sp->sct_work = tmp;
302                 wu(0, sp->sct_own,
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,*/
307                 /*ownxy(sp), n);*/
308                 recruit++;
309     } else {
310                 /* ratio >= 5 */
311                 /*logerror("(#%d) %d che fleeing %d mil in %s", sp->sct_own,*/
312                 /*che, mil, ownxy(sp));*/
313                 move++;
314     }
315     if (mil > 0 && che > 0) {
316                 /*
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.
320                  */
321                 if (chance(ratio*0.10)) {
322                         n = (mil/5) + 1;
323                         if ((n+che) == 0){
324                                 logerror("n=%d che=%d\n",n,che);
325                                 if (che == 0)
326                                         return;
327                         }
328                         odds = (double) che / (n + che);
329                         odds /= hap_fact(tnat,getnatp(sp->sct_oldown));
330                         while (che > 0 && n > 0) {
331                                 if (chance(odds)) {
332                                         mc++;
333                                         n--;
334                                 } else {
335                                         cc++;
336                                         che--;
337                                 }
338                         }
339                         take_casualties(sp,mc);
340                         recruit = 0;
341                         /*logerror("Caught che; mc: %d, cc: %d", cc, mc);*/
342                 }
343     }
344     if (convert && sp->sct_loyal >= 50) {
345                 register int n;
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)
350                         sp->sct_oldown = 0;
351                 else
352                         takeover(sp, sp->sct_oldown);
353                 sp->sct_mobil = oldmob;
354                 civ += uw;
355                 uw = 0;
356                 /*
357                  * so we can't keep losing money by having
358                  * our cap retaken
359                  */
360                 if (sp->sct_type == SCT_CAPIT &&
361                         sp->sct_newtype == SCT_CAPIT)
362                         sp->sct_newtype = SCT_AGRI;
363                 n = civ / 20;
364                 civ -= n;
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);
368                 move++;
369                 recruit = 0;
370                 if (sp->sct_own)
371                         wu(0, sp->sct_own, "Sector %s has been retaken!\n",
372                            xyas(sp->sct_x, sp->sct_y, sp->sct_own));
373     }
374     if (recruit && che > 0) {
375                 /* loyalty drops during recruitment efforts */
376                 n = sp->sct_loyal;
377                 if (n < 30)
378                         n += (random() % 5) + 1;
379                 else if (n < 70)
380                         n += (random() % 10) + 4;
381                 if (n > 127)
382                         n = 127;
383                 sp->sct_loyal = n;
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)
388                                 n = CHE_MAX - che;
389                         che += n;
390                         civ -= n;
391                         putvar(V_CIVIL, civ, (s_char *)sp, EF_SECTOR);
392                 }
393                 n = uw * (random() % 3) / 200;
394                 if (n + che > CHE_MAX)
395                         n = CHE_MAX - che;
396                 che += n;
397                 uw -= n;
398                 putvar(V_UW, uw, (s_char *)sp, EF_SECTOR);
399     }
400  domove:
401     if (move && che > 0) {
402                 struct sctstr *maybe_sp = 0;
403                 if (convert)
404                         min_mil = 999;
405                 else
406                         min_mil = mil;
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)
411                                 continue;
412                         if (nsp->sct_own != target)
413                                 continue;
414                         if ((val = getvar(V_CHE, (s_char *)nsp, EF_SECTOR)) > 0) {
415                                 che_combo = val;
416                                 if (get_che_cnum(che_combo) != target)
417                                         continue;
418                                 if (get_che_value(che_combo) + che > CHE_MAX)
419                                         continue;
420                         }
421                         val = getvar(V_MILIT, (s_char *)nsp, EF_SECTOR);
422                         if (val >= min_mil)
423                                 continue;
424                         maybe_sp = nsp;
425                         min_mil = val;
426                 }
427                 /*
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").
431                  */
432                 if (maybe_sp != 0) {
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);
438                         che = 0;
439                 }
440     }
441     if (che > 0) {
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);
445     } else
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 */
449                 wu(0, target,
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);
454     }
455 }
456
457 void
458 take_casualties(struct sctstr *sp, int mc)
459 {
460     int orig_mil;
461     int cantake;
462     int nunits=0, each, deq;
463     struct      lndstr *lp;
464     s_char      *nxtitemp(struct nstr_item *np, int owner);
465     struct      nstr_item ni;
466     
467     /* casualties come out of mil first */
468     orig_mil = getvar(V_MILIT, (s_char *)sp, EF_SECTOR);
469     
470     if (mc <= orig_mil){
471         putvar(V_MILIT, (orig_mil-mc), (s_char *)sp, EF_SECTOR);
472         return;
473     }
474     putvar(V_MILIT, 0, (s_char *)sp, EF_SECTOR);
475     
476     /* remaining casualites */
477     mc -= orig_mil;
478     
479     /*
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.
484      */
485     snxtitem_xy(&ni, EF_LAND, sp->sct_x,sp->sct_y);
486     while(NULL != (lp=(struct lndstr *)nxtitemp(&ni, 0))){
487         nunits++;
488         if (lchr[(int)lp->lnd_type].l_flags & L_SECURITY)
489             nunits++;
490     }
491     
492     if (nunits==0)
493         return;
494     
495     each = (mc/nunits)+2;
496     
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))
501             continue;
502         
503         cantake = (((float)(lp->lnd_effic-40)/100.0)*
504                    (float)lnd_getmil(lp))*2;
505         /*                              (float)lchr[lp->lnd_type].l_mil)*2;*/
506         
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))
510                    *100.0);
511             mc -= each;
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;*/
519         }else
520             deq = 0;
521         
522         lp->lnd_effic -= deq;
523         lp->lnd_mobil -= deq/2;
524         deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
525         lnd_submil(lp, deq);
526         if (mc<=0) return;
527     }
528     
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)
533             continue;
534         
535         cantake = (((float)(lp->lnd_effic-40)/100.0)*
536                    (float)lnd_getmil(lp));
537         
538         if (cantake >= each){
539             deq = (((float)each/(float)(lnd_getmil(lp)*2))
540                    *100.0);
541             mc -= each;
542         }else if (cantake > 0){
543             deq = (((float)cantake/(float)lnd_getmil(lp))
544                    * 100.0);
545             mc -= (((float)deq/100.0)*
546                    (float)lnd_getmil(lp));
547         }else
548             deq = 0;
549         lp->lnd_effic -= deq;
550         lp->lnd_mobil -= deq/2;
551         deq = (double)lchr[(int)lp->lnd_type].l_mil * (deq / 100.0);
552         lnd_submil(lp, deq);
553         if (mc<=0) return;
554     }
555     
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)
561             continue;
562         
563         mc -= (((float)lp->lnd_effic/100.0) *
564                (float)lnd_getmil(lp));
565         lp->lnd_effic = 0;
566         lnd_submil(lp, 1000); /* Remove 'em all */
567         wu(0,lp->lnd_own,"%s dies fighting guerrillas in %s\n",
568            prland(lp),
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);
571         lp->lnd_own = 0;
572         if (mc<=0) return;
573     }
574     
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))
580             continue;
581         
582         mc -= (((float)lp->lnd_effic / 100.0) * (float)lnd_getmil(lp)) * 2;
583         lp->lnd_effic = 0;
584         lnd_submil(lp, 1000); /* Kill 'em all */
585         wu(0, lp->lnd_own, "%s dies fighting guerrillas in %s\n",
586            prland(lp),
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);
589         lp->lnd_own = 0;
590         if (mc <= 0) 
591             return;
592     }
593     
594     /* Hmm.. everyone dead.. too bad */
595 }