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