]> git.pond.sub.org Git - empserver/blob - src/lib/commands/anti.c
19d89445892d833b5c882d123157033bf0cb70e9
[empserver] / src / lib / commands / anti.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  anti.c: Take action against che
28  *
29  *  Known contributors to this file:
30  *     Pat Loney, 1992
31  *     Steve McClure, 1997
32  *     Markus Armbruster, 2004-2012
33  */
34
35 #include <config.h>
36
37 #include "chance.h"
38 #include "commands.h"
39 #include "item.h"
40 #include "optlist.h"
41
42 /*
43  * format: anti <SECT>
44  */
45 int
46 anti(void)
47 {
48     struct sctstr sect;
49     int nsect;
50     struct nstr_sect nstr;
51     int mil, che, target;
52     int avail_mil;
53     int amil, ache;
54     int milkilled, chekilled;
55     double odds, damil, dache;
56     int mob;
57     int n_cheleft;
58
59     if (!snxtsct(&nstr, player->argp[1]))
60         return RET_SYN;
61     prdate();
62     nsect = 0;
63     while (nxtsct(&nstr, &sect)) {
64         if (!player->owner)
65             continue;
66         if (nsect++ == 0) {
67             pr("  sect    subversion activity report\n");
68             pr("  ----    --------------------------\n");
69         }
70         mil = sect.sct_item[I_MILIT];
71         che = sect.sct_che;
72         target = sect.sct_che_target;
73         avail_mil = sect.sct_mobil / 2;
74         if (mil <= avail_mil)
75             avail_mil = mil;
76         prxy("%4d,%-4d ", sect.sct_x, sect.sct_y);
77         if (avail_mil <= 0) {
78             pr("No available mil or mob in sector.\n");
79             continue;
80         }
81         pr("Sector mobility/troop strength will allow %d troops to engage.\n",
82            avail_mil);
83
84         if (target == player->cnum) {
85             amil = mil;
86             ache = che;
87             milkilled = 0;
88             chekilled = 0;
89             mob = sect.sct_mobil;
90             while (amil != 0 && ache != 0 && mob > 1) {
91                 damil = amil;
92                 dache = ache;
93                 odds = (dache * 2.0 / (damil + dache));
94                 odds /= hap_fact(getnatp(sect.sct_own),
95                                  getnatp(sect.sct_oldown));
96                 mob = mob - 2;
97                 if (chance(odds)) {
98                     amil = amil - 1;
99                     milkilled = milkilled + 1;
100                 } else {
101                     ache = ache - 1;
102                     chekilled = chekilled + 1;
103                 }
104             }
105             if (mil - milkilled > 0) {
106                 sect.sct_mobil = sect.sct_mobil - chekilled - milkilled;
107                 sect.sct_item[I_MILIT] = mil - milkilled;
108                 if (ache == 0)
109                     sect.sct_che_target = 0;
110                 sect.sct_che = ache;
111                 putsect(&sect);
112                 pr("          Body count:  Military %d - Guerillas %d.\n",
113                    milkilled, chekilled);
114                 if (ache == 0) {
115                     pr("          Partisans cleared out of this sector for now.\n");
116                 } else {
117                     pr("          Partisans still active in this sector.\n");
118                 }
119             } else {
120                 if (opt_MOB_ACCESS) {
121                     sect.sct_mobil =
122                         -(etu_per_update / sect_mob_neg_factor);
123                 } else {
124                     sect.sct_mobil = 0;
125                 }
126                 sect.sct_loyal = sect.sct_loyal * 0.5;
127                 n_cheleft = roll0(4);
128                 /* 75% chance some che will get left */
129                 if (n_cheleft) {
130                     /* Ok, now leave anywhere from 16% to 25% of the che */
131                     n_cheleft = (ache / (n_cheleft + 3));
132                     ache -= n_cheleft;
133                     sect.sct_che = n_cheleft;
134                 } else {
135                     sect.sct_che = 0;
136                     sect.sct_che_target = 0;
137                 }
138                 sect.sct_item[I_MILIT] = ache;
139                 if (sect.sct_own == sect.sct_oldown)
140                     sect.sct_oldown = 0;
141                 sect.sct_own = sect.sct_oldown;
142                 sect.sct_off = 1;       /* Turn the sector off */
143                 putsect(&sect);
144                 pr("          Partisans take over the sector.  You blew it.\n");
145                 wu(0, sect.sct_oldown,
146                    "Sector %s regained from partisan activity.\n",
147                    xyas(nstr.x, nstr.y, sect.sct_oldown));
148             }
149         } else {
150             pr("          Body count:  Military 0 - Guerillas 0.\n");
151             pr("          Partisans cleared out of this sector for now.\n");
152         }
153     }
154     pr("%d sector%s\n", nsect, splur(nsect));
155     return RET_OK;
156 }