]> git.pond.sub.org Git - empserver/blob - src/lib/commands/demo.c
c170e22bce3802f839728a5edcc7cdde94b7a7d0
[empserver] / src / lib / commands / demo.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 <config.h>
35
36 #include "misc.h"
37 #include "player.h"
38 #include "xy.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 static 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 = getstarg(player->argp[2], "Number to de-mobilize : ", buf)))
69         return RET_SYN;
70     number = atoi(p);
71     if (!(p = getstarg(player->argp[3],
72                        "New civilians on active reserve? (y/n) ", buf)))
73         return RET_SYN;
74     if (*p != 'y' && *p != 'n')
75         return RET_SYN;
76     cost = do_demo(natp, nstr, number, p, 0);
77     if (chkmoney(cost, cash, player->argp[4]))
78         return RET_SYN;
79     return (int)do_demo(natp, nstr, number, p, 1);
80 }
81
82 static long
83 do_demo(struct natstr *natp, struct nstr_sect nstr, int number, s_char *p,
84         int for_real)
85 {
86     struct sctstr sect;
87     int mil_demob;
88     int mil;
89     int civ;
90     int deltamil;
91     int reserves;
92     long cost = 0;
93
94     mil_demob = 0;
95     reserves = 0;
96     while (nxtsct(&nstr, &sect)) {
97         if (!player->owner || sect.sct_effic < 60)
98             continue;
99         if ((mil = sect.sct_item[I_MILIT]) == 0)
100             continue;
101         if (sect.sct_own != sect.sct_oldown)
102             continue;
103         civ = sect.sct_item[I_CIVIL];
104         deltamil = number < 0 ? mil + number : MIN(mil, number);
105         if (deltamil <= 0)
106             continue;
107         if (deltamil > ITEM_MAX - civ)
108             deltamil = ITEM_MAX - civ;
109         civ += deltamil;
110         mil -= deltamil;
111         mil_demob += deltamil;
112         if (!for_real) {
113             cost += deltamil * 5;
114             continue;
115         }
116         player->dolcost += deltamil * 5;
117         pr("%d demobilized in %s (%d mil left)\n",
118            deltamil, xyas(sect.sct_x, sect.sct_y, player->cnum), mil);
119         if (*p == 'y')
120             reserves += deltamil;
121         sect.sct_item[I_MILIT] = mil;
122         sect.sct_item[I_CIVIL] = civ;
123         putsect(&sect);
124     }
125     if (!for_real)
126         return cost;
127     if (!mil_demob) {
128         pr("No eligible sectors/military for demobilization\n");
129         return (long)RET_FAIL;
130     }
131     pr("Total new civilians : %d\n", mil_demob);
132     if (*p == 'y')
133         pr("Military reserve stands at %ld (up %d)\n",
134            natp->nat_reserve + reserves, reserves);
135     if (reserves > 0) {
136         natp->nat_reserve += reserves;
137         putnat(natp);
138     }
139     return RET_OK;
140 }