X-Git-Url: http://git.pond.sub.org/?p=empserver;a=blobdiff_plain;f=src%2Flib%2Fupdate%2Fproduce.c;h=817be09523836322cc59ed2db516c897ca8bc123;hp=2b36f852299c016c2c4f7325c567d3448358b302;hb=4a714a37d;hpb=42290db8c85e93e5abadca04c456ffa25d12fe13 diff --git a/src/lib/update/produce.c b/src/lib/update/produce.c index 2b36f8522..817be0952 100644 --- a/src/lib/update/produce.c +++ b/src/lib/update/produce.c @@ -1,11 +1,11 @@ /* * Empire - A multi-player, client/server Internet based war game. - * Copyright (C) 1986-2000, 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,230 +14,257 @@ * 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. * * --- * * produce.c: Produce goodies - * + * * Known contributors to this file: - * + * Markus Armbruster, 2004-2016 */ -#include "misc.h" -#include "var.h" -#include "sect.h" -#include "product.h" +#include + +#include +#include "chance.h" #include "nat.h" -#include "file.h" -#include "xy.h" +#include "optlist.h" #include "player.h" +#include "product.h" +#include "prototypes.h" #include "update.h" -#include "gen.h" -#include "subs.h" -#include "common.h" -#include "optlist.h" -#include "budg.h" -static void materials_charge(struct pchrstr *, register int *, - register int); -static int materials_cost(struct pchrstr *, register int *, int *); +static void materials_charge(struct pchrstr *, short *, double); + +static char *levelnames[] = { + "Technology", "Research", "Education", "Happiness" +}; + +void +produce(struct natstr *np, struct sctstr *sp) +{ + struct budget *budget = &nat_budget[sp->sct_own]; + struct pchrstr *product; + double prodeff; + double output; + double cost; + + if (dchr[sp->sct_type].d_prd < 0) + return; + product = &pchr[dchr[sp->sct_type].d_prd]; + + prodeff = prod_eff(sp->sct_type, np->nat_level[product->p_nlndx]); + output = prod_output(sp, prodeff); + if (!output) + return; -s_char *levelnames[] = - { "Technology", "Research", "Education", "Happiness" }; + cost = product->p_cost * output / prodeff; + if (opt_TECH_POP) { + if (product->p_level == NAT_TLEV) { + if (tpops[sp->sct_own] > 50000) + cost *= tpops[sp->sct_own] / 50000.0; + } + } + + budget->prod[sp->sct_type].count += ldround(output, 1); + budget->prod[sp->sct_type].money -= cost; + budget->money -= cost; +} -int -produce(struct natstr *np, struct sctstr *sp, int *vec, int work, - int desig, int neweff, int *cost, int *amount) +double +prod_output(struct sctstr *sp, double prodeff) { - register struct pchrstr *product; - int vtype; + struct pchrstr *product = &pchr[dchr[sp->sct_type].d_prd]; + i_type item = product->p_type; + unsigned char *resource; double p_e; - double level_p_e; - s_char *resource; - int output; - int actual; - int unit_work; - double depend; - int item; - int worker_limit; - int material_limit; - int material_consume; + double material_limit, worker_limit, res_limit; + double material_consume, output; + int unit_work, work_used; int val; - product = &pchr[dchr[desig].d_prd]; - if (product == &pchr[0]) - return 0; - vtype = product->p_type; - item = vtype & ~VT_ITEM; - *amount = 0; - *cost = 0; + if (product->p_nrndx) + resource = (unsigned char *)sp + product->p_nrndx; + else + resource = NULL; - if ((material_limit = materials_cost(product, vec, &unit_work)) <= 0) - return 0; - /* - * calculate production efficiency. - */ - p_e = neweff / 100.0; - if (product->p_nrndx != 0) { + material_limit = prod_materials_cost(product, sp->sct_item, + &unit_work); + + /* sector p.e. */ + p_e = sp->sct_effic / 100.0; + if (resource) { unit_work++; - resource = ((s_char *)sp) + product->p_nrndx; - p_e = (*resource * p_e) / 100.0; - if (product->p_nrdep > 0) { - /* XXX this looks way wrong */ - depend = (*resource * 100.0) / product->p_nrdep; - if (p_e > depend) - p_e = depend; - } + p_e *= *resource / 100.0; } - /* - * determine number that can be made with - * the available workforce - */ if (unit_work == 0) unit_work = 1; - material_consume = material_limit; - worker_limit = roundavg(work * p_e / unit_work); + + worker_limit = sp->sct_avail * p_e / unit_work; + res_limit = prod_resource_limit(product, resource); + + material_consume = res_limit; if (material_consume > worker_limit) material_consume = worker_limit; - if (material_consume == 0) - return 0; - level_p_e = 1.0; - if (product->p_nlndx >= 0) { - level_p_e = np->nat_level[product->p_nlndx] - product->p_nlmin; - if ((level_p_e < 0.0) && (!player->simulation)) { + if (material_consume > material_limit) + material_consume = material_limit; + if (CANT_HAPPEN(material_consume < 0.0)) + material_consume = 0.0; + if (material_consume == 0.0) + return 0.0; + + if (prodeff <= 0.0) { + if (!player->simulation) wu(0, sp->sct_own, "%s level too low to produce in %s (need %d)\n", levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin); - return 0; - } - level_p_e = level_p_e / (level_p_e + product->p_nllag); + return 0.0; } + /* * Adjust produced amount by commodity production ratio */ - output = roundavg(product->p_effic * 0.01 * material_consume); - if ((vtype == 0) && (!player->simulation)) { - levels[sp->sct_own][product->p_level] += output * level_p_e; - wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n", - product->p_name, output * level_p_e, ownxy(sp)); - } else { - if ((actual = roundavg(level_p_e * output)) <= 0) - return 0; - if (product->p_nrdep != 0) { - if (*resource * 100 < product->p_nrdep * actual) - actual = *resource * 100 / product->p_nrdep; - } - if (actual > 999) { - actual = 999; - material_consume = (int)(actual / (product->p_effic * 0.01)); + output = material_consume * prodeff; + if (item == I_NONE) { + if (!player->simulation) { + levels[sp->sct_own][product->p_level] += output; + wu(0, sp->sct_own, "%s (%.2f) produced in %s\n", + product->p_name, output, ownxy(sp)); } - vec[item] += actual; - if (vec[item] > 9999) { - material_consume = - roundavg((9999.0 - vec[item] + actual) * - material_consume / actual); - if (material_consume < 0) - material_consume = 0; - vec[item] = 9999; - if (( /* vtype != V_FOOD && */ sp->sct_own) && - (!player->simulation)) + } else { + output = floor(output); + if (output > 999.0) + output = 999.0; + if (sp->sct_item[item] + output > ITEM_MAX) { + output = ITEM_MAX - sp->sct_item[item]; + if (sp->sct_own && !player->simulation) wu(0, sp->sct_own, "%s production backlog in %s\n", product->p_name, ownxy(sp)); } + material_consume = output / prodeff; + sp->sct_item[item] += output; } + /* * Reset produced amount by commodity production ratio */ - if (!player->simulation) { - materials_charge(product, vec, material_consume); - if (product->p_nrdep != 0) { - /* - * lower natural resource in sector depending on - * amount produced - */ - val = *resource - roundavg(product->p_nrdep * - material_consume / 100.0); - if (val < 0) - val = 0; - *resource = val; - } + materials_charge(product, sp->sct_item, material_consume); + if (resource && product->p_nrdep != 0) { + /* + * lower natural resource in sector depending on + * amount produced + */ + val = *resource - roundavg(product->p_nrdep * + material_consume / 100.0); + if (val < 0) + val = 0; + *resource = val; } - *amount = actual; - *cost = product->p_cost * material_consume; - if (opt_TECH_POP) { - if (product->p_level == NAT_TLEV) { - if (tpops[sp->sct_own] > 50000) - *cost = - (double)*cost * (double)tpops[sp->sct_own] / 50000.0; - } - } + if (CANT_HAPPEN(p_e <= 0.0)) + return 0.0; + work_used = roundavg(unit_work * material_consume / p_e); + if (CANT_HAPPEN(work_used > sp->sct_avail)) + work_used = sp->sct_avail; + sp->sct_avail -= work_used; - /* The min() here is to take care of integer rounding errors */ - if (p_e > 0.0) { - return min(work, (int)(unit_work * material_consume / p_e)); - } - return 0; + return output; } -static int -materials_cost(struct pchrstr *product, register int *vec, int *costp) +/* + * Return how much of product @pp can be made from materials @vec[]. + * Store amount of work per unit in *@costp. + */ +double +prod_materials_cost(struct pchrstr *pp, short vec[], int *costp) { - register u_char *vp; - register u_short *ap; - register int count; - register int cost; - register int n; - register u_char *endp; - - count = 9999; + double count, n; + int cost, i; + + count = ITEM_MAX; cost = 0; - ap = product->p_vamt; - endp = product->p_vtype + product->p_nv; - for (vp = product->p_vtype; vp < endp; vp++, ap++) { - if (!*ap) + for (i = 0; i < MAXPRCON; ++i) { + if (!pp->p_camt[i]) + continue; + if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i])) continue; - n = vec[*vp & ~VT_ITEM] / *ap; + n = (double)vec[pp->p_ctype[i]] / pp->p_camt[i]; if (n < count) count = n; - cost += *ap; + cost += pp->p_camt[i]; } *costp = cost; return count; } static void -materials_charge(struct pchrstr *product, register int *vec, - register int count) +materials_charge(struct pchrstr *pp, short *vec, double count) { - register u_char *vp; - register u_short *ap; - register u_char *endp; - register int item; - register int n; - - ap = product->p_vamt; - endp = product->p_vtype + product->p_nv; - for (vp = product->p_vtype; vp < endp; vp++, ap++) { - item = *vp & ~VT_ITEM; - if (item < 0 || item > I_MAX) { - logerror("materials_charge: bad item %d", item); + int i; + i_type item; + double n; + + for (i = 0; i < MAXPRCON; ++i) { + item = pp->p_ctype[i]; + if (!pp->p_camt[i]) continue; - } - if ((n = vec[item] - *ap * count) < 0) { - logerror("materials_charge: %d > %d item #%d", - n, vec[item], item); - n = 0; - } - vec[item] = n; + if (CANT_HAPPEN(item <= I_NONE || I_MAX < item)) + continue; + n = vec[item] - pp->p_camt[i] * count; + if (CANT_HAPPEN(n < 0.0)) + n = 0.0; + vec[item] = roundavg(n); } } + +/* + * Return how much of product @pp can be made from its resource. + * If @pp depletes a resource, @resource must point to its value. + */ +double +prod_resource_limit(struct pchrstr *pp, unsigned char *resource) +{ + if (CANT_HAPPEN(pp->p_nrndx && !resource)) + return 0; + if (resource && pp->p_nrdep != 0) + return *resource * 100.0 / pp->p_nrdep; + return ITEM_MAX; +} + +/* + * Return p.e. for sector type @type. + * Zero means level is too low for production. + * @level is the level affecting production. + */ +double +prod_eff(int type, float level) +{ + double level_p_e; + struct dchrstr *dp = &dchr[type]; + struct pchrstr *pp = &pchr[dp->d_prd]; + + if (CANT_HAPPEN(dp->d_prd < 0)) + return 0.0; + + if (pp->p_nlndx < 0) + level_p_e = 1.0; + else { + double delta = (double)level - (double)pp->p_nlmin; + + if (delta < 0.0) + return 0.0; + if (CANT_HAPPEN(delta + pp->p_nllag <= 0)) + return 0.0; + level_p_e = delta / (delta + pp->p_nllag); + } + + return level_p_e * dp->d_peffic * 0.01; +}