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)
67 if (sp->sct_mobil <= 0)
69 plague = sp->sct_pstage;
70 for (i = 1; i <= I_MAX; i++) {
71 if (sp->sct_del[i] == 0)
73 thresh = sp->sct_del[i] & ~0x7;
74 dir = sp->sct_del[i] & 0x7;
75 n = deliver(sp, &ichr[i], dir, thresh, sp->sct_item[i], plague);
78 if (sp->sct_mobil <= 0)
85 * Increase sector efficiency if old type == new type.
86 * decrease sector efficiency if old type != new type.
87 * Return amount of work used.
90 upd_buildeff(struct natstr *np, register struct sctstr *sp, int *workp,
91 int *vec, int etu, int *desig, int sctwork, int *cost)
93 register int work_cost = 0;
94 int buildeff_work = (int)(*workp / 2);
95 int n, hcms, lcms, neweff;
96 u_char old_type = *desig;
99 neweff = sp->sct_effic;
101 if (*desig != sp->sct_newtype) {
103 * Tear down existing sector.
104 * Easier to destroy than to build.
106 work_cost = (sp->sct_effic + 3) / 4;
107 if (work_cost > buildeff_work)
108 work_cost = buildeff_work;
109 buildeff_work -= work_cost;
110 n = sp->sct_effic - work_cost * 4;
113 *desig = sp->sct_newtype;
118 if (!n && dchr[old_type].d_pkg == UPKG &&
119 dchr[*desig].d_pkg != UPKG) {
120 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
121 if (vec[I_CIVIL] > maxpop)
122 vec[I_CIVIL] = maxpop;
123 if (vec[I_UW] > maxpop)
125 *workp = (vec[I_CIVIL] * sctwork) / 100.0
126 + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
127 *workp = roundavg((etu * (*workp)) / 100.0);
129 buildeff_work = min((int)(*workp / 2), buildeff_work);
133 if (np->nat_priorities[*desig]) {
134 if (*desig == sp->sct_newtype) {
135 work_cost = 100 - neweff;
136 if (work_cost > buildeff_work)
137 work_cost = buildeff_work;
139 if (dchr[*desig].d_lcms > 0) {
141 lcms /= dchr[*desig].d_lcms;
142 if (work_cost > lcms)
145 if (dchr[*desig].d_hcms > 0) {
147 hcms /= dchr[*desig].d_hcms;
148 if (work_cost > hcms)
153 *cost += work_cost * dchr[*desig].d_build;
154 buildeff_work -= work_cost;
156 if ((dchr[*desig].d_lcms > 0) || (dchr[*desig].d_hcms > 0)) {
157 vec[I_LCM] -= work_cost * dchr[*desig].d_lcms;
158 vec[I_HCM] -= work_cost * dchr[*desig].d_hcms;
162 *workp = *workp / 2 + buildeff_work;
168 * enlistment sectors are special; they require military
169 * to convert civ into mil in large numbers.
170 * Conversion will happen much more slowly without
171 * some mil initially.
174 enlist(register int *vec, int etu, int *cost)
179 /* Need to check treaties here */
181 maxmil = (vec[I_CIVIL] / 2) - vec[I_MILIT];
183 enlisted = (etu * (10 + vec[I_MILIT]) * 0.05);
184 if (enlisted > maxmil)
186 vec[I_CIVIL] -= enlisted;
187 vec[I_MILIT] += enlisted;
189 *cost = enlisted * 3;
193 /* Fallout is calculated here. */
196 meltitems(int etus, int fallout, int own, short *vec, int type, int x, int y,
202 for (n = 1; n <= I_MAX; n++) {
203 melt = roundavg(vec[n] * etus * (long)fallout
204 / (1000.0 * melt_item_denom[n]));
207 if (melt > 5 && own) {
208 if (type == EF_SECTOR)
209 wu(0, own, "Lost %d %s to radiation in %s.\n",
210 melt < vec[n] ? melt : vec[n], ichr[n].i_name,
212 else if (type == EF_LAND)
213 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
214 uid, melt < vec[n] ? melt : vec[n], ichr[n].i_name,
216 else if (type == EF_SHIP)
217 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
218 uid, melt < vec[n] ? melt : vec[n], ichr[n].i_name,
226 * do_fallout - calculate fallout for sectors.
228 * This is etu based. But, do limit HUGE kill offs in large ETU
229 * games, the melting etus rate is limited to 24 etus.
233 do_fallout(register struct sctstr *sp, register int etus)
239 /* This check shouldn't be needed, but just in case. :) */
240 if (!sp->sct_fallout || !sp->sct_updated)
244 meltitems(etus, sp->sct_fallout, sp->sct_own, sp->sct_item, EF_SECTOR,
245 sp->sct_x, sp->sct_y, 0);
246 for (i = 0; NULL != (lp = getlandp(i)); i++) {
249 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
251 meltitems(etus, sp->sct_fallout, lp->lnd_own, lp->lnd_item, EF_LAND,
252 lp->lnd_x, lp->lnd_y, lp->lnd_uid);
254 for (i = 0; NULL != (spp = getshipp(i)); i++) {
257 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
259 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
261 meltitems(etus, sp->sct_fallout, spp->shp_own, spp->shp_item, EF_SHIP,
262 spp->shp_x, spp->shp_y, spp->shp_uid);
265 if ((sp->sct_fallout > 20) && chance(100))
267 #endif /* GODZILLA */
271 spread_fallout(struct sctstr *sp, int etus)
279 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
280 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
281 if (ap->sct_type == SCT_SANCT)
283 inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
285 if (sp->sct_fallout) {
286 wu(0, 0, "Fallout from sector %d,%d to %d,%d is %d=%d*%e*%d\n",
287 sp->sct_x, sp->sct_y, sp->sct_x + diroff[n][0],
288 sp->sct_y + diroff[n][1], inc, etus,
289 fallout_spread, sp->sct_fallout);
294 ap->sct_fallout = min(ap->sct_fallout + inc, FALLOUT_MAX);
299 decay_fallout(struct sctstr *sp, int etus)
305 decay = roundavg(((decay_per_etu + 6.0) * fallout_spread) *
306 (double)etus * (double)sp->sct_fallout);
309 if (decay || sp->sct_fallout)
310 wu(0, 0, "Fallout decay in %d,%d is %d from %d\n", sp->sct_x,
311 sp->sct_y, decay, sp->sct_fallout);
314 sp->sct_fallout = decay < sp->sct_fallout ? sp->sct_fallout - decay : 0;
317 #define SHOULD_PRODUCE(sp,t) (((sp->sct_type == t) || (t == -1)) ? 1 : 0)
320 * Produce only a set sector type for a specific nation
321 * (or all, if sector_type == -1)
325 produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
328 register struct sctstr *sp;
329 register struct natstr *np;
331 int work, cost, ecost, pcost, sctwork;
332 int n, desig, maxpop, neweff, amount;
334 for (n = 0; NULL != (sp = getsectid(n)); n++) {
335 if (sp->sct_type == SCT_WATER)
337 if (sp->sct_own != natnum)
339 if (sp->sct_updated != 0)
341 if (!SHOULD_PRODUCE(sp, sector_type))
344 if ((sp->sct_type == SCT_CAPIT) && (sp->sct_effic > 60)) {
345 p_sect[SCT_CAPIT][0]++;
346 p_sect[SCT_CAPIT][1] += etu;
349 if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
351 /* If everybody is dead, the sector reverts to unowned.
352 * This is also checked at the end of the production in
353 * they all starved or were plagued off.
355 if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
356 !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
357 makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
366 np = getnatp(natnum);
368 /* do_feed trys to supply. So, we need to enable cacheing
370 bp_enable_cachepath();
372 sctwork = do_feed(sp, np, vec, &work, bp, etu);
374 bp_disable_cachepath();
375 bp_clear_cachepath();
377 if (sp->sct_off || np->nat_money < 0) {
378 if (!player->simulation) {
379 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
384 if ((np->nat_priorities[sp->sct_type] == 0) &&
385 (sp->sct_type == sp->sct_newtype) &&
386 ((pchr[dchr[sp->sct_type].d_prd].p_cost != 0) ||
387 (sp->sct_type == SCT_ENLIST))) {
388 if (!player->simulation) {
389 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
390 logerror("Skipping %s production for country %s\n",
391 dchr[sp->sct_type].d_name, np->nat_cnam);
396 neweff = sp->sct_effic;
398 pcost = cost = ecost = 0;
400 desig = sp->sct_type;
402 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
405 upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
407 pt_bg_nmbr(bp, sp, I_LCM, vec[I_LCM]);
408 pt_bg_nmbr(bp, sp, I_HCM, vec[I_HCM]);
409 p_sect[SCT_EFFIC][0]++;
410 p_sect[SCT_EFFIC][1] += cost;
411 if (!player->simulation) {
412 np->nat_money -= cost;
413 /* No longer tear down infrastructure
414 if (sp->sct_type != desig) {
417 } else if (neweff < sp->sct_effic) {
418 sp->sct_road -= (sp->sct_road * (sp->sct_effic - neweff) / 100.0);
419 sp->sct_defense -= (sp->sct_defense * (sp->sct_effic - neweff) / 100.0);
420 if (sp->sct_road < 0)
422 if (sp->sct_defense < 0)
426 sp->sct_type = desig;
427 sp->sct_effic = neweff;
428 if (!opt_DEFENSE_INFRA)
429 sp->sct_defense = sp->sct_effic;
433 if ((np->nat_priorities[desig] == 0) &&
434 ((pchr[dchr[desig].d_prd].p_cost != 0) ||
435 (desig == SCT_ENLIST))) {
436 if (!player->simulation) {
437 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
438 logerror("Skipping %s production for country %s\n",
439 dchr[sp->sct_type].d_name, np->nat_cnam);
444 if (desig == SCT_ENLIST && neweff >= 60 &&
445 sp->sct_own == sp->sct_oldown) {
446 p_sect[desig][0] += enlist(vec, etu, &ecost);
447 p_sect[desig][1] += ecost;
448 if (!player->simulation)
449 np->nat_money -= ecost;
453 * now do the production (if sector effic >= 60%)
457 if (np->nat_money > 0 && dchr[desig].d_prd)
459 produce(np, sp, vec, work, desig, neweff, &pcost, &amount);
462 pt_bg_nmbr(bp, sp, I_MAX + 1, work);
463 p_sect[desig][0] += amount;
464 p_sect[desig][1] += pcost;
465 if (!player->simulation) {
466 maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
467 if (vec[I_CIVIL] > maxpop)
468 vec[I_CIVIL] = maxpop;
469 if (vec[I_UW] > maxpop)
471 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
472 sp->sct_avail = work;
473 np->nat_money -= pcost;