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 * sect.c: Do production for sectors
30 * Known contributors to this file:
46 #include "distribute.h"
59 * Increase sector efficiency if old type == new type.
60 * decrease sector efficiency if old type != new type.
61 * Return amount of work used.
64 upd_buildeff(struct natstr *np, register struct sctstr *sp, int *workp,
65 short *vec, int etu, int *desig, int sctwork, int *cost)
67 register int work_cost = 0;
68 int buildeff_work = *workp / 2;
69 int n, hcms, lcms, neweff;
70 u_char old_type = *desig;
73 neweff = sp->sct_effic;
75 if (*desig != sp->sct_newtype) {
77 * Tear down existing sector.
78 * Easier to destroy than to build.
80 work_cost = (sp->sct_effic + 3) / 4;
81 if (work_cost > buildeff_work)
82 work_cost = buildeff_work;
83 buildeff_work -= work_cost;
84 n = sp->sct_effic - work_cost * 4;
87 *desig = sp->sct_newtype;
92 if (!n && dchr[old_type].d_pkg == UPKG &&
93 dchr[*desig].d_pkg != UPKG) {
94 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
95 if (vec[I_CIVIL] > maxpop)
96 vec[I_CIVIL] = maxpop;
97 if (vec[I_UW] > maxpop)
99 *workp = (vec[I_CIVIL] * sctwork) / 100.0
100 + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
101 *workp = roundavg((etu * (*workp)) / 100.0);
103 buildeff_work = min((int)(*workp / 2), buildeff_work);
107 if (np->nat_priorities[*desig]) {
108 if (*desig == sp->sct_newtype) {
109 work_cost = 100 - neweff;
110 if (work_cost > buildeff_work)
111 work_cost = buildeff_work;
113 if (dchr[*desig].d_lcms > 0) {
115 lcms /= dchr[*desig].d_lcms;
116 if (work_cost > lcms)
119 if (dchr[*desig].d_hcms > 0) {
121 hcms /= dchr[*desig].d_hcms;
122 if (work_cost > hcms)
127 *cost += work_cost * dchr[*desig].d_build;
128 buildeff_work -= work_cost;
130 if ((dchr[*desig].d_lcms > 0) || (dchr[*desig].d_hcms > 0)) {
131 vec[I_LCM] -= work_cost * dchr[*desig].d_lcms;
132 vec[I_HCM] -= work_cost * dchr[*desig].d_hcms;
136 *workp = (*workp + 1) / 2 + buildeff_work;
142 * enlistment sectors are special; they require military
143 * to convert civ into mil in large numbers.
144 * Conversion will happen much more slowly without
145 * some mil initially.
148 enlist(short *vec, int etu, int *cost)
153 /* Need to check treaties here */
155 maxmil = (vec[I_CIVIL] / 2) - vec[I_MILIT];
157 enlisted = (etu * (10 + vec[I_MILIT]) * 0.05);
158 if (enlisted > maxmil)
160 vec[I_CIVIL] -= enlisted;
161 vec[I_MILIT] += enlisted;
163 *cost = enlisted * 3;
167 /* Fallout is calculated here. */
170 meltitems(int etus, int fallout, int own, short *vec, int type, int x, int y,
176 for (n = 1; n <= I_MAX; n++) {
177 melt = roundavg(vec[n] * etus * (long)fallout
178 / (1000.0 * melt_item_denom[n]));
181 if (melt > 5 && own) {
182 if (type == EF_SECTOR)
183 wu(0, own, "Lost %d %s to radiation in %s.\n",
184 melt < vec[n] ? melt : vec[n], ichr[n].i_name,
186 else if (type == EF_LAND)
187 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
188 uid, melt < vec[n] ? melt : vec[n], ichr[n].i_name,
190 else if (type == EF_SHIP)
191 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
192 uid, melt < vec[n] ? melt : vec[n], ichr[n].i_name,
200 * do_fallout - calculate fallout for sectors.
202 * This is etu based. But, do limit HUGE kill offs in large ETU
203 * games, the melting etus rate is limited to 24 etus.
207 do_fallout(register struct sctstr *sp, register int etus)
213 /* This check shouldn't be needed, but just in case. :) */
214 if (!sp->sct_fallout || !sp->sct_updated)
218 meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item, EF_SECTOR,
219 sp->sct_x, sp->sct_y, 0);
220 for (i = 0; NULL != (lp = getlandp(i)); i++) {
223 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
225 meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item, EF_LAND,
226 lp->lnd_x, lp->lnd_y, lp->lnd_uid);
228 for (i = 0; NULL != (spp = getshipp(i)); i++) {
231 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
233 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
235 meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item, EF_SHIP,
236 spp->shp_x, spp->shp_y, spp->shp_uid);
239 if ((sp->sct_fallout > 20) && chance(100))
241 #endif /* GODZILLA */
245 spread_fallout(struct sctstr *sp, int etus)
253 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
254 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
255 if (ap->sct_type == SCT_SANCT)
257 inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
259 if (sp->sct_fallout) {
260 wu(0, 0, "Fallout from sector %d,%d to %d,%d is %d=%d*%e*%d\n",
261 sp->sct_x, sp->sct_y, sp->sct_x + diroff[n][0],
262 sp->sct_y + diroff[n][1], inc, etus,
263 fallout_spread, sp->sct_fallout);
268 ap->sct_fallout = min(ap->sct_fallout + inc, FALLOUT_MAX);
273 decay_fallout(struct sctstr *sp, int etus)
279 decay = roundavg(((decay_per_etu + 6.0) * fallout_spread) *
280 (double)etus * (double)sp->sct_fallout);
283 if (decay || sp->sct_fallout)
284 wu(0, 0, "Fallout decay in %d,%d is %d from %d\n", sp->sct_x,
285 sp->sct_y, decay, sp->sct_fallout);
288 sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
292 * Produce only a set sector type for a specific nation
293 * (or all, if sector_type == -1)
297 produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
300 register struct sctstr *sp;
301 register struct natstr *np;
302 short buf[I_MAX + 1];
304 int work, cost, ecost, pcost, sctwork;
305 int n, desig, maxpop, neweff, amount;
307 for (n = 0; NULL != (sp = getsectid(n)); n++) {
308 if (sp->sct_type == SCT_WATER)
310 if (sp->sct_own != natnum)
312 if (sp->sct_updated != 0)
314 if (sp->sct_type != sector_type && sector_type != -1)
317 if ((sp->sct_type == SCT_CAPIT) && (sp->sct_effic > 60)) {
318 p_sect[SCT_CAPIT][0]++;
319 p_sect[SCT_CAPIT][1] += etu;
322 if (player->simulation) {
323 /* work on a copy, which will be discarded */
324 memcpy(buf, sp->sct_item, sizeof(buf));
329 /* If everybody is dead, the sector reverts to unowned.
330 * This is also checked at the end of the production in
331 * they all starved or were plagued off.
333 if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
334 !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
335 makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
344 np = getnatp(natnum);
346 /* do_feed trys to supply. So, we need to enable cacheing
348 bp_enable_cachepath();
350 sctwork = do_feed(sp, np, vec, &work, bp, etu);
352 bp_disable_cachepath();
353 bp_clear_cachepath();
355 if (sp->sct_off || np->nat_money < 0) {
356 if (!player->simulation)
360 if ((np->nat_priorities[sp->sct_type] == 0) &&
361 (sp->sct_type == sp->sct_newtype) &&
362 ((pchr[dchr[sp->sct_type].d_prd].p_cost != 0) ||
363 (sp->sct_type == SCT_ENLIST))) {
364 if (!player->simulation) {
365 logerror("Skipping %s production for country %s\n",
366 dchr[sp->sct_type].d_name, np->nat_cnam);
371 neweff = sp->sct_effic;
373 pcost = cost = ecost = 0;
375 desig = sp->sct_type;
377 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
379 neweff = upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
381 pt_bg_nmbr(bp, sp, I_LCM, vec[I_LCM]);
382 pt_bg_nmbr(bp, sp, I_HCM, vec[I_HCM]);
383 p_sect[SCT_EFFIC][0]++;
384 p_sect[SCT_EFFIC][1] += cost;
385 if (!player->simulation) {
386 np->nat_money -= cost;
387 sp->sct_type = desig;
388 sp->sct_effic = neweff;
389 if (!opt_DEFENSE_INFRA)
390 sp->sct_defense = sp->sct_effic;
394 if ((np->nat_priorities[desig] == 0) &&
395 ((pchr[dchr[desig].d_prd].p_cost != 0) ||
396 (desig == SCT_ENLIST))) {
397 if (!player->simulation) {
398 logerror("Skipping %s production for country %s\n",
399 dchr[sp->sct_type].d_name, np->nat_cnam);
404 if (desig == SCT_ENLIST && neweff >= 60 &&
405 sp->sct_own == sp->sct_oldown) {
406 p_sect[desig][0] += enlist(vec, etu, &ecost);
407 p_sect[desig][1] += ecost;
408 if (!player->simulation)
409 np->nat_money -= ecost;
413 * now do the production (if sector effic >= 60%)
417 if (np->nat_money > 0 && dchr[desig].d_prd)
419 produce(np, sp, vec, work, desig, neweff, &pcost, &amount);
422 pt_bg_nmbr(bp, sp, I_MAX + 1, work);
423 p_sect[desig][0] += amount;
424 p_sect[desig][1] += pcost;
425 if (!player->simulation) {
426 maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
427 if (vec[I_CIVIL] > maxpop)
428 vec[I_CIVIL] = maxpop;
429 if (vec[I_UW] > maxpop)
431 sp->sct_avail = work;
432 np->nat_money -= pcost;