]> git.pond.sub.org Git - empserver/blob - src/lib/commands/budg.c
commands: Rename the command functions
[empserver] / src / lib / commands / budg.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, 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-2020
34  */
35
36 #include <config.h>
37
38 #include <limits.h>
39 #include "commands.h"
40 #include "item.h"
41 #include "optlist.h"
42 #include "product.h"
43 #include "update.h"
44
45 static struct budget *calc_all(void);
46 static char *dotsprintf(char *buf, char *format, int data);
47
48 int
49 c_budget(void)
50 {
51     static struct {
52         char *activity;
53         char *object;
54     } bm_name[] = {
55         { "Ship building", "ship" },
56         { "Ship maintenance", "ship" },
57         { "Plane building", "plane" },
58         { "Plane maintenance", "plane" },
59         { "Unit building", "unit" },
60         { "Unit maintenance", "unit" },
61         { "Sector building", "sector" },
62         { "Sector maintenance", "sector" }
63     };
64     unsigned i;
65     struct budget *budget;
66     int income, expenses, taxes;
67     struct natstr *np;
68     char buf[1024];
69
70     np = getnatp(player->cnum);
71
72     player->simulation = 1;
73     budget = calc_all();
74     player->simulation = 0;
75
76     income = expenses = 0;
77
78     pr("Sector Type\t\t\tProduction\t\t\t    Cost\n");
79     for (i = 0; i < ARRAY_SIZE(budget->prod); i++) {
80         if (!budget->prod[i].money)
81             continue;
82         pr("%-17s\t\t", dchr[i].d_name);
83         if (i == SCT_ENLIST)
84             pr("%d mil    \t", budget->prod[i].count);
85         else if (dchr[i].d_prd >= 0)
86             pr("%d %-7s\t", budget->prod[i].count,
87                pchr[dchr[i].d_prd].p_sname);
88         else
89             pr("\t\t");
90         pr("\t\t%8.0f\n", -budget->prod[i].money);
91         expenses -= budget->prod[i].money;
92     }
93
94     for (i = 0; i <= BUDG_BLD_MAX; i++) {
95         if (!budget->bm[i].money)
96             continue;
97         snprintf(buf, sizeof(buf), "%d %s%s",
98                  budget->bm[i].count, bm_name[i].object,
99                  splur(budget->bm[i].count));
100         pr("%-20s\t\t%-16s\t\t%8.0f\n",
101            bm_name[i].activity, buf, -budget->bm[i].money);
102         expenses -= budget->bm[i].money;
103     }
104
105     if (budget->mil.money) {
106         snprintf(buf, sizeof(buf), "%d mil, %d res",
107                  budget->mil.count, np->nat_reserve);
108         pr("Military payroll\t\t%-32s%8.0f\n",
109            buf, -budget->mil.money);
110         expenses -= budget->mil.money;
111     }
112
113     pr("Total expenses%s\n", dotsprintf(buf, "%58d", expenses));
114     taxes = budget->civ.money + budget->uw.money;
115     if (taxes) {
116         snprintf(buf, sizeof(buf), "%d civ%s, %d uw%s",
117                  budget->civ.count, splur(budget->civ.count),
118                  budget->uw.count, splur(budget->uw.count));
119         pr("Income from taxes\t\t%-32s%+8d\n", buf, taxes);
120         income += taxes;
121     }
122     if (budget->bars.money) {
123         snprintf(buf, sizeof(buf), "%d bar%s",
124                  budget->bars.count, splur(budget->bars.count));
125         pr("Income from bars\t\t%-32s%+8.0f\n",
126            buf, budget->bars.money);
127         income += budget->bars.money;
128     }
129     pr("Total income%s\n", dotsprintf(buf, "%+60d", income));
130     pr("Balance forward\t\t\t\t\t\t      %10d\n", np->nat_money);
131     pr("Estimated delta\t\t\t\t\t\t      %+10d\n", income - expenses);
132     pr("Estimated new treasury%s\n",
133        dotsprintf(buf, "%50d", np->nat_money + income - expenses));
134     if (np->nat_money + income - expenses < 0 && !player->god) {
135         pr("After processing sectors, you will be broke!\n");
136         pr("Sectors will not produce, distribute, or deliver!\n\n");
137     }
138
139     return RET_OK;
140 }
141
142 static struct budget *
143 calc_all(void)
144 {
145     struct budget *budget = &nat_budget[player->cnum];
146     struct natstr *np;
147     struct bp *bp;
148     int i;
149     int etu = etu_per_update;
150
151     memset(nat_budget, 0, sizeof(nat_budget));
152     np = getnatp(player->cnum);
153     /* Take care not to disclose others going broke: */
154     for (i = 0; i < MAXNOC; i++)
155         nat_budget[i].start_money = nat_budget[i].money = INT_MAX;
156     budget->start_money = budget->money = np->nat_money;
157     bp = bp_alloc();
158
159     prep_ships(etu, bp);
160     prep_planes(etu, bp);
161     prep_lands(etu, bp);
162     prepare_sects(etu, bp);
163     pay_reserve(np, etu);
164
165     /* Maintain ships, planes and land units */
166     prod_ship(etu, bp, 0);
167     prod_plane(etu, bp, 0);
168     prod_land(etu, bp, 0);
169
170     /* Produce */
171     produce_sect(etu, bp);
172
173     /* Build ships, planes and land units */
174     prod_ship(etu, bp, 1);
175     prod_plane(etu, bp, 1);
176     prod_land(etu, bp, 1);
177
178     if (CANT_HAPPEN(np->nat_money != budget->start_money))
179         np->nat_money = budget->start_money;
180
181     free(bp);
182     return budget;
183 }
184
185 static char *
186 dotsprintf(char *buf, char *format, int data)
187 {
188     sprintf(buf, format, data);
189     return memset(buf, '.', strspn(buf, " "));
190 }