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