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 dodeliver(struct sctstr *sp, int *vec)
69 if (sp->sct_mobil <= 0)
71 if (getvec(VT_DEL, del, (s_char *)sp, EF_SECTOR) <= 0)
74 plague = getvar(V_PSTAGE, (s_char *)sp, EF_SECTOR);
75 for (i = 1; i <= I_MAX; i++) {
78 thresh = del[i] & ~0x7;
80 n = deliver(sp, &ichr[i], dir, thresh, vec[i], plague);
84 if (sp->sct_mobil <= 0)
92 * Increase sector efficiency if old type == new type.
93 * decrease sector efficiency if old type != new type.
94 * Return amount of work used.
97 upd_buildeff(struct natstr *np, register struct sctstr *sp, int *workp,
98 int *vec, int etu, int *desig, int sctwork, int *cost)
100 register int work_cost = 0;
101 int buildeff_work = (int)(*workp / 2);
102 int n, hcms, lcms, neweff;
103 u_char old_type = *desig;
106 neweff = sp->sct_effic;
108 if (*desig != sp->sct_newtype) {
110 * Tear down existing sector.
111 * Easier to destroy than to build.
113 work_cost = (sp->sct_effic + 3) / 4;
114 if (work_cost > buildeff_work)
115 work_cost = buildeff_work;
116 buildeff_work -= work_cost;
117 n = sp->sct_effic - work_cost * 4;
120 *desig = sp->sct_newtype;
125 if (!n && dchr[old_type].d_pkg == UPKG &&
126 dchr[*desig].d_pkg != UPKG) {
127 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
128 if (vec[I_CIVIL] > maxpop)
129 vec[I_CIVIL] = maxpop;
130 if (vec[I_UW] > maxpop)
132 *workp = (vec[I_CIVIL] * sctwork) / 100.0
133 + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
134 *workp = roundavg((etu * (*workp)) / 100.0);
136 buildeff_work = min((int)(*workp / 2), buildeff_work);
140 if (np->nat_priorities[*desig]) {
141 if (*desig == sp->sct_newtype) {
142 work_cost = 100 - neweff;
143 if (work_cost > buildeff_work)
144 work_cost = buildeff_work;
146 if (dchr[*desig].d_lcms > 0) {
148 lcms /= dchr[*desig].d_lcms;
149 if (work_cost > lcms)
152 if (dchr[*desig].d_hcms > 0) {
154 hcms /= dchr[*desig].d_hcms;
155 if (work_cost > hcms)
160 *cost += work_cost * dchr[*desig].d_build;
161 buildeff_work -= work_cost;
163 if ((dchr[*desig].d_lcms > 0) || (dchr[*desig].d_hcms > 0)) {
164 vec[I_LCM] -= work_cost * dchr[*desig].d_lcms;
165 vec[I_HCM] -= work_cost * dchr[*desig].d_hcms;
169 *workp = *workp / 2 + buildeff_work;
175 * enlistment sectors are special; they require military
176 * to convert civ into mil in large numbers.
177 * Conversion will happen much more slowly without
178 * some mil initially.
181 enlist(register int *vec, int etu, int *cost)
186 /* Need to check treaties here */
188 maxmil = (vec[I_CIVIL] / 2) - vec[I_MILIT];
190 enlisted = (etu * (10 + vec[I_MILIT]) * 0.05);
191 if (enlisted > maxmil)
193 vec[I_CIVIL] -= enlisted;
194 vec[I_MILIT] += enlisted;
196 *cost = enlisted * 3;
200 /* Fallout is calculated here. */
203 meltitems(int etus, int fallout, int own, int *vec, int type, int x, int y,
209 for (n = 1; n <= I_MAX; n++) {
210 melt = roundavg(vec[n] * etus * (long)fallout /
211 (1000.0 * melt_item_denom[n]));
212 if (melt > 5 && own) {
213 if (type == EF_SECTOR)
214 wu(0, own, "Lost %d %s to radiation in %s.\n",
215 (melt < vec[n] ? melt : vec[n]), ichr[n].i_name,
217 else if (type == EF_LAND)
218 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
219 uid, (melt < vec[n] ? melt : vec[n]), ichr[n].i_name,
221 else if (type == EF_SHIP)
222 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
223 uid, (melt < vec[n] ? melt : vec[n]), ichr[n].i_name,
234 * do_fallout - calculate fallout for sectors.
236 * This is etu based. But, do limit HUGE kill offs in large ETU
237 * games, the melting etus rate is limited to 24 etus.
241 do_fallout(register struct sctstr *sp, register int etus)
250 getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
251 getvec(VT_COND, cvec, (s_char *)sp, EF_SECTOR);
252 /* This check shouldn't be needed, but just in case. :) */
253 if (!cvec[C_FALLOUT] || !sp->sct_updated)
258 wu(0, 0, "Running fallout in %d,%d\n", sp->sct_x, sp->sct_y);
260 meltitems(etus, cvec[C_FALLOUT], sp->sct_own, vec, EF_SECTOR,
261 sp->sct_x, sp->sct_y, 0);
262 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
263 for (i = 0; NULL != (lp = getlandp(i)); i++) {
266 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
268 getvec(VT_ITEM, tvec, (s_char *)lp, EF_LAND);
269 meltitems(etus, cvec[C_FALLOUT], lp->lnd_own, tvec, EF_LAND,
270 lp->lnd_x, lp->lnd_y, lp->lnd_uid);
271 putvec(VT_ITEM, tvec, (s_char *)lp, EF_LAND);
273 for (i = 0; NULL != (spp = getshipp(i)); i++) {
276 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
278 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
280 getvec(VT_ITEM, tvec, (s_char *)spp, EF_SHIP);
281 meltitems(etus, cvec[C_FALLOUT], spp->shp_own, tvec, EF_SHIP,
282 spp->shp_x, spp->shp_y, spp->shp_uid);
283 putvec(VT_ITEM, tvec, (s_char *)spp, EF_SHIP);
286 if ((cvec[C_FALLOUT] > 20) && chance(100))
288 #endif /* GODZILLA */
292 spread_fallout(struct sctstr *sp, int etus)
302 getvec(VT_COND, cvec, (s_char *)sp, EF_SECTOR);
303 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
304 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
305 getvec(VT_COND, tvec, (char *)ap, EF_SECTOR);
306 if (ap->sct_type == SCT_SANCT)
308 inc = roundavg(etus * fallout_spread * (cvec[C_FALLOUT])) - 1;
310 if (cvec[C_FALLOUT]) {
311 wu(0, 0, "Fallout from sector %d,%d to %d,%d is %d=%d*%e*%d\n",
312 sp->sct_x, sp->sct_y, sp->sct_x + diroff[n][0],
313 sp->sct_y + diroff[n][1], inc, etus,
314 fallout_spread, cvec[C_FALLOUT]);
319 tvec[C_FALLOUT] += inc;
320 putvec(VT_COND, tvec, (char *)ap, EF_SECTOR);
325 decay_fallout(struct sctstr *sp, int etus)
332 getvec(VT_COND, cvec, (char *)sp, EF_SECTOR);
333 decay = roundavg(((decay_per_etu + 6.0) * fallout_spread) *
334 (double)etus * (double)cvec[C_FALLOUT]);
337 if (decay || cvec[C_FALLOUT])
338 wu(0, 0, "Fallout decay in %d,%d is %d from %d\n", sp->sct_x,
339 sp->sct_y, decay, cvec[C_FALLOUT]);
343 (decay < cvec[C_FALLOUT]) ? (cvec[C_FALLOUT] - decay) : 0;
344 if (cvec[C_FALLOUT] < 0)
346 putvec(VT_COND, cvec, (s_char *)sp, EF_SECTOR);
349 #define SHOULD_PRODUCE(sp,t) (((sp->sct_type == t) || (t == -1)) ? 1 : 0)
352 * Produce only a set sector type for a specific nation
353 * (or all, if sector_type == -1)
357 produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
360 register struct sctstr *sp;
361 register struct natstr *np;
363 int work, cost, ecost, pcost, sctwork;
364 int n, desig, maxpop, neweff, amount;
366 for (n = 0; NULL != (sp = getsectid(n)); n++) {
367 if (sp->sct_type == SCT_WATER)
369 if (sp->sct_own != natnum)
371 if (sp->sct_updated != 0)
373 if (!SHOULD_PRODUCE(sp, sector_type))
376 if ((sp->sct_type == SCT_CAPIT) && (sp->sct_effic > 60)) {
377 p_sect[SCT_CAPIT][0]++;
378 p_sect[SCT_CAPIT][1] += etu;
381 if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
383 /* If everybody is dead, the sector reverts to unowned.
384 * This is also checked at the end of the production in
385 * they all starved or were plagued off.
387 if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
388 !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
389 makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
398 np = getnatp(natnum);
400 /* do_feed trys to supply. So, we need to enable cacheing
402 bp_enable_cachepath();
404 sctwork = do_feed(sp, np, vec, &work, bp, etu);
406 bp_disable_cachepath();
407 bp_clear_cachepath();
409 if (sp->sct_off || np->nat_money < 0) {
410 if (!player->simulation) {
411 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
416 if ((np->nat_priorities[sp->sct_type] == 0) &&
417 (sp->sct_type == sp->sct_newtype) &&
418 ((pchr[dchr[sp->sct_type].d_prd].p_cost != 0) ||
419 (sp->sct_type == SCT_ENLIST))) {
420 if (!player->simulation) {
421 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
422 logerror("Skipping %s production for country %s\n",
423 dchr[sp->sct_type].d_name, np->nat_cnam);
428 neweff = sp->sct_effic;
430 pcost = cost = ecost = 0;
432 desig = sp->sct_type;
434 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
437 upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
439 pt_bg_nmbr(bp, sp, I_LCM, vec[I_LCM]);
440 pt_bg_nmbr(bp, sp, I_HCM, vec[I_HCM]);
441 p_sect[SCT_EFFIC][0]++;
442 p_sect[SCT_EFFIC][1] += cost;
443 if (!player->simulation) {
444 np->nat_money -= cost;
445 /* No longer tear down infrastructure
446 if (sp->sct_type != desig) {
449 } else if (neweff < sp->sct_effic) {
450 sp->sct_road -= (sp->sct_road * (sp->sct_effic - neweff) / 100.0);
451 sp->sct_defense -= (sp->sct_defense * (sp->sct_effic - neweff) / 100.0);
452 if (sp->sct_road < 0)
454 if (sp->sct_defense < 0)
458 sp->sct_type = desig;
459 sp->sct_effic = neweff;
460 if (!opt_DEFENSE_INFRA)
461 sp->sct_defense = sp->sct_effic;
465 if ((np->nat_priorities[desig] == 0) &&
466 ((pchr[dchr[desig].d_prd].p_cost != 0) ||
467 (desig == SCT_ENLIST))) {
468 if (!player->simulation) {
469 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
470 logerror("Skipping %s production for country %s\n",
471 dchr[sp->sct_type].d_name, np->nat_cnam);
476 if (desig == SCT_ENLIST && neweff >= 60 &&
477 sp->sct_own == sp->sct_oldown) {
478 p_sect[desig][0] += enlist(vec, etu, &ecost);
479 p_sect[desig][1] += ecost;
480 if (!player->simulation)
481 np->nat_money -= ecost;
485 * now do the production (if sector effic >= 60%)
489 if (np->nat_money > 0 && dchr[desig].d_prd)
491 produce(np, sp, vec, work, sctwork, desig, neweff,
495 pt_bg_nmbr(bp, sp, I_MAX + 1, work);
496 p_sect[desig][0] += amount;
497 p_sect[desig][1] += pcost;
498 if (!player->simulation) {
499 maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
500 if (vec[I_CIVIL] > maxpop)
501 vec[I_CIVIL] = maxpop;
502 if (vec[I_UW] > maxpop)
504 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
505 sp->sct_avail = work;
506 np->nat_money -= pcost;