]> git.pond.sub.org Git - empserver/blob - src/lib/commands/budg.c
Fix trailing whitespace
[empserver] / src / lib / commands / budg.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  *  budg.c: Calculate production levels, prioritize
29  *
30  *  Known contributors to this file:
31  *     Thomas Ruschak, 1992
32  *     Ville Virrankoski, 1995
33  *     Steve McClure, 1997-2000
34  *     Markus Armbruster, 2004-2006
35  */
36
37 #include <config.h>
38
39 #include <ctype.h>
40 #include "budg.h"
41 #include "commands.h"
42 #include "item.h"
43 #include "land.h"
44 #include "optlist.h"
45 #include "plane.h"
46 #include "product.h"
47 #include "ship.h"
48
49 static void calc_all(long (*p_sect)[2], int *taxes, int *Ncivs,
50                      int *Nuws, int *bars, int *Nbars, int *mil,
51                      int *ships, int *sbuild, int *nsbuild, int *smaint,
52                      int *units, int *lbuild, int *nlbuild, int *lmaint,
53                      int *planes, int *pbuild, int *npbuild, int *pmaint);
54 static char *dotsprintf(char *buf, char *format, int data);
55
56 int
57 budg(void)
58 {
59     int i;
60     long p_sect[SCT_TYPE_MAX+2][2];
61     int taxes, Ncivs, Nuws, bars, Nbars, mil;
62     int ships, sbuild, nsbuild, smaint;
63     int units, lbuild, nlbuild, lmaint;
64     int planes, pbuild, npbuild, pmaint;
65     int n, etu;
66     int income, expenses;
67     struct natstr *np;
68     char buf[1024];
69     char in[80];
70
71     etu = etu_per_update;
72
73     np = getnatp(player->cnum);
74
75     player->simulation = 1;
76     calc_all(p_sect,
77              &taxes, &Ncivs, &Nuws, &bars, &Nbars, &mil,
78              &ships, &sbuild, &nsbuild, &smaint,
79              &units, &lbuild, &nlbuild, &lmaint,
80              &planes, &pbuild, &npbuild, &pmaint);
81     player->simulation = 0;
82
83     income = taxes + bars;
84     expenses = 0;
85     pr("Sector Type\t\t\tProduction\t\t\t    Cost\n");
86     for (i = 0; i <= SCT_TYPE_MAX; i++) {
87         if (!p_sect[i][1] || i == SCT_CAPIT)
88             continue;
89         pr("%-17s\t\t", dchr[i].d_name);
90         if (i == SCT_ENLIST)
91             pr("%ld mil    \t", p_sect[i][0]);
92         else if (dchr[i].d_prd >= 0)
93             pr("%ld %-7s\t", p_sect[i][0], pchr[dchr[i].d_prd].p_sname);
94         else
95             pr("\t\t");
96         pr("\t\t%8ld\n", p_sect[i][1]);
97         expenses += p_sect[i][1];
98     }
99
100     if (sbuild) {
101         sprintf(buf, "%d ship%s", nsbuild, splur(nsbuild));
102         pr("Ship building\t\t\t%-16s\t\t%8d\n", buf, -sbuild);
103         expenses += -sbuild;
104     }
105
106     if (smaint) {
107         sprintf(buf, "%d ship%s", ships, splur(ships));
108         pr("Ship maintenance\t\t%-16s\t\t%8d\n", buf, -smaint);
109         expenses += -smaint;
110     }
111
112     if (pbuild) {
113         sprintf(buf, "%d plane%s", npbuild, splur(npbuild));
114         pr("Plane building\t\t\t%-16s\t\t%8d\n", buf, -pbuild);
115         expenses += -pbuild;
116     }
117
118     if (pmaint) {
119         sprintf(buf, "%d plane%s", planes, splur(planes));
120         pr("Plane maintenance\t\t%-16s\t\t%8d\n", buf, -pmaint);
121         expenses += -pmaint;
122     }
123     if (lbuild) {
124         sprintf(buf, "%d unit%s", nlbuild, splur(nlbuild));
125         pr("Unit building\t\t\t%-16s\t\t%8d\n", buf, -lbuild);
126         expenses += -lbuild;
127     }
128
129     if (lmaint) {
130         sprintf(buf, "%d unit%s", units, splur(units));
131         pr("Unit maintenance\t\t%-16s\t\t%8d\n", buf, -lmaint);
132         expenses += -lmaint;
133     }
134
135     if (p_sect[SCT_EFFIC][1]) {
136         pr("Sector building\t\t\t\t%8ld sct(s)\t\t%8ld\n",
137            p_sect[SCT_EFFIC][0], p_sect[SCT_EFFIC][1]);
138         expenses += p_sect[SCT_EFFIC][1];
139     }
140     if (mil) {
141         n = (mil - np->nat_reserve * money_res * etu) / (etu * money_mil);
142         sprintf(in, "%d mil, %d res", n, (int)np->nat_reserve);
143         pr("Military payroll\t\t%-32s%8d\n", in, -mil);
144         expenses -= mil;
145     }
146     if (p_sect[SCT_CAPIT][0]) {
147         pr("%c%s maintenance\t\t",
148            toupper(dchr[SCT_CAPIT].d_name[0]),
149            dchr[SCT_CAPIT].d_name + 1);
150         n = p_sect[SCT_CAPIT][0];
151         sprintf(in, "%d %s", n, dchr[SCT_CAPIT].d_name);
152         plurize(in, sizeof(in), n);
153         pr("%-32s%8ld\n", in, p_sect[SCT_CAPIT][1]);
154         expenses += p_sect[SCT_CAPIT][1];
155     }
156     pr("Total expenses%s\n", dotsprintf(buf, "%58d", expenses));
157     if (taxes) {
158         sprintf(in, "%d civ%s, %d uw%s",
159                 Ncivs, splur(Ncivs), Nuws, splur(Nuws));
160         pr("Income from taxes\t\t%-32s%+8d\n", in, taxes);
161     }
162     if (bars) {
163         sprintf(in, "%d bar%s", Nbars, splur(Nbars));
164         pr("Income from bars\t\t%-32s%+8d\n", in, bars);
165     }
166     pr("Total income%s\n", dotsprintf(buf, "%+60d", income));
167     pr("Balance forward\t\t\t\t\t\t      %10ld\n", np->nat_money);
168     pr("Estimated delta\t\t\t\t\t\t      %+10d\n", income - expenses);
169     pr("Estimated new treasury%s\n",
170        dotsprintf(buf, "%50d", np->nat_money + income - expenses));
171     if (np->nat_money + income - expenses < 0 && !player->god) {
172         pr("After processsing sectors, you will be broke!\n");
173         pr("Sectors will not produce, distribute, or deliver!\n\n");
174     }
175
176     return RET_OK;
177 }
178
179 static void
180 calc_all(long p_sect[][2],
181          int *taxes, int *Ncivs, int *Nuws, int *bars, int *Nbars, int *mil,
182          int *ships, int *sbuild, int *nsbuild, int *smaint,
183          int *units, int *lbuild, int *nlbuild, int *lmaint,
184          int *planes, int *pbuild, int *npbuild, int *pmaint)
185 {
186     struct natstr *np;
187     struct bp *bp;
188     long pop = 0;
189     int n, civ_tax, uw_tax, mil_pay;
190     struct sctstr *sp;
191     int etu = etu_per_update;
192
193     memset(p_sect, 0, sizeof(**p_sect) * (SCT_TYPE_MAX+2) * 2);
194     *taxes = *Ncivs = *Nuws = *bars = *Nbars = *mil = 0;
195     *ships = *sbuild = *nsbuild = *smaint = 0;
196     *units = *lbuild = *nlbuild = *lmaint = 0;
197     *planes = *pbuild = *npbuild = *pmaint = 0;
198
199     np = getnatp(player->cnum);
200     bp = bp_alloc();
201     for (n = 0; NULL != (sp = getsectid(n)); n++) {
202         bp_set_from_sect(bp, sp);
203         if (sp->sct_own == player->cnum) {
204             sp->sct_updated = 0;
205             tax(sp, np, etu, &pop, &civ_tax, &uw_tax, &mil_pay);
206             *Ncivs += sp->sct_item[I_CIVIL];
207             *Nuws += sp->sct_item[I_UW];
208             *taxes += civ_tax + uw_tax;
209             *mil += mil_pay;
210             if (sp->sct_type == SCT_BANK) {
211                 *bars += bank_income(sp, etu);
212                 *Nbars += sp->sct_item[I_BAR];
213             }
214         }
215     }
216     tpops[player->cnum] = pop;
217     *mil += (int)(np->nat_reserve * money_res * etu);
218
219     *mil += upd_slmilcosts(np->nat_cnum, etu);
220
221     /* Maintain ships */
222     sea_money[player->cnum] = 0;
223     *ships = prod_ship(etu, player->cnum, bp, 0);
224     *smaint = sea_money[player->cnum];
225
226     /* Maintain planes */
227     air_money[player->cnum] = 0;
228     *planes = prod_plane(etu, player->cnum, bp, 0);
229     *pmaint = air_money[player->cnum];
230
231     /* Maintain land units */
232     lnd_money[player->cnum] = 0;
233     *units = prod_land(etu, player->cnum, bp, 0);
234     *lmaint = lnd_money[player->cnum];
235
236     /* Produce */
237     produce_sect(player->cnum, etu, bp, p_sect);
238
239     /* Build ships */
240     sea_money[player->cnum] = 0;
241     *nsbuild = prod_ship(etu, player->cnum, bp, 1);
242     *sbuild = sea_money[player->cnum];
243     sea_money[player->cnum] = 0;
244
245     /* Build planes */
246     air_money[player->cnum] = 0;
247     *npbuild = prod_plane(etu, player->cnum, bp, 1);
248     *pbuild = air_money[player->cnum];
249     air_money[player->cnum] = 0;
250
251     /* Build land units */
252     lnd_money[player->cnum] = 0;
253     *nlbuild = prod_land(etu, player->cnum, bp, 1);
254     *lbuild = lnd_money[player->cnum];
255     lnd_money[player->cnum] = 0;
256
257     free(bp);
258 }
259
260 static char *
261 dotsprintf(char *buf, char *format, int data)
262 {
263     sprintf(buf, format, data);
264     return memset(buf, '.', strspn(buf, " "));
265 }