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