]> git.pond.sub.org Git - empserver/blob - src/lib/commands/demo.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / commands / demo.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  *  demo.c: De-mobilize "n" military in a given sector
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  */
33
34 #include "misc.h"
35 #include "player.h"
36 #include "xy.h"
37 #include "deity.h"
38 #include "var.h"
39 #include "sect.h"
40 #include "nat.h"
41 #include "nsc.h"
42 #include "file.h"
43 #include "commands.h"
44
45 /*
46  * format: demo <SECTS> number
47  *
48  */
49
50 long do_demo(struct natstr *natp, struct nstr_sect nstr, int number,
51              s_char *p, int for_real);
52
53 int
54 demo(void)
55 {
56     struct natstr *natp;
57     long cash;
58     long cost;
59     int number;
60     s_char *p;
61     s_char buf[1024];
62     struct nstr_sect nstr;
63
64     natp = getnatp(player->cnum);
65     cash = natp->nat_money;
66     if (!snxtsct(&nstr, player->argp[1]))
67         return RET_SYN;
68     if ((p =
69          getstarg(player->argp[2], "Number to de-mobilize : ", buf)) == 0)
70         return RET_SYN;
71     number = atoi(p);
72     if (!
73         (p =
74          getstarg(player->argp[3],
75                   "New civilians on active reserve? (y/n) ", buf)))
76         return RET_SYN;
77     if (*p != 'y' && *p != 'n')
78         return RET_SYN;
79     cost = do_demo(natp, nstr, number, p, 0);
80     if (chkmoney(cost, cash, player->argp[4]))
81         return RET_SYN;
82     return (int)do_demo(natp, nstr, number, p, 1);
83 }
84
85 long
86 do_demo(struct natstr *natp, struct nstr_sect nstr, int number, s_char *p,
87         int for_real)
88 {
89     struct sctstr sect;
90     int mil_demob;
91     int mil;
92     int civ;
93     int deltamil;
94     int reserves;
95     long cost = 0;
96
97     mil_demob = 0;
98     reserves = 0;
99     while (nxtsct(&nstr, &sect)) {
100         if (!player->owner || sect.sct_effic < 60)
101             continue;
102         if ((mil = getvar(V_MILIT, (s_char *)&sect, EF_SECTOR)) == 0)
103             continue;
104         if (sect.sct_own != sect.sct_oldown)
105             continue;
106         civ = getvar(V_CIVIL, (s_char *)&sect, EF_SECTOR);
107         if (number < 0) {
108             if ((deltamil = mil + number) <= 0)
109                 continue;
110         } else if ((deltamil = min(mil, number)) <= 0)
111             continue;
112         civ += deltamil;
113         mil -= deltamil;
114         mil_demob += deltamil;
115         if (!for_real) {
116             cost += deltamil * 5;
117             continue;
118         }
119         player->dolcost += deltamil * 5;
120         pr("%d demobilized in %s (%d mil left)\n",
121            deltamil, xyas(sect.sct_x, sect.sct_y, player->cnum), mil);
122         if (*p == 'y')
123             reserves += deltamil;
124         putvar(V_MILIT, mil, (s_char *)&sect, EF_SECTOR);
125         putvar(V_CIVIL, civ, (s_char *)&sect, EF_SECTOR);
126         putsect(&sect);
127     }
128     if (!for_real)
129         return cost;
130     if (!mil_demob) {
131         pr("No eligible sectors/military for demobilization\n");
132         return (long)RET_FAIL;
133     }
134     pr("Total new civilians : %d\n", mil_demob);
135     if (*p == 'y')
136         pr("Military reserve stands at %d (up %d)\n",
137            natp->nat_reserve + reserves, reserves);
138     if (reserves > 0) {
139         natp->nat_reserve += reserves;
140         putnat(natp);
141     }
142     return RET_OK;
143 }