2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future 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 static char *levelnames[] = {
53 "Technology", "Research", "Education", "Happiness"
57 produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
58 int desig, int neweff, int *cost, int *amount)
60 struct pchrstr *product;
63 unsigned char *resource;
73 product = &pchr[dchr[desig].d_prd];
74 if (product == &pchr[0])
76 item = product->p_type;
80 if ((material_limit = materials_cost(product, vec, &unit_work)) <= 0)
83 * calculate production efficiency.
86 if (product->p_nrndx != 0) {
88 resource = (unsigned char *)sp + product->p_nrndx;
89 p_e = (*resource * p_e) / 100.0;
92 * determine number that can be made with
93 * the available workforce
97 material_consume = material_limit;
98 worker_limit = roundavg(work * p_e / unit_work);
99 if (material_consume > worker_limit)
100 material_consume = worker_limit;
101 if (material_consume == 0)
103 prodeff = prod_eff(product, np->nat_level[product->p_nlndx]);
104 if (prodeff <= 0.0 && !player->simulation) {
106 "%s level too low to produce in %s (need %d)\n",
107 levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
111 * Adjust produced amount by commodity production ratio
113 output = material_consume * prodeff;
114 actual = roundavg(output);
117 if (item == I_NONE) {
118 if (!player->simulation) {
119 levels[sp->sct_own][product->p_level] += output;
120 wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
121 product->p_name, output, ownxy(sp));
124 if (product->p_nrdep != 0) {
125 if (*resource * 100 < product->p_nrdep * actual)
126 actual = *resource * 100 / product->p_nrdep;
129 material_consume = roundavg(999.0 * material_consume / actual);
132 if (vec[item] + actual > ITEM_MAX) {
133 material_consume = roundavg((double)(ITEM_MAX - vec[item])
134 * material_consume / actual);
135 if (material_consume < 0)
136 material_consume = 0;
137 vec[item] = ITEM_MAX;
138 if (sp->sct_own && !player->simulation)
140 "%s production backlog in %s\n",
141 product->p_name, ownxy(sp));
146 * Reset produced amount by commodity production ratio
148 if (!player->simulation) {
149 materials_charge(product, vec, material_consume);
150 if (product->p_nrdep != 0) {
152 * lower natural resource in sector depending on
155 val = *resource - roundavg(product->p_nrdep *
156 material_consume / 100.0);
163 *cost = product->p_cost * material_consume;
166 if (product->p_level == NAT_TLEV) {
167 if (tpops[sp->sct_own] > 50000)
168 *cost *= tpops[sp->sct_own] / 50000.0;
172 /* The MIN() here is to take care of integer rounding errors */
174 return MIN(work, (int)(unit_work * material_consume / p_e));
180 materials_cost(struct pchrstr *pp, short *vec, int *costp)
188 for (i = 0; i < MAXPRCON; ++i) {
191 if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
193 n = vec[pp->p_ctype[i]] / pp->p_camt[i];
196 cost += pp->p_camt[i];
203 materials_charge(struct pchrstr *pp, short *vec, int count)
208 for (i = 0; i < MAXPRCON; ++i) {
209 item = pp->p_ctype[i];
212 if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
214 n = vec[item] - pp->p_camt[i] * count;
215 if (CANT_HAPPEN(n < 0))
222 * Return level p.e. for product PP.
223 * Zero means level is too low for production.
224 * LEVEL is the affecting production of PP; it must match PP->p_nlndx.
227 prod_eff(struct pchrstr *pp, float level)
234 double delta = (double)level - (double)pp->p_nlmin;
238 if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
240 level_p_e = delta / (delta + pp->p_nllag);
243 return level_p_e * pp->p_effic * 0.01;