2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * produce.c: Produce goodies
29 * Known contributors to this file:
30 * Markus Armbruster, 2004-2016
41 #include "prototypes.h"
44 static void materials_charge(struct pchrstr *, short *, double);
46 static char *levelnames[] = {
47 "Technology", "Research", "Education", "Happiness"
51 produce(struct natstr *np, struct sctstr *sp)
53 struct budget *budget = &nat_budget[sp->sct_own];
54 struct pchrstr *product;
57 unsigned char *resource;
59 int unit_work, work_used;
61 double material_limit, worker_limit, res_limit;
62 double material_consume;
66 if (dchr[sp->sct_type].d_prd < 0)
68 product = &pchr[dchr[sp->sct_type].d_prd];
69 item = product->p_type;
71 resource = (unsigned char *)sp + product->p_nrndx;
75 material_limit = prod_materials_cost(product, sp->sct_item,
79 p_e = sp->sct_effic / 100.0;
82 p_e *= *resource / 100.0;
87 worker_limit = sp->sct_avail * p_e / unit_work;
88 res_limit = prod_resource_limit(product, resource);
90 material_consume = res_limit;
91 if (material_consume > worker_limit)
92 material_consume = worker_limit;
93 if (material_consume > material_limit)
94 material_consume = material_limit;
95 if (CANT_HAPPEN(material_consume < 0.0))
96 material_consume = 0.0;
97 if (material_consume == 0.0)
100 prodeff = prod_eff(sp->sct_type, np->nat_level[product->p_nlndx]);
101 if (prodeff <= 0.0) {
102 if (!player->simulation)
104 "%s level too low to produce in %s (need %d)\n",
105 levelnames[product->p_nlndx], ownxy(sp), product->p_nlmin);
109 * Adjust produced amount by commodity production ratio
111 output = material_consume * prodeff;
112 if (item == I_NONE) {
113 if (!player->simulation) {
114 levels[sp->sct_own][product->p_level] += output;
115 wu(0, sp->sct_own, "%s (%.2f) produced in %s\n",
116 product->p_name, output, ownxy(sp));
119 output = floor(output);
122 if (sp->sct_item[item] + output > ITEM_MAX) {
123 output = ITEM_MAX - sp->sct_item[item];
124 if (sp->sct_own && !player->simulation)
126 "%s production backlog in %s\n",
127 product->p_name, ownxy(sp));
129 material_consume = output / prodeff;
130 sp->sct_item[item] += output;
134 * Reset produced amount by commodity production ratio
136 materials_charge(product, sp->sct_item, material_consume);
137 if (resource && product->p_nrdep != 0) {
139 * lower natural resource in sector depending on
142 val = *resource - roundavg(product->p_nrdep *
143 material_consume / 100.0);
149 cost = product->p_cost * material_consume;
151 if (product->p_level == NAT_TLEV) {
152 if (tpops[sp->sct_own] > 50000)
153 cost *= tpops[sp->sct_own] / 50000.0;
157 budget->prod[sp->sct_type].count += ldround(output, 1);
158 budget->prod[sp->sct_type].money -= cost;
159 budget->money -= cost;
161 if (CANT_HAPPEN(p_e <= 0.0))
163 work_used = roundavg(unit_work * material_consume / p_e);
164 if (CANT_HAPPEN(work_used > sp->sct_avail))
165 work_used = sp->sct_avail;
166 sp->sct_avail -= work_used;
170 * Return how much of product @pp can be made from materials @vec[].
171 * Store amount of work per unit in *@costp.
174 prod_materials_cost(struct pchrstr *pp, short vec[], int *costp)
181 for (i = 0; i < MAXPRCON; ++i) {
184 if (CANT_HAPPEN(pp->p_ctype[i] <= I_NONE || I_MAX < pp->p_ctype[i]))
186 n = (double)vec[pp->p_ctype[i]] / pp->p_camt[i];
189 cost += pp->p_camt[i];
196 materials_charge(struct pchrstr *pp, short *vec, double count)
202 for (i = 0; i < MAXPRCON; ++i) {
203 item = pp->p_ctype[i];
206 if (CANT_HAPPEN(item <= I_NONE || I_MAX < item))
208 n = vec[item] - pp->p_camt[i] * count;
209 if (CANT_HAPPEN(n < 0.0))
211 vec[item] = roundavg(n);
216 * Return how much of product @pp can be made from its resource.
217 * If @pp depletes a resource, @resource must point to its value.
220 prod_resource_limit(struct pchrstr *pp, unsigned char *resource)
222 if (CANT_HAPPEN(pp->p_nrndx && !resource))
224 if (resource && pp->p_nrdep != 0)
225 return *resource * 100.0 / pp->p_nrdep;
230 * Return p.e. for sector type @type.
231 * Zero means level is too low for production.
232 * @level is the level affecting production.
235 prod_eff(int type, float level)
238 struct dchrstr *dp = &dchr[type];
239 struct pchrstr *pp = &pchr[dp->d_prd];
241 if (CANT_HAPPEN(dp->d_prd < 0))
247 double delta = (double)level - (double)pp->p_nlmin;
251 if (CANT_HAPPEN(delta + pp->p_nllag <= 0))
253 level_p_e = delta / (delta + pp->p_nllag);
256 return level_p_e * dp->d_peffic * 0.01;