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:
41 static void materials_charge(struct pchrstr *, short *, int);
42 static int materials_cost(struct pchrstr *, short *, int *);
44 static char *levelnames[] = {
45 "Technology", "Research", "Education", "Happiness"
49 produce(struct natstr *np, struct sctstr *sp, short *vec, int work,
50 int desig, int neweff, int *cost, int *amount)
52 struct pchrstr *product;
55 unsigned char *resource;
65 if (dchr[desig].d_prd < 0)
67 product = &pchr[dchr[desig].d_prd];
68 item = product->p_type;
72 if ((material_limit = materials_cost(product, vec, &unit_work)) <= 0)
75 * calculate production efficiency.
78 if (product->p_nrndx != 0) {
80 resource = (unsigned char *)sp + product->p_nrndx;
81 p_e = (*resource * p_e) / 100.0;
84 * determine number that can be made with
85 * the available workforce
89 material_consume = material_limit;
90 worker_limit = roundavg(work * p_e / unit_work);
91 if (material_consume > worker_limit)
92 material_consume = worker_limit;
93 if (material_consume == 0)
95 prodeff = prod_eff(desig, np->nat_level[product->p_nlndx]);
96 if (prodeff <= 0.0 && !player->simulation) {
98 "%s level too low to produce in %s (need %d)\n",
99 levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
103 * Adjust produced amount by commodity production ratio
105 output = material_consume * prodeff;
106 actual = roundavg(output);
109 if (item == I_NONE) {
110 if (!player->simulation) {
111 levels[sp->sct_own][product->p_level] += output;
112 wu((natid)0, sp->sct_own, "%s (%.2f) produced in %s\n",
113 product->p_name, output, ownxy(sp));
116 if (product->p_nrdep != 0) {
117 if (*resource * 100 < product->p_nrdep * actual)
118 actual = *resource * 100 / product->p_nrdep;
121 material_consume = roundavg(999.0 * material_consume / actual);
124 if (vec[item] + actual > ITEM_MAX) {
125 material_consume = roundavg((double)(ITEM_MAX - vec[item])
126 * material_consume / actual);
127 if (material_consume < 0)
128 material_consume = 0;
129 vec[item] = ITEM_MAX;
130 if (sp->sct_own && !player->simulation)
132 "%s production backlog in %s\n",
133 product->p_name, ownxy(sp));
138 * Reset produced amount by commodity production ratio
140 if (!player->simulation) {
141 materials_charge(product, vec, material_consume);
142 if (product->p_nrdep != 0) {
144 * lower natural resource in sector depending on
147 val = *resource - roundavg(product->p_nrdep *
148 material_consume / 100.0);
155 *cost = product->p_cost * material_consume;
158 if (product->p_level == NAT_TLEV) {
159 if (tpops[sp->sct_own] > 50000)
160 *cost *= tpops[sp->sct_own] / 50000.0;
164 /* The MIN() here is to take care of integer rounding errors */
166 return MIN(work, (int)(unit_work * material_consume / p_e));
172 materials_cost(struct pchrstr *pp, short *vec, int *costp)
180 for (i = 0; i < MAXPRCON; ++i) {
183 if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
185 n = vec[pp->p_ctype[i]] / pp->p_camt[i];
188 cost += pp->p_camt[i];
195 materials_charge(struct pchrstr *pp, short *vec, int count)
200 for (i = 0; i < MAXPRCON; ++i) {
201 item = pp->p_ctype[i];
204 if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
206 n = vec[item] - pp->p_camt[i] * count;
207 if (CANT_HAPPEN(n < 0))
214 * Return level p.e. for sector type TYPE.
215 * Zero means level is too low for production.
216 * LEVEL is the affecting production of PP; it must match PP->p_nlndx.
219 prod_eff(int type, float level)
222 struct dchrstr *dp = &dchr[type];
223 struct pchrstr *pp = &pchr[dp->d_prd];
225 if (CANT_HAPPEN(dp->d_prd < 0))
231 double delta = (double)level - (double)pp->p_nlmin;
235 if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
237 level_p_e = delta / (delta + pp->p_nllag);
240 return level_p_e * dp->d_peffic * 0.01;