]> git.pond.sub.org Git - empserver/blob - src/lib/commands/anti.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / commands / anti.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  *  anti.c: Take action against che
29  * 
30  *  Known contributors to this file:
31  *     Pat Loney, 1992
32  *     Steve McClure, 1997
33  */
34
35
36 #include <stdio.h>
37 #include <ctype.h>
38 #include "misc.h"
39 #include "player.h"
40 #include "var.h"
41 #include "xy.h"
42 #include "sect.h"
43 #include "nat.h"
44 #include "news.h"
45 #include "nsc.h"
46 #include "item.h"
47 #include "file.h"
48 #include "commands.h"
49 #include "optlist.h"
50
51 extern int etu_per_update;
52 extern int sect_mob_neg_factor;
53 /*
54  * format: anti <SECT>
55  */
56 int
57 anti(void)
58 {
59     struct sctstr sect;
60     int nsect;
61     struct nstr_sect nstr;
62     int cond[I_MAX + 1];
63     int mil, che, target;
64     int avail_mil;
65     int amil, ache;
66     int milkilled, chekilled;
67     double odds, damil, dache;
68     int mob;
69     int n_cheleft;
70     extern double hap_fact();
71
72     if (!snxtsct(&nstr, player->argp[1]))
73         return RET_SYN;
74     prdate();
75     nsect = 0;
76     while (nxtsct(&nstr, &sect)) {
77         if (!player->owner)
78             continue;
79         if (nsect++ == 0) {
80             pr("  sect    subversion activity report\n");
81             pr("  ----    --------------------------\n");
82         }
83         getvec(VT_COND, cond, (char *)&sect, EF_SECTOR);
84         mil = getvar(V_MILIT, (char *)&sect, EF_SECTOR);
85         che = get_che_value(cond[C_CHE]);
86         target = get_che_cnum(cond[C_CHE]);
87         avail_mil = sect.sct_mobil / 2;
88         if (mil <= avail_mil)
89             avail_mil = mil;
90         prxy("%4d,%-4d ", sect.sct_x, sect.sct_y, player->cnum);
91         if (avail_mil <= 0) {
92             pr("No available mil or mob in sector.\n");
93             continue;
94         }
95         pr("Sector mobility/troop strength will allow %d troops to engage.\n", avail_mil);
96
97         if (target == player->cnum) {
98             amil = mil;
99             ache = che;
100             milkilled = 0;
101             chekilled = 0;
102             mob = sect.sct_mobil;
103             while (amil != 0 && ache != 0 && mob > 1) {
104                 damil = amil;
105                 dache = ache;
106                 odds = (dache * 2.0 / (damil + dache));
107                 odds /= hap_fact(getnatp(sect.sct_own),
108                                  getnatp(sect.sct_oldown));
109                 mob = mob - 2;
110                 if (chance(odds)) {
111                     amil = amil - 1;
112                     milkilled = milkilled + 1;
113                 } else {
114                     ache = ache - 1;
115                     chekilled = chekilled + 1;
116                 }
117             }
118             if (mil - milkilled > 0) {
119                 sect.sct_mobil = sect.sct_mobil - chekilled - milkilled;
120                 putvar(V_MILIT, mil - milkilled, (char *)&sect, EF_SECTOR);
121                 if (ache == 0)
122                     cond[C_CHE] = 0;
123                 set_che_value(cond[C_CHE], ache);
124                 putvar(V_CHE, cond[C_CHE], (char *)&sect, EF_SECTOR);
125                 putsect(&sect);
126                 pr("          Body count:  Military %d - Guerillas %d.\n",
127                    milkilled, chekilled);
128                 if (ache == 0) {
129                     pr("          Partisans cleared out of this sector for now.\n");
130                 } else {
131                     pr("          Partisans still active in this sector.\n");
132                 }
133             } else {
134                 if (opt_MOB_ACCESS) {
135                     sect.sct_mobil =
136                         -(etu_per_update / sect_mob_neg_factor);
137                 } else {
138                     sect.sct_mobil = 0;
139                 }
140                 sect.sct_loyal = sect.sct_loyal * 0.5;
141                 n_cheleft = (random() % 4);
142                 /* 75% chance some che will get left */
143                 if (n_cheleft) {
144                     /* Ok, now leave anywhere from 16% to 25% of the che */
145                     n_cheleft = (ache / (n_cheleft + 3));
146                     ache -= n_cheleft;
147                     set_che_value(cond[C_CHE], n_cheleft);
148                 } else
149                     cond[C_CHE] = 0;
150                 putvar(V_MILIT, ache, (char *)&sect, EF_SECTOR);
151                 putvar(V_CHE, cond[C_CHE], (char *)&sect, EF_SECTOR);
152                 if (sect.sct_own == sect.sct_oldown)
153                     sect.sct_oldown = 0;
154                 makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x,
155                          sect.sct_y);
156                 makenotlost(EF_SECTOR, sect.sct_oldown, 0, sect.sct_x,
157                             sect.sct_y);
158                 sect.sct_own = sect.sct_oldown;
159                 sect.sct_off = 1;       /* Turn the sector off */
160                 putsect(&sect);
161                 pr("          Partisans take over the sector.  You blew it.\n");
162                 wu(0, sect.sct_oldown,
163                    "Sector %s regained from Partisan activity.\n",
164                    xyas(nstr.x, nstr.y, sect.sct_oldown));
165             }
166         } else {
167             pr("          Body count:  Military 0 - Guerillas 0.\n");
168             pr("          Partisans cleared out of this sector for now.\n");
169         }
170     }
171     pr("%d sector%s\n", nsect, splur(nsect));
172     return RET_OK;
173 }