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
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.
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.
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
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.
28 * produce.c: Produce goodies
30 * Known contributors to this file:
49 static void materials_charge(struct pchrstr *, short *, int);
50 static int materials_cost(struct pchrstr *, short *, int *);
52 s_char *levelnames[] =
53 { "Technology", "Research", "Education", "Happiness" };
56 produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
57 int desig, int neweff, int *cost, int *amount)
59 register struct pchrstr *product;
72 product = &pchr[dchr[desig].d_prd];
73 if (product == &pchr[0])
75 item = product->p_type;
79 if ((material_limit = materials_cost(product, vec, &unit_work)) <= 0)
82 * calculate production efficiency.
85 if (product->p_nrndx != 0) {
87 resource = ((s_char *)sp) + product->p_nrndx;
88 p_e = (*resource * p_e) / 100.0;
91 * determine number that can be made with
92 * the available workforce
96 material_consume = material_limit;
97 worker_limit = roundavg(work * p_e / unit_work);
98 if (material_consume > worker_limit)
99 material_consume = worker_limit;
100 if (material_consume == 0)
102 prodeff = prod_eff(product, np->nat_level[product->p_nlndx]);
103 if (prodeff <= 0.0 && !player->simulation) {
105 "%s level too low to produce in %s (need %d)\n",
106 levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
110 * Adjust produced amount by commodity production ratio
112 output = material_consume * prodeff;
113 if (item == I_NONE && !player->simulation) {
114 levels[sp->sct_own][product->p_level] += output;
115 wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
116 product->p_name, output, ownxy(sp));
118 if ((actual = roundavg(output)) <= 0)
120 if (product->p_nrdep != 0) {
121 if (*resource * 100 < product->p_nrdep * actual)
122 actual = *resource * 100 / product->p_nrdep;
125 material_consume = roundavg(999.0 * material_consume / actual);
128 if (vec[item] + actual > ITEM_MAX) {
129 material_consume = roundavg((double)(ITEM_MAX - vec[item])
130 * material_consume / actual);
131 if (material_consume < 0)
132 material_consume = 0;
133 vec[item] = ITEM_MAX;
134 if (sp->sct_own && !player->simulation)
136 "%s production backlog in %s\n",
137 product->p_name, ownxy(sp));
142 * Reset produced amount by commodity production ratio
144 if (!player->simulation) {
145 materials_charge(product, vec, material_consume);
146 if (product->p_nrdep != 0) {
148 * lower natural resource in sector depending on
151 val = *resource - roundavg(product->p_nrdep *
152 material_consume / 100.0);
159 *cost = product->p_cost * material_consume;
162 if (product->p_level == NAT_TLEV) {
163 if (tpops[sp->sct_own] > 50000)
165 (double)*cost * (double)tpops[sp->sct_own] / 50000.0;
169 /* The min() here is to take care of integer rounding errors */
171 return min(work, (int)(unit_work * material_consume / p_e));
177 materials_cost(struct pchrstr *pp, short *vec, int *costp)
185 for (i = 0; i < MAXPRCON; ++i) {
188 if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
190 n = vec[pp->p_ctype[i]] / pp->p_camt[i];
193 cost += pp->p_camt[i];
200 materials_charge(struct pchrstr *pp, short *vec, int count)
204 for (i = 0; i < MAXPRCON; ++i) {
205 item = pp->p_ctype[i];
208 if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
210 n = vec[item] - pp->p_camt[i] * count;
211 if (CANT_HAPPEN(n < 0))
218 * Return level p.e. for product PP.
219 * Zero means level is too low for production.
220 * LEVEL is the affecting production of PP; it must match PP->p_nlndx.
223 prod_eff(struct pchrstr *pp, float level)
230 double delta = (double)level - (double)pp->p_nlmin;
234 if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
236 level_p_e = delta / (delta + pp->p_nllag);
239 return level_p_e * pp->p_effic * 0.01;