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