]> git.pond.sub.org Git - empserver/blob - src/lib/commands/demo.c
Do not include var.h where no longer needed. Clean up register keywords in these...
[empserver] / src / lib / commands / demo.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2004, 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 "sect.h"
38 #include "nat.h"
39 #include "nsc.h"
40 #include "file.h"
41 #include "commands.h"
42
43 /*
44  * format: demo <SECTS> number
45  *
46  */
47
48 static long do_demo(struct natstr *natp, struct nstr_sect nstr, int number,
49                     s_char *p, int for_real);
50
51 int
52 demo(void)
53 {
54     struct natstr *natp;
55     long cash;
56     long cost;
57     int number;
58     s_char *p;
59     s_char buf[1024];
60     struct nstr_sect nstr;
61
62     natp = getnatp(player->cnum);
63     cash = natp->nat_money;
64     if (!snxtsct(&nstr, player->argp[1]))
65         return RET_SYN;
66     if (!(p = getstarg(player->argp[2], "Number to de-mobilize : ", buf)))
67         return RET_SYN;
68     number = atoi(p);
69     if (!(p = getstarg(player->argp[3],
70                        "New civilians on active reserve? (y/n) ", buf)))
71         return RET_SYN;
72     if (*p != 'y' && *p != 'n')
73         return RET_SYN;
74     cost = do_demo(natp, nstr, number, p, 0);
75     if (chkmoney(cost, cash, player->argp[4]))
76         return RET_SYN;
77     return (int)do_demo(natp, nstr, number, p, 1);
78 }
79
80 static long
81 do_demo(struct natstr *natp, struct nstr_sect nstr, int number, s_char *p,
82         int for_real)
83 {
84     struct sctstr sect;
85     int mil_demob;
86     int mil;
87     int civ;
88     int deltamil;
89     int reserves;
90     long cost = 0;
91
92     mil_demob = 0;
93     reserves = 0;
94     while (nxtsct(&nstr, &sect)) {
95         if (!player->owner || sect.sct_effic < 60)
96             continue;
97         if ((mil = sect.sct_item[I_MILIT]) == 0)
98             continue;
99         if (sect.sct_own != sect.sct_oldown)
100             continue;
101         civ = sect.sct_item[I_CIVIL];
102         deltamil = number < 0 ? mil + number : min(mil, number);
103         if (deltamil <= 0)
104             continue;
105         if (deltamil > ITEM_MAX - civ)
106             deltamil = ITEM_MAX - civ;
107         civ += deltamil;
108         mil -= deltamil;
109         mil_demob += deltamil;
110         if (!for_real) {
111             cost += deltamil * 5;
112             continue;
113         }
114         player->dolcost += deltamil * 5;
115         pr("%d demobilized in %s (%d mil left)\n",
116            deltamil, xyas(sect.sct_x, sect.sct_y, player->cnum), mil);
117         if (*p == 'y')
118             reserves += deltamil;
119         sect.sct_item[I_MILIT] = mil;
120         sect.sct_item[I_CIVIL] = civ;
121         putsect(&sect);
122     }
123     if (!for_real)
124         return cost;
125     if (!mil_demob) {
126         pr("No eligible sectors/military for demobilization\n");
127         return (long)RET_FAIL;
128     }
129     pr("Total new civilians : %d\n", mil_demob);
130     if (*p == 'y')
131         pr("Military reserve stands at %ld (up %d)\n",
132            natp->nat_reserve + reserves, reserves);
133     if (reserves > 0) {
134         natp->nat_reserve += reserves;
135         putnat(natp);
136     }
137     return RET_OK;
138 }