]> git.pond.sub.org Git - empserver/blob - src/lib/commands/anti.c
Move declarations for chance.c to new chance.h
[empserver] / src / lib / commands / anti.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2013, 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  */
33
34 #include <config.h>
35
36 #include "chance.h"
37 #include "commands.h"
38 #include "item.h"
39 #include "optlist.h"
40
41 /*
42  * format: anti <SECT>
43  */
44 int
45 anti(void)
46 {
47     struct sctstr sect;
48     int nsect;
49     struct nstr_sect nstr;
50     int mil, che, target;
51     int avail_mil;
52     int amil, ache;
53     int milkilled, chekilled;
54     double odds, damil, dache;
55     int mob;
56     int n_cheleft;
57
58     if (!snxtsct(&nstr, player->argp[1]))
59         return RET_SYN;
60     prdate();
61     nsect = 0;
62     while (nxtsct(&nstr, &sect)) {
63         if (!player->owner)
64             continue;
65         if (nsect++ == 0) {
66             pr("  sect    subversion activity report\n");
67             pr("  ----    --------------------------\n");
68         }
69         mil = sect.sct_item[I_MILIT];
70         che = sect.sct_che;
71         target = sect.sct_che_target;
72         avail_mil = sect.sct_mobil / 2;
73         if (mil <= avail_mil)
74             avail_mil = mil;
75         prxy("%4d,%-4d ", sect.sct_x, sect.sct_y);
76         if (avail_mil <= 0) {
77             pr("No available mil or mob in sector.\n");
78             continue;
79         }
80         pr("Sector mobility/troop strength will allow %d troops to engage.\n",
81            avail_mil);
82
83         if (target == player->cnum) {
84             amil = mil;
85             ache = che;
86             milkilled = 0;
87             chekilled = 0;
88             mob = sect.sct_mobil;
89             while (amil != 0 && ache != 0 && mob > 1) {
90                 damil = amil;
91                 dache = ache;
92                 odds = (dache * 2.0 / (damil + dache));
93                 odds /= hap_fact(getnatp(sect.sct_own),
94                                  getnatp(sect.sct_oldown));
95                 mob = mob - 2;
96                 if (chance(odds)) {
97                     amil = amil - 1;
98                     milkilled = milkilled + 1;
99                 } else {
100                     ache = ache - 1;
101                     chekilled = chekilled + 1;
102                 }
103             }
104             if (mil - milkilled > 0) {
105                 sect.sct_mobil = sect.sct_mobil - chekilled - milkilled;
106                 sect.sct_item[I_MILIT] = mil - milkilled;
107                 if (ache == 0)
108                     sect.sct_che_target = 0;
109                 sect.sct_che = ache;
110                 putsect(&sect);
111                 pr("          Body count:  Military %d - Guerillas %d.\n",
112                    milkilled, chekilled);
113                 if (ache == 0) {
114                     pr("          Partisans cleared out of this sector for now.\n");
115                 } else {
116                     pr("          Partisans still active in this sector.\n");
117                 }
118             } else {
119                 if (opt_MOB_ACCESS) {
120                     sect.sct_mobil =
121                         -(etu_per_update / sect_mob_neg_factor);
122                 } else {
123                     sect.sct_mobil = 0;
124                 }
125                 sect.sct_loyal = sect.sct_loyal * 0.5;
126                 n_cheleft = (random() % 4);
127                 /* 75% chance some che will get left */
128                 if (n_cheleft) {
129                     /* Ok, now leave anywhere from 16% to 25% of the che */
130                     n_cheleft = (ache / (n_cheleft + 3));
131                     ache -= n_cheleft;
132                     sect.sct_che = n_cheleft;
133                 } else {
134                     sect.sct_che = 0;
135                     sect.sct_che_target = 0;
136                 }
137                 sect.sct_item[I_MILIT] = ache;
138                 if (sect.sct_own == sect.sct_oldown)
139                     sect.sct_oldown = 0;
140                 sect.sct_own = sect.sct_oldown;
141                 sect.sct_off = 1;       /* Turn the sector off */
142                 putsect(&sect);
143                 pr("          Partisans take over the sector.  You blew it.\n");
144                 wu(0, sect.sct_oldown,
145                    "Sector %s regained from partisan activity.\n",
146                    xyas(nstr.x, nstr.y, sect.sct_oldown));
147             }
148         } else {
149             pr("          Body count:  Military 0 - Guerillas 0.\n");
150             pr("          Partisans cleared out of this sector for now.\n");
151         }
152     }
153     pr("%d sector%s\n", nsect, splur(nsect));
154     return RET_OK;
155 }