X-Git-Url: http://git.pond.sub.org/?p=empserver;a=blobdiff_plain;f=src%2Flib%2Fcommands%2Fprod.c;h=86cd78479e66b02fc13dc7fdc4d874558b9fc563;hp=6101b17cc002674a540fa8f83d2a02c4cb2fe01c;hb=4a714a37d;hpb=a2798857cf87591531ce14f3481301ad8c86b7d6 diff --git a/src/lib/commands/prod.c b/src/lib/commands/prod.c index 6101b17cc..86cd78479 100644 --- a/src/lib/commands/prod.c +++ b/src/lib/commands/prod.c @@ -1,11 +1,11 @@ /* * Empire - A multi-player, client/server Internet based war game. - * Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak, - * Ken Stevens, Steve McClure + * Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak, + * Ken Stevens, Steve McClure, Markus Armbruster * - * This program is free software; you can redistribute it and/or modify + * Empire is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or + * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, @@ -14,35 +14,35 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * along with this program. If not, see . * * --- * - * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the - * related information and legal notices. It is expected that any future - * projects/authors will amend these files as needed. + * See files README, COPYING and CREDITS in the root of the source + * tree for related information and legal notices. It is expected + * that future projects/authors will amend these files as needed. * * --- * * prod.c: Calculate production levels - * + * * Known contributors to this file: * David Muir Sharnoff, 1987 * Steve McClure, 1997-2000 + * Markus Armbruster, 2004-2016 */ -#include "misc.h" -#include "player.h" -#include "xy.h" -#include "nsc.h" -#include "sect.h" -#include "product.h" -#include "nat.h" +#include + +#include +#include "commands.h" #include "item.h" -#include "file.h" #include "optlist.h" -#include "commands.h" +#include "product.h" +#include "update.h" + +static void prprod(struct sctstr *, double, double, char, + double, double, double, char[], int[], int[], int); int count_pop(int n) @@ -65,39 +65,22 @@ int prod(void) { struct natstr *natp; - struct sctstr sect; + struct sctstr sect, scratch_sect; struct nstr_sect nstr; struct pchrstr *pp; double p_e; - double maxr; /* floating version of max */ double prodeff; - double real; /* floating pt version of act */ - int work; int totpop; - int act; /* actual production */ - int cost; - int i, j; - int max; /* production w/infinite materials */ + double cost; + int i; int nsect; - double take; - double mtake; - int there; - int unit_work; /* sum of component amounts */ - int used; /* production w/infinite workforce */ + double real, maxr; + double take, mtake; i_type it; - i_type vtype; - u_char *resource; - s_char maxc[MAXPRCON][10]; - s_char use[MAXPRCON][10]; - int lcms, hcms; - int civs = 0; - int uws = 0; - int bwork; - int twork; - int type; - int eff; - int maxpop; - u_char otype; + unsigned char *resource; + char cmnem[MAXPRCON]; + int cuse[MAXPRCON], cmax[MAXPRCON]; + char mnem; if (!snxtsct(&nstr, player->argp[1])) return RET_SYN; @@ -107,134 +90,61 @@ prod(void) while (nxtsct(&nstr, §)) { if (!player->owner) continue; + if (sect.sct_off) + continue; - civs = (1.0 + obrate * etu_per_update) * sect.sct_item[I_CIVIL]; - uws = (1.0 + uwbrate * etu_per_update) * sect.sct_item[I_UW]; natp = getnatp(sect.sct_own); - maxpop = max_pop(natp->nat_level[NAT_RLEV], §); + do_feed(§, natp, etu_per_update, 1); + buildeff(§); + if (sect.sct_effic < 60) + continue; - /* This isn't quite right, since research might rise/fall */ - /* during the update, but it's the best we can really do */ - work = new_work(§, - total_work(sect.sct_work, etu_per_update, - civs, sect.sct_item[I_MILIT], uws, maxpop)); - bwork = work / 2; + if (sect.sct_type == SCT_ENLIST) { + int maxmil; + int enlisted; - if (sect.sct_off) - continue; - type = sect.sct_type; - eff = sect.sct_effic; - if (sect.sct_newtype != type) { - twork = (eff + 3) / 4; - if (twork > bwork) { - twork = bwork; - } - bwork -= twork; - eff -= twork * 4; - otype = type; - if (eff <= 0) { - type = sect.sct_newtype; - eff = 0; - } - if (!eff && IS_BIG_CITY(otype) && - !IS_BIG_CITY(type)) { - natp = getnatp(sect.sct_own); - maxpop = max_population(natp->nat_level[NAT_RLEV], - type, eff); - work = new_work(§, - total_work(sect.sct_work, etu_per_update, - civs, sect.sct_item[I_MILIT], - uws, maxpop)); - bwork = min(work / 2, bwork); - } - twork = 100 - eff; - if (twork > bwork) { - twork = bwork; - } - if (dchr[type].d_lcms > 0) { - lcms = sect.sct_item[I_LCM]; - lcms /= dchr[type].d_lcms; - if (twork > lcms) - twork = lcms; - } - if (dchr[type].d_hcms > 0) { - hcms = sect.sct_item[I_HCM]; - hcms /= dchr[type].d_hcms; - if (twork > hcms) - twork = hcms; - } - bwork -= twork; - eff += twork; - } else if (eff < 100) { - twork = 100 - eff; - if (twork > bwork) { - twork = bwork; + if (sect.sct_own != sect.sct_oldown) + continue; + enlisted = 0; + maxmil = sect.sct_item[I_CIVIL] / 2 - sect.sct_item[I_MILIT]; + if (maxmil > 0) { + enlisted = (etu_per_update + * (10 + sect.sct_item[I_MILIT]) + * 0.05); + if (enlisted > maxmil) + enlisted = maxmil; } - bwork -= twork; - eff += twork; + if (enlisted < 0) + enlisted = 0; + prprod(§, sect.sct_effic / 100.0, 1.0, + ichr[I_MILIT].i_mnem, enlisted, maxmil, enlisted * 3, + "c\0\0", &enlisted, &enlisted, nsect++); + continue; } - work = (work + 1) / 2 + bwork; - if (eff < 60) + + if (dchr[sect.sct_type].d_prd < 0) continue; + pp = &pchr[dchr[sect.sct_type].d_prd]; + if (pp->p_nrndx) + resource = (unsigned char *)§ + pp->p_nrndx; + else + resource = NULL; - p_e = eff / 100.0; - if (p_e > 1.0) - p_e = 1.0; + /* sector p.e. */ + p_e = sect.sct_effic / 100.0; + if (resource) + p_e *= *resource / 100.0; - if (dchr[type].d_prd == 0 && type != SCT_ENLIST) - continue; - unit_work = 0; - pp = &pchr[dchr[type].d_prd]; - vtype = pp->p_type; - natp = getnatp(sect.sct_own); - /* - * sect p_e (inc improvements) - */ - if (type == SCT_ENLIST && sect.sct_own != sect.sct_oldown) - continue; - if (type == SCT_ENLIST) - goto is_enlist; - if (pp->p_nrndx != 0) { - unit_work++; - resource = (u_char *)§ + pp->p_nrndx; - p_e = (*resource * p_e) / 100.0; - } - /* - * production effic. - */ - prodeff = prod_eff(pp, natp->nat_level[pp->p_nlndx]); - /* - * raw material limit - */ - used = 9999; - for (j = 0; j < MAXPRCON; ++j) { - it = pp->p_ctype[j]; - if (!pp->p_camt[j]) - continue; - if (CANT_HAPPEN(it <= I_NONE || I_MAX < it)) - continue; - used = min(used, sect.sct_item[it] / pp->p_camt[j]); - unit_work += pp->p_camt[j]; - } - if (unit_work == 0) - unit_work = 1; - /* - * is production limited by resources or - * workforce? - */ - max = (int)(work * p_e / (double)unit_work + 0.5); - act = min(used, max); + prodeff = prod_eff(sect.sct_type, natp->nat_level[pp->p_nlndx]); - real = dmin(999.0, (double)act * prodeff); - maxr = dmin(999.0, (double)max * prodeff); + scratch_sect = sect; + real = prod_output(&scratch_sect, prodeff); - if (vtype != I_NONE) { - if (real < 0.0) - real = 0.0; - /* production backlog? */ - there = min(ITEM_MAX, sect.sct_item[vtype]); - real = dmin(real, ITEM_MAX - there); - } + scratch_sect = sect; + for (i = 0; i < MAXPRCON; ++i) + scratch_sect.sct_item[pp->p_ctype[i]] = ITEM_MAX; + scratch_sect.sct_item[pp->p_type] = 0; + maxr = prod_output(&scratch_sect, prodeff); if (prodeff != 0) { take = real / prodeff; @@ -242,115 +152,35 @@ prod(void) } else mtake = take = 0.0; - cost = (int)(take * (double)pp->p_cost); + cost = take * pp->p_cost; if (opt_TECH_POP) { if (pp->p_level == NAT_TLEV) { totpop = count_pop(sect.sct_own); if (totpop > 50000) - cost = (int)((double)cost * (double)totpop / 50000.0); + cost *= totpop / 50000.0; } } - i = 0; - for (j = 0; j < MAXPRCON; ++j) { - it = pp->p_ctype[j]; - if (it > I_NONE && it <= I_MAX && ichr[it].i_mnem != 0) { - if (CANT_HAPPEN(i >= 3)) - break; - sprintf(use[i], "%4d%c", - (int)((take * (double)pp->p_camt[j]) + 0.5), - ichr[it].i_mnem); - sprintf(maxc[i], "%4d%c", - (int)((mtake * (double)pp->p_camt[j]) + 0.5), - ichr[it].i_mnem); - ++i; - } - } - while (i < 3) { - strcpy(use[i], " "); - strcpy(maxc[i], " "); - ++i; - } - - is_enlist: - - if (nsect++ == 0) { - pr("PRODUCTION SIMULATION\n"); - pr(" sect des eff avail make p.e. cost use1 use2 use3 max1 max2 max3 max\n"); - } - - prxy("%4d,%-4d", nstr.x, nstr.y, player->cnum); - pr(" %c", dchr[type].d_mnem); - pr(" %3.0f%%", p_e * 100.0); - pr(" %5d", work); - - if (vtype != I_NONE) { - pr(" %4d%c", (int)(real + 0.5), ichr[vtype].i_mnem); - } else if (type != SCT_ENLIST) { - switch (pp->p_level) { - case NAT_TLEV: - case NAT_RLEV: - pr(" %5.2f", real); - break; - case NAT_ELEV: - case NAT_HLEV: - pr(" %4.0f ", real); - break; - default: - CANT_HAPPEN("bad TYPE"); - pr(" ??? "); - break; - } - } else { - int maxmil; - int enlisted; - int civs; - - civs = (1.0 + obrate * etu_per_update) * sect.sct_item[I_CIVIL]; - natp = getnatp(sect.sct_own); - maxpop = max_pop(natp->nat_level[NAT_RLEV], §); - civs = min(civs, maxpop); - /* This isn't quite right, since research might - rise/fall during the update, but it's the best - we can really do */ - enlisted = 0; - maxmil = (civs / 2) - sect.sct_item[I_MILIT]; - if (maxmil > 0) { - enlisted = (etu_per_update - * (10 + sect.sct_item[I_MILIT]) - * 0.05); - if (enlisted > maxmil) - enlisted = maxmil; - } - if (enlisted < 0) - enlisted = 0; - if (natp->nat_priorities[type] == 0) { - maxmil = 0; - } - pr(" %4d%c 1.00 $%-5d%4dc %4dc %5d\n", - enlisted, ichr[I_MILIT].i_mnem, enlisted * 3, - enlisted, enlisted, maxmil); - continue; + for (i = 0; i < MAXPRCON; ++i) { + cmnem[i] = cuse[i] = cmax[i] = 0; + if (!pp->p_camt[i]) + continue; + it = pp->p_ctype[i]; + if (CANT_HAPPEN(it <= I_NONE || I_MAX < it)) + continue; + cmnem[i] = ichr[it].i_mnem; + cuse[i] = (int)ceil(take * pp->p_camt[i]); + cmax[i] = (int)ceil(mtake * pp->p_camt[i]); } - pr(" %.2f", prodeff); - pr(" $%-5d", cost); - for (i = 0; i < 3; i++) { - pr(use[i]); - } - pr(" "); - for (i = 0; i < 3; i++) { - pr(maxc[i]); - } - if (natp->nat_priorities[type] == 0) { - max = 0; - maxr = 0; - } - if (vtype != I_NONE || pp->p_level == NAT_ELEV - || pp->p_level == NAT_HLEV) - pr(" %5d\n", min(999, (int)(max * prodeff + 0.5))); + if (pp->p_type != I_NONE) + mnem = ichr[pp->p_type].i_mnem; + else if (pp->p_level == NAT_TLEV || pp->p_level == NAT_RLEV) + mnem = '.'; else - pr(" %5.2f\n", maxr); + mnem = 0; + prprod(§, p_e, prodeff, mnem, real, maxr, cost, + cmnem, cuse, cmax, nsect++); } player->simulation = 0; if (nsect == 0) { @@ -363,3 +193,42 @@ prod(void) pr("%d sector%s\n", nsect, splur(nsect)); return RET_OK; } + +static void +prprod(struct sctstr *sp, double p_e, double prodeff, + char mnem, double make, double max, double cost, + char cmnem[], int cuse[], int cmax[], int nsect) +{ + int i; + + if (nsect == 0) { + pr("PRODUCTION SIMULATION\n"); + pr(" sect des eff avail make p.e. cost use1 use2 use3 max1 max2 max3 max\n"); + } + + prxy("%4d,%-4d", sp->sct_x, sp->sct_y); + pr(" %c %3.0f%% %5d", + dchr[sp->sct_type].d_mnem, p_e * 100.0, sp->sct_avail); + if (mnem == '.') + pr(" %5.2f", make); + else + pr(" %4.0f%c", make, mnem ? mnem : ' '); + pr(" %.2f $%-5.0f", prodeff, cost); + for (i = 0; i < 3; i++) { + if (i < MAXPRCON && cmnem[i]) + pr("%4d%c", cuse[i], cmnem[i]); + else + pr(" "); + } + pr(" "); + for (i = 0; i < 3; i++) { + if (i < MAXPRCON && cmnem[i]) + pr("%4d%c", cmax[i], cmnem[i]); + else + pr(" "); + } + if (mnem == '.') + pr(" %5.2f\n", max); + else + pr(" %5.0f\n", max); +}