]> git.pond.sub.org Git - empserver/blob - src/lib/commands/budg.c
Add some missing declarations to headers. Remove some redundant
[empserver] / src / lib / commands / budg.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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  */
35
36 #include <string.h>
37 #include "misc.h"
38 #include "player.h"
39 #include "var.h"
40 #include "xy.h"
41 #include "nsc.h"
42 #include "sect.h"
43 #include "product.h"
44 #include "nat.h"
45 #include "item.h"
46 #include "file.h"
47 #include "deity.h"
48 #include "ship.h"
49 #include "land.h"
50 #include "plane.h"
51 #include "optlist.h"
52 #include "budg.h"
53 #include "commands.h"
54
55 #ifndef MIN
56 #define MIN(x,y)        ((x) > (y) ? (y) : (x))
57 #endif
58
59 static void calc_all(long int (*p_sect)[2], int *taxes, int *Ncivs,
60                      int *Nuws, int *bars, int *Nbars, int *mil,
61                      int *ships, int *sbuild, int *nsbuild, int *smaint,
62                      int *units, int *lbuild, int *nlbuild, int *lmaint,
63                      int *planes, int *pbuild, int *npbuild, int *pmaint);
64 static s_char *dotsprintf(s_char *buf, s_char *format, int data);
65 static int goodsect(char c);
66 static void prexpense(long int cash, int *expensesp, s_char priority, int amount);
67
68 int
69 budg(void)
70 {
71     s_char stype = 0, *pq;
72     int priority, x;
73     long p_sect[SCT_MAXDEF + 1][2];
74     int taxes = 0, bars = 0, mil = 0;
75     int Ncivs = 0, Nuws = 0, Nbars = 0;
76     int n, etu;
77     int income = 0, expenses = 0;
78     int sbuild = 0, smaint = 0, pbuild = 0, pmaint = 0, ships = 0, planes =
79         0;
80     int nsbuild = 0, npbuild = 0, which = 0;
81     int lbuild = 0, lmaint = 0, units = 0, nlbuild = 0;
82     struct natstr *np;
83     s_char buf[1024];
84     s_char in[80];
85
86     memset(p_sect, 0, sizeof(p_sect));
87     etu = etu_per_update;
88
89     np = getnatp(player->cnum);
90     if (player->argp[1] != (s_char *)0) {
91         if (goodsect(player->argp[1][0]))
92             stype = player->argp[1][0];
93         else
94             switch (player->argp[1][0]) {
95             case 'P':
96             case 'S':
97             case 'L':
98             case 'A':
99             case 'M':
100             case 'N':
101             case 'C':
102                 stype = player->argp[1][0];
103                 break;
104             default:
105                 return RET_SYN;
106             }
107     }
108     if ((stype != 0) && (stype != 'C')) {
109         pq = getstarg(player->argp[2], "Priority? ", buf);
110     } else {
111         pq = (s_char *)0;
112     }
113     if (pq != (s_char *)0) {
114         if (isdigit(*pq)) {
115             priority = (atoi(pq) < 0 ? -1 * atoi(pq) : atoi(pq));
116             if (priority >= SCT_MAXDEF + 8) {
117                 pr("Priorities must be less than %d!\n", SCT_MAXDEF + 8);
118                 return RET_FAIL;
119             }
120             for (x = 0; x < SCT_MAXDEF + 8; x++)
121                 if (priority && (np->nat_priorities[x] == priority)) {
122                     pr("Priorities must be unique!\n");
123                     return RET_FAIL;
124                 }
125         } else if (*pq == '~')
126             priority = -1;
127         else
128             return RET_SYN;
129     }
130     if ((stype) && !player->god) {
131         if (!isupper(stype)) {
132             which = 0;
133             while ((which < SCT_MAXDEF + 2) &&
134                    (stype != dchr[which].d_mnem))
135                 which++;
136             if (which == SCT_MAXDEF + 2)
137                 return RET_SYN;
138         } else {
139             switch (stype) {
140             case 'P':
141                 which = PRI_PBUILD;
142                 break;
143             case 'S':
144                 which = PRI_SBUILD;
145                 break;
146             case 'L':
147                 which = PRI_LBUILD;
148                 break;
149             case 'A':
150                 which = PRI_LMAINT;
151                 break;
152             case 'M':
153                 which = PRI_SMAINT;
154                 break;
155             case 'N':
156                 which = PRI_PMAINT;
157                 break;
158             case 'C':
159                 which = (-1);
160                 break;
161             default:
162                 return RET_SYN;
163             }
164         }
165         if (which == -1) {
166             for (x = 0; x < SCT_MAXDEF + 8; x++) {
167                 np->nat_priorities[x] = -1;
168             }
169         } else {
170             np->nat_priorities[which] = priority;
171         }
172
173     }
174     putnat(np);
175
176     player->simulation = 1;
177     calc_all(p_sect, &taxes, &Ncivs, &Nuws, &bars, &Nbars, &mil, &ships,
178              &sbuild, &nsbuild, &smaint, &units, &lbuild, &nlbuild,
179              &lmaint, &planes, &pbuild, &npbuild, &pmaint);
180
181     income = taxes + bars;
182     pr("Sector Type\t\tAbbr\tProduction\tPriority\t    Cost\n");
183     for (x = 0; x < SCT_MAXDEF + 1; x++) {
184         if (!p_sect[x][1] && np->nat_priorities[x] == -1)
185             continue;
186         if (!pchr[dchr[x].d_prd].p_cost &&
187             np->nat_priorities[x] == -1 && x != SCT_ENLIST) {
188             continue;
189         }
190
191         pr("%-17s\t%c\t", dchr[x].d_name, dchr[x].d_mnem);
192         if (x == SCT_ENLIST)
193             pr("%d mil    \t", p_sect[x][0]);
194         else if (pchr[dchr[x].d_prd].p_cost != 0)
195             pr("%d %-7s\t", p_sect[x][0], pchr[dchr[x].d_prd].p_sname);
196         else
197             pr("\t\t");
198
199         if (np->nat_priorities[x] != -1) {
200             pr("%d", np->nat_priorities[x]);
201         }
202         pr("\t");
203         pr("\t");
204         if (np->nat_priorities[x] != 0) {
205             if ((np->nat_money + income - expenses) > 0) {
206                 pr("%8d", p_sect[x][1]);
207                 expenses += p_sect[x][1];
208             } else
209                 pr("[%7d]", p_sect[x][1]);
210         } else {
211             if ((np->nat_money + income - expenses) > 0)
212                 pr("(%7d)", p_sect[x][1]);
213             else
214                 pr("[(%7d)]", p_sect[x][1]);
215         }
216
217         pr("\n");
218     }
219
220     if (lbuild) {
221         sprintf(buf, "%d unit%s", nlbuild, splur(nlbuild));
222         pr("Unit building\t\tL\t%-16s", buf);
223         if (np->nat_priorities[PRI_LBUILD] != -1)
224             pr("%d", np->nat_priorities[PRI_LBUILD]);
225         prexpense(np->nat_money + income, &expenses,
226                   np->nat_priorities[PRI_LBUILD], -1 * lbuild);
227     }
228
229     if (lmaint) {
230         sprintf(buf, "%d unit%s", units, splur(units));
231         pr("Unit maintenance\tA\t%-16s", buf);
232         if (np->nat_priorities[PRI_LMAINT] != -1)
233             pr("%d", np->nat_priorities[PRI_LMAINT]);
234         prexpense(np->nat_money + income, &expenses,
235                   np->nat_priorities[PRI_LMAINT], -1 * lmaint);
236     }
237
238     if (sbuild) {
239         sprintf(buf, "%d ship%s", nsbuild, splur(nsbuild));
240         pr("Ship building\t\tS\t%-16s", buf);
241         if (np->nat_priorities[PRI_SBUILD] != -1)
242             pr("%d", np->nat_priorities[PRI_SBUILD]);
243         prexpense(np->nat_money + income, &expenses,
244                   np->nat_priorities[PRI_SBUILD], -1 * sbuild);
245     }
246
247     if (smaint) {
248         sprintf(buf, "%d ship%s", ships, splur(ships));
249         pr("Ship maintenance\tM\t%-16s", buf);
250         if (np->nat_priorities[PRI_SMAINT] != -1)
251             pr("%d", np->nat_priorities[PRI_SMAINT]);
252         prexpense(np->nat_money + income, &expenses,
253                   np->nat_priorities[PRI_SMAINT], -1 * smaint);
254     }
255
256     if (pbuild) {
257         sprintf(buf, "%d plane%s", npbuild, splur(npbuild));
258         pr("Plane building\t\tP\t%-16s", buf);
259         if (np->nat_priorities[PRI_PBUILD] != -1)
260             pr("%d", np->nat_priorities[PRI_PBUILD]);
261         prexpense(np->nat_money + income, &expenses,
262                   np->nat_priorities[PRI_PBUILD], -1 * pbuild);
263     }
264
265     if (pmaint) {
266         sprintf(buf, "%d plane%s", planes, splur(planes));
267         pr("Plane maintenance\tN\t%-16s", buf);
268         if (np->nat_priorities[PRI_PMAINT] != -1)
269             pr("%d", np->nat_priorities[PRI_PMAINT]);
270         prexpense(np->nat_money + income, &expenses,
271                   np->nat_priorities[PRI_PMAINT], -1 * pmaint);
272     }
273     if (p_sect[SCT_EFFIC][1]) {
274         pr("Sector building\t\t\t\t%8d sct(s)\t\t%8d\n",
275            p_sect[SCT_EFFIC][0], p_sect[SCT_EFFIC][1]);
276         expenses += p_sect[SCT_EFFIC][1];
277     }
278     if (mil) {
279
280         n = (mil - np->nat_reserve * money_res * etu) / (etu * money_mil);
281         sprintf(in, "%d mil, %d res", n, (int)np->nat_reserve);
282         pr("Military payroll\t\t%-32s%8d\n", in, -mil);
283         expenses -= mil;
284     }
285     if (p_sect[SCT_CAPIT][0]) {
286         n = p_sect[SCT_CAPIT][0];
287         sprintf(in, "%d %s",
288                 n,
289                 n ==
290                 1 ? opt_BIG_CITY ? "city" : "capital" : opt_BIG_CITY ?
291                 "cities" : "capitals");
292         pr("%s maintenance\t\t%-32s%8d\n",
293            opt_BIG_CITY ? "City" : "Capital", in, p_sect[SCT_CAPIT][1]);
294         expenses += p_sect[SCT_CAPIT][1];
295     }
296     pr("Total expenses%s\n", dotsprintf(buf, "%58d", expenses));
297     if (taxes) {
298         sprintf(in, "%d civ%s, %d uw%s",
299                 Ncivs, splur(Ncivs), Nuws, splur(Nuws));
300         pr("Income from taxes\t\t%-32s%+8d\n", in, taxes);
301     }
302     if (bars) {
303         sprintf(in, "%d bar%s", Nbars, splur(Nbars));
304         pr("Income from bars\t\t%-32s%+8d\n", in, bars);
305     }
306     pr("Total income%s\n", dotsprintf(buf, "%+60d", income));
307     pr("Balance forward\t\t\t\t\t\t      %10d\n", np->nat_money);
308     pr("Estimated delta\t\t\t\t\t\t      %+10d\n", income - expenses);
309     pr("Estimated new treasury%s\n",
310        dotsprintf(buf, "%50d", np->nat_money + income - expenses));
311     if (((np->nat_money + income - expenses) < 0) && !player->god) {
312         pr("After processsing sectors, you will be broke!\n");
313         pr("Sectors will not produce, distribute, or deliver!\n\n");
314     }
315
316     player->simulation = 0;
317     return RET_OK;
318 }
319
320 static void
321 calc_all(long int (*p_sect)[2], int *taxes, int *Ncivs, int *Nuws,
322          int *bars, int *Nbars, int *mil, int *ships, int *sbuild,
323          int *nsbuild, int *smaint, int *units, int *lbuild, int *nlbuild,
324          int *lmaint, int *planes, int *pbuild, int *npbuild, int *pmaint)
325 {
326     register int y, z;
327     struct natstr *np;
328     int sm = 0, sb = 0, pm = 0, pb = 0, lm = 0, lb = 0;
329     int *bp;
330     long pop = 0;
331     int n, civ_tax, uw_tax, mil_pay;
332     struct sctstr *sp;
333     int etu = etu_per_update;
334     int vec[I_MAX + 1];
335     long tmp_money;
336
337     lnd_money[player->cnum] = sea_money[player->cnum] = 0;
338     air_money[player->cnum] = 0;
339     mil_dbl_pay = 0;
340     *taxes = 0;
341     *Ncivs = 0;
342     np = getnatp(player->cnum);
343     bp = (int *)calloc(WORLD_X * WORLD_Y * 8, sizeof(int));
344     for (n = 0; NULL != (sp = getsectid(n)); n++) {
345         fill_update_array(bp, sp);
346         if (sp->sct_own == player->cnum) {
347             sp->sct_updated = 0;
348             tax(sp, np, etu, &pop, &civ_tax, &uw_tax, &mil_pay);
349             getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
350             *Ncivs += vec[I_CIVIL];
351             *Nuws += vec[I_UW];
352             *taxes += civ_tax + uw_tax;
353             *mil += mil_pay;
354             if (sp->sct_type == SCT_BANK) {
355                 *bars += bank_income(sp, etu);
356                 *Nbars += vec[I_BAR];
357             }
358         }
359     }
360     tpops[player->cnum] = pop;
361     *mil += (int)(np->nat_reserve * money_res * etu);
362
363     *mil += (int)upd_slmilcosts(np->nat_cnum, etu);
364
365     for (y = 1; y < SCT_MAXDEF + 8; y++) {
366         for (z = 0; z < SCT_MAXDEF + 8; z++)
367             if (np->nat_priorities[z] == y)
368                 switch (z) {
369                 case PRI_SMAINT:
370                     tmp_money = lnd_money[player->cnum];
371                     *ships = prod_ship(etu, player->cnum, bp, 0);
372                     *smaint = lnd_money[player->cnum] - tmp_money;
373                     sm = 1;
374                     break;
375                 case PRI_SBUILD:
376                     tmp_money = sea_money[player->cnum];
377                     *nsbuild = prod_ship(etu, player->cnum, bp, 1);
378                     *sbuild = sea_money[player->cnum] - tmp_money;
379                     sb = 1;
380                     break;
381                 case PRI_LMAINT:
382                     tmp_money = lnd_money[player->cnum];
383                     *units = prod_land(etu, player->cnum, bp, 0);
384                     *lmaint = lnd_money[player->cnum] - tmp_money;
385                     lm = 1;
386                     break;
387                 case PRI_LBUILD:
388                     tmp_money = lnd_money[player->cnum];
389                     *nlbuild = prod_land(etu, player->cnum, bp, 1);
390                     *lbuild = lnd_money[player->cnum] - tmp_money;
391                     lb = 1;
392                     break;
393                 case PRI_PMAINT:
394                     tmp_money = air_money[player->cnum];
395                     *planes = prod_plane(etu, player->cnum, bp, 0);
396                     *pmaint = air_money[player->cnum] - tmp_money;
397                     pm = 1;
398                     break;
399                 case PRI_PBUILD:
400                     tmp_money = air_money[player->cnum];
401                     *npbuild = prod_plane(etu, player->cnum, bp, 1);
402                     *pbuild = air_money[player->cnum] - tmp_money;
403                     pb = 1;
404                     break;
405                 default:
406                     produce_sect(player->cnum, etu, bp, p_sect, z);
407                     break;
408                 }
409     }
410     /* 0 is maintain, 1 is build */
411     if (!sm) {
412         tmp_money = sea_money[player->cnum];
413         *ships = prod_ship(etu, player->cnum, bp, 0);
414         *smaint = sea_money[player->cnum] - tmp_money;
415     }
416     if (!sb) {
417         tmp_money = sea_money[player->cnum];
418         *nsbuild = prod_ship(etu, player->cnum, bp, 1);
419         *sbuild = sea_money[player->cnum] - tmp_money;
420     }
421     if (!lm) {
422         tmp_money = lnd_money[player->cnum];
423         *units = prod_land(etu, player->cnum, bp, 0);
424         *lmaint = lnd_money[player->cnum] - tmp_money;
425     }
426     if (!lb) {
427         tmp_money = lnd_money[player->cnum];
428         *nlbuild = prod_land(etu, player->cnum, bp, 1);
429         *lbuild = lnd_money[player->cnum] - tmp_money;
430     }
431     if (!pm) {
432         tmp_money = air_money[player->cnum];
433         *planes = prod_plane(etu, player->cnum, bp, 0);
434         *pmaint = air_money[player->cnum] - tmp_money;
435     }
436     if (!pb) {
437         tmp_money = air_money[player->cnum];
438         *npbuild = prod_plane(etu, player->cnum, bp, 1);
439         *pbuild = air_money[player->cnum] - tmp_money;
440     }
441
442     /* produce all sects that haven't produced yet */
443     produce_sect(player->cnum, etu, bp, p_sect, -1);
444
445     lnd_money[player->cnum] = sea_money[player->cnum] = 0;
446     air_money[player->cnum] = 0;
447     free(bp);
448 }
449
450 static int
451 goodsect(char c)
452 {
453     register int x;
454
455     for (x = 4; x < SCT_MAXDEF + 2; x++)
456         if (dchr[x].d_mnem == c)
457             return 1;
458
459     return 0;
460 }
461
462 static s_char *
463 dotsprintf(s_char *buf, s_char *format, int data)
464 {
465     sprintf(buf, format, data);
466     return (s_char *)memset(buf, '.', strspn(buf, " "));
467 }
468
469 static void
470 prexpense(long int cash, int *expensesp, s_char priority, int amount)
471 {
472     if (cash > *expensesp) {
473         if (priority) {
474             pr("\t\t%8d\n", amount);
475             *expensesp += amount;
476         } else
477             pr("\t\t(%7d)\n", amount);
478     } else {
479         if (priority) {
480             pr("\t\t[%7d]\n", amount);
481             *expensesp += amount;
482         } else
483             pr("\t\t[(%6d)]\n", amount);
484     }
485 }