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