src/lib/commands/budg.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  budg.c: Calculate production levels, prioritize
00029  * 
00030  *  Known contributors to this file:
00031  *     Thomas Ruschak, 1992
00032  *     Ville Virrankoski, 1995
00033  *     Steve McClure, 1997-2000
00034  *     Markus Armbruster, 2004-2006
00035  */
00036 
00037 #include <config.h>
00038 
00039 #include <ctype.h>
00040 #include "budg.h"
00041 #include "commands.h"
00042 #include "item.h"
00043 #include "land.h"
00044 #include "optlist.h"
00045 #include "plane.h"
00046 #include "product.h"
00047 #include "ship.h"
00048 
00049 static void calc_all(long (*p_sect)[2], int *taxes, int *Ncivs,
00050                      int *Nuws, int *bars, int *Nbars, int *mil,
00051                      int *ships, int *sbuild, int *nsbuild, int *smaint,
00052                      int *units, int *lbuild, int *nlbuild, int *lmaint,
00053                      int *planes, int *pbuild, int *npbuild, int *pmaint);
00054 static char *dotsprintf(char *buf, char *format, int data);
00055 
00056 int
00057 budg(void)
00058 {
00059     int i;
00060     long p_sect[SCT_TYPE_MAX+2][2];
00061     int taxes, Ncivs, Nuws, bars, Nbars, mil;
00062     int ships, sbuild, nsbuild, smaint;
00063     int units, lbuild, nlbuild, lmaint;
00064     int planes, pbuild, npbuild, pmaint;
00065     int n, etu;
00066     int income, expenses;
00067     struct natstr *np;
00068     char buf[1024];
00069     char in[80];
00070 
00071     etu = etu_per_update;
00072 
00073     np = getnatp(player->cnum);
00074 
00075     player->simulation = 1;
00076     calc_all(p_sect,
00077              &taxes, &Ncivs, &Nuws, &bars, &Nbars, &mil,
00078              &ships, &sbuild, &nsbuild, &smaint,
00079              &units, &lbuild, &nlbuild, &lmaint,
00080              &planes, &pbuild, &npbuild, &pmaint);
00081     player->simulation = 0;
00082 
00083     income = taxes + bars;
00084     expenses = 0;
00085     pr("Sector Type\t\t\tProduction\t\t\t    Cost\n");
00086     for (i = 0; i <= SCT_TYPE_MAX; i++) {
00087         if (!p_sect[i][1] || i == SCT_CAPIT)
00088             continue;
00089         pr("%-17s\t\t", dchr[i].d_name);
00090         if (i == SCT_ENLIST)
00091             pr("%ld mil    \t", p_sect[i][0]);
00092         else if (dchr[i].d_prd >= 0)
00093             pr("%ld %-7s\t", p_sect[i][0], pchr[dchr[i].d_prd].p_sname);
00094         else
00095             pr("\t\t");
00096         pr("\t\t%8ld\n", p_sect[i][1]);
00097         expenses += p_sect[i][1];
00098     }
00099 
00100     if (sbuild) {
00101         sprintf(buf, "%d ship%s", nsbuild, splur(nsbuild));
00102         pr("Ship building\t\t\t%-16s\t\t%8d\n", buf, -sbuild);
00103         expenses += -sbuild;
00104     }
00105 
00106     if (smaint) {
00107         sprintf(buf, "%d ship%s", ships, splur(ships));
00108         pr("Ship maintenance\t\t%-16s\t\t%8d\n", buf, -smaint);
00109         expenses += -smaint;
00110     }
00111 
00112     if (pbuild) {
00113         sprintf(buf, "%d plane%s", npbuild, splur(npbuild));
00114         pr("Plane building\t\t\t%-16s\t\t%8d\n", buf, -pbuild);
00115         expenses += -pbuild;
00116     }
00117 
00118     if (pmaint) {
00119         sprintf(buf, "%d plane%s", planes, splur(planes));
00120         pr("Plane maintenance\t\t%-16s\t\t%8d\n", buf, -pmaint);
00121         expenses += -pmaint;
00122     }
00123     if (lbuild) {
00124         sprintf(buf, "%d unit%s", nlbuild, splur(nlbuild));
00125         pr("Unit building\t\t\t%-16s\t\t%8d\n", buf, -lbuild);
00126         expenses += -lbuild;
00127     }
00128 
00129     if (lmaint) {
00130         sprintf(buf, "%d unit%s", units, splur(units));
00131         pr("Unit maintenance\t\t%-16s\t\t%8d\n", buf, -lmaint);
00132         expenses += -lmaint;
00133     }
00134 
00135     if (p_sect[SCT_EFFIC][1]) {
00136         pr("Sector building\t\t\t\t%8ld sct(s)\t\t%8ld\n",
00137            p_sect[SCT_EFFIC][0], p_sect[SCT_EFFIC][1]);
00138         expenses += p_sect[SCT_EFFIC][1];
00139     }
00140     if (mil) {
00141         n = (mil - np->nat_reserve * money_res * etu) / (etu * money_mil);
00142         sprintf(in, "%d mil, %d res", n, (int)np->nat_reserve);
00143         pr("Military payroll\t\t%-32s%8d\n", in, -mil);
00144         expenses -= mil;
00145     }
00146     if (p_sect[SCT_CAPIT][0]) {
00147         pr("%c%s maintenance\t\t",
00148            toupper(dchr[SCT_CAPIT].d_name[0]),
00149            dchr[SCT_CAPIT].d_name + 1);
00150         n = p_sect[SCT_CAPIT][0];
00151         sprintf(in, "%d %s", n, dchr[SCT_CAPIT].d_name);
00152         plurize(in, sizeof(in), n);
00153         pr("%-32s%8ld\n", in, p_sect[SCT_CAPIT][1]);
00154         expenses += p_sect[SCT_CAPIT][1];
00155     }
00156     pr("Total expenses%s\n", dotsprintf(buf, "%58d", expenses));
00157     if (taxes) {
00158         sprintf(in, "%d civ%s, %d uw%s",
00159                 Ncivs, splur(Ncivs), Nuws, splur(Nuws));
00160         pr("Income from taxes\t\t%-32s%+8d\n", in, taxes);
00161     }
00162     if (bars) {
00163         sprintf(in, "%d bar%s", Nbars, splur(Nbars));
00164         pr("Income from bars\t\t%-32s%+8d\n", in, bars);
00165     }
00166     pr("Total income%s\n", dotsprintf(buf, "%+60d", income));
00167     pr("Balance forward\t\t\t\t\t\t      %10ld\n", np->nat_money);
00168     pr("Estimated delta\t\t\t\t\t\t      %+10d\n", income - expenses);
00169     pr("Estimated new treasury%s\n",
00170        dotsprintf(buf, "%50d", np->nat_money + income - expenses));
00171     if (np->nat_money + income - expenses < 0 && !player->god) {
00172         pr("After processsing sectors, you will be broke!\n");
00173         pr("Sectors will not produce, distribute, or deliver!\n\n");
00174     }
00175 
00176     return RET_OK;
00177 }
00178 
00179 static void
00180 calc_all(long p_sect[][2],
00181          int *taxes, int *Ncivs, int *Nuws, int *bars, int *Nbars, int *mil,
00182          int *ships, int *sbuild, int *nsbuild, int *smaint,
00183          int *units, int *lbuild, int *nlbuild, int *lmaint,
00184          int *planes, int *pbuild, int *npbuild, int *pmaint)
00185 {
00186     struct natstr *np;
00187     struct bp *bp;
00188     long pop = 0;
00189     int n, civ_tax, uw_tax, mil_pay;
00190     struct sctstr *sp;
00191     int etu = etu_per_update;
00192 
00193     memset(p_sect, 0, sizeof(**p_sect) * (SCT_TYPE_MAX+2) * 2);
00194     *taxes = *Ncivs = *Nuws = *bars = *Nbars = *mil = 0;
00195     *ships = *sbuild = *nsbuild = *smaint = 0;
00196     *units = *lbuild = *nlbuild = *lmaint = 0;
00197     *planes = *pbuild = *npbuild = *pmaint = 0;
00198     
00199     np = getnatp(player->cnum);
00200     bp = bp_alloc();
00201     for (n = 0; NULL != (sp = getsectid(n)); n++) {
00202         bp_set_from_sect(bp, sp);
00203         if (sp->sct_own == player->cnum) {
00204             sp->sct_updated = 0;
00205             tax(sp, np, etu, &pop, &civ_tax, &uw_tax, &mil_pay);
00206             *Ncivs += sp->sct_item[I_CIVIL];
00207             *Nuws += sp->sct_item[I_UW];
00208             *taxes += civ_tax + uw_tax;
00209             *mil += mil_pay;
00210             if (sp->sct_type == SCT_BANK) {
00211                 *bars += bank_income(sp, etu);
00212                 *Nbars += sp->sct_item[I_BAR];
00213             }
00214         }
00215     }
00216     tpops[player->cnum] = pop;
00217     *mil += (int)(np->nat_reserve * money_res * etu);
00218 
00219     *mil += upd_slmilcosts(np->nat_cnum, etu);
00220 
00221     /* Maintain ships */
00222     sea_money[player->cnum] = 0;
00223     *ships = prod_ship(etu, player->cnum, bp, 0);
00224     *smaint = sea_money[player->cnum];
00225 
00226     /* Maintain planes */
00227     air_money[player->cnum] = 0;
00228     *planes = prod_plane(etu, player->cnum, bp, 0);
00229     *pmaint = air_money[player->cnum];
00230 
00231     /* Maintain land units */
00232     lnd_money[player->cnum] = 0;
00233     *units = prod_land(etu, player->cnum, bp, 0);
00234     *lmaint = lnd_money[player->cnum];
00235 
00236     /* Produce */
00237     produce_sect(player->cnum, etu, bp, p_sect);
00238 
00239     /* Build ships */
00240     sea_money[player->cnum] = 0;
00241     *nsbuild = prod_ship(etu, player->cnum, bp, 1);
00242     *sbuild = sea_money[player->cnum];
00243     sea_money[player->cnum] = 0;
00244 
00245     /* Build planes */
00246     air_money[player->cnum] = 0;
00247     *npbuild = prod_plane(etu, player->cnum, bp, 1);
00248     *pbuild = air_money[player->cnum];
00249     air_money[player->cnum] = 0;
00250 
00251     /* Build land units */
00252     lnd_money[player->cnum] = 0;
00253     *nlbuild = prod_land(etu, player->cnum, bp, 1);
00254     *lbuild = lnd_money[player->cnum];
00255     lnd_money[player->cnum] = 0;
00256 
00257     free(bp);
00258 }
00259 
00260 static char *
00261 dotsprintf(char *buf, char *format, int data)
00262 {
00263     sprintf(buf, format, data);
00264     return memset(buf, '.', strspn(buf, " "));
00265 }

Generated on Fri Mar 28 11:01:11 2008 for empserver by  doxygen 1.5.2