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