]> git.pond.sub.org Git - empserver/blob - src/lib/commands/budg.c
Make sector maintenance cost configurable
[empserver] / src / lib / commands / budg.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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_BUDG_MAX+1][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])
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 (p_sect[SCT_MAINT][0]) {
141         sprintf(buf, "%ld sector%s",
142                 p_sect[SCT_MAINT][0], splur(p_sect[SCT_MAINT][0]));
143         pr("Sector maintenance\t\t%-16s\t\t%8ld\n",
144            buf, p_sect[SCT_MAINT][1]);
145         expenses += p_sect[SCT_MAINT][1];
146     }
147     if (mil) {
148         n = (mil - np->nat_reserve * money_res * etu) / (etu * money_mil);
149         sprintf(in, "%d mil, %d res", n, (int)np->nat_reserve);
150         pr("Military payroll\t\t%-32s%8d\n", in, -mil);
151         expenses -= mil;
152     }
153     pr("Total expenses%s\n", dotsprintf(buf, "%58d", expenses));
154     if (taxes) {
155         sprintf(in, "%d civ%s, %d uw%s",
156                 Ncivs, splur(Ncivs), Nuws, splur(Nuws));
157         pr("Income from taxes\t\t%-32s%+8d\n", in, taxes);
158     }
159     if (bars) {
160         sprintf(in, "%d bar%s", Nbars, splur(Nbars));
161         pr("Income from bars\t\t%-32s%+8d\n", in, bars);
162     }
163     pr("Total income%s\n", dotsprintf(buf, "%+60d", income));
164     pr("Balance forward\t\t\t\t\t\t      %10ld\n", np->nat_money);
165     pr("Estimated delta\t\t\t\t\t\t      %+10d\n", income - expenses);
166     pr("Estimated new treasury%s\n",
167        dotsprintf(buf, "%50d", np->nat_money + income - expenses));
168     if (np->nat_money + income - expenses < 0 && !player->god) {
169         pr("After processsing sectors, you will be broke!\n");
170         pr("Sectors will not produce, distribute, or deliver!\n\n");
171     }
172
173     return RET_OK;
174 }
175
176 static void
177 calc_all(long p_sect[][2],
178          int *taxes, int *Ncivs, int *Nuws, int *bars, int *Nbars, int *mil,
179          int *ships, int *sbuild, int *nsbuild, int *smaint,
180          int *units, int *lbuild, int *nlbuild, int *lmaint,
181          int *planes, int *pbuild, int *npbuild, int *pmaint)
182 {
183     struct natstr *np;
184     struct bp *bp;
185     long pop = 0;
186     int n, civ_tax, uw_tax, mil_pay;
187     struct sctstr *sp;
188     int etu = etu_per_update;
189
190     memset(p_sect, 0, sizeof(**p_sect) * (SCT_BUDG_MAX+1) * 2);
191     *taxes = *Ncivs = *Nuws = *bars = *Nbars = *mil = 0;
192     *ships = *sbuild = *nsbuild = *smaint = 0;
193     *units = *lbuild = *nlbuild = *lmaint = 0;
194     *planes = *pbuild = *npbuild = *pmaint = 0;
195
196     np = getnatp(player->cnum);
197     bp = bp_alloc();
198     for (n = 0; NULL != (sp = getsectid(n)); n++) {
199         bp_set_from_sect(bp, sp);
200         if (sp->sct_own == player->cnum) {
201             sp->sct_updated = 0;
202             tax(sp, np, etu, &pop, &civ_tax, &uw_tax, &mil_pay);
203             *Ncivs += sp->sct_item[I_CIVIL];
204             *Nuws += sp->sct_item[I_UW];
205             *taxes += civ_tax + uw_tax;
206             *mil += mil_pay;
207             if (sp->sct_type == SCT_BANK) {
208                 *bars += bank_income(sp, etu);
209                 *Nbars += sp->sct_item[I_BAR];
210             }
211         }
212     }
213     tpops[player->cnum] = pop;
214     *mil += (int)(np->nat_reserve * money_res * etu);
215
216     *mil += upd_slmilcosts(np->nat_cnum, etu);
217
218     /* Maintain ships */
219     sea_money[player->cnum] = 0;
220     *ships = prod_ship(etu, player->cnum, bp, 0);
221     *smaint = sea_money[player->cnum];
222
223     /* Maintain planes */
224     air_money[player->cnum] = 0;
225     *planes = prod_plane(etu, player->cnum, bp, 0);
226     *pmaint = air_money[player->cnum];
227
228     /* Maintain land units */
229     lnd_money[player->cnum] = 0;
230     *units = prod_land(etu, player->cnum, bp, 0);
231     *lmaint = lnd_money[player->cnum];
232
233     /* Produce */
234     produce_sect(player->cnum, etu, bp, p_sect);
235
236     /* Build ships */
237     sea_money[player->cnum] = 0;
238     *nsbuild = prod_ship(etu, player->cnum, bp, 1);
239     *sbuild = sea_money[player->cnum];
240     sea_money[player->cnum] = 0;
241
242     /* Build planes */
243     air_money[player->cnum] = 0;
244     *npbuild = prod_plane(etu, player->cnum, bp, 1);
245     *pbuild = air_money[player->cnum];
246     air_money[player->cnum] = 0;
247
248     /* Build land units */
249     lnd_money[player->cnum] = 0;
250     *nlbuild = prod_land(etu, player->cnum, bp, 1);
251     *lbuild = lnd_money[player->cnum];
252     lnd_money[player->cnum] = 0;
253
254     free(bp);
255 }
256
257 static char *
258 dotsprintf(char *buf, char *format, int data)
259 {
260     sprintf(buf, format, data);
261     return memset(buf, '.', strspn(buf, " "));
262 }