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)
68 if (sp->sct_mobil <= 0)
71 plague = sp->sct_pstage;
72 for (i = 1; i <= I_MAX; i++) {
73 if (sp->sct_del[i] == 0)
75 thresh = sp->sct_del[i] & ~0x7;
76 dir = sp->sct_del[i] & 0x7;
77 n = deliver(sp, &ichr[i], dir, thresh, vec[i], plague);
81 if (sp->sct_mobil <= 0)
89 * Increase sector efficiency if old type == new type.
90 * decrease sector efficiency if old type != new type.
91 * Return amount of work used.
94 upd_buildeff(struct natstr *np, register struct sctstr *sp, int *workp,
95 int *vec, int etu, int *desig, int sctwork, int *cost)
97 register int work_cost = 0;
98 int buildeff_work = (int)(*workp / 2);
99 int n, hcms, lcms, neweff;
100 u_char old_type = *desig;
103 neweff = sp->sct_effic;
105 if (*desig != sp->sct_newtype) {
107 * Tear down existing sector.
108 * Easier to destroy than to build.
110 work_cost = (sp->sct_effic + 3) / 4;
111 if (work_cost > buildeff_work)
112 work_cost = buildeff_work;
113 buildeff_work -= work_cost;
114 n = sp->sct_effic - work_cost * 4;
117 *desig = sp->sct_newtype;
122 if (!n && dchr[old_type].d_pkg == UPKG &&
123 dchr[*desig].d_pkg != UPKG) {
124 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
125 if (vec[I_CIVIL] > maxpop)
126 vec[I_CIVIL] = maxpop;
127 if (vec[I_UW] > maxpop)
129 *workp = (vec[I_CIVIL] * sctwork) / 100.0
130 + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
131 *workp = roundavg((etu * (*workp)) / 100.0);
133 buildeff_work = min((int)(*workp / 2), buildeff_work);
137 if (np->nat_priorities[*desig]) {
138 if (*desig == sp->sct_newtype) {
139 work_cost = 100 - neweff;
140 if (work_cost > buildeff_work)
141 work_cost = buildeff_work;
143 if (dchr[*desig].d_lcms > 0) {
145 lcms /= dchr[*desig].d_lcms;
146 if (work_cost > lcms)
149 if (dchr[*desig].d_hcms > 0) {
151 hcms /= dchr[*desig].d_hcms;
152 if (work_cost > hcms)
157 *cost += work_cost * dchr[*desig].d_build;
158 buildeff_work -= work_cost;
160 if ((dchr[*desig].d_lcms > 0) || (dchr[*desig].d_hcms > 0)) {
161 vec[I_LCM] -= work_cost * dchr[*desig].d_lcms;
162 vec[I_HCM] -= work_cost * dchr[*desig].d_hcms;
166 *workp = *workp / 2 + buildeff_work;
172 * enlistment sectors are special; they require military
173 * to convert civ into mil in large numbers.
174 * Conversion will happen much more slowly without
175 * some mil initially.
178 enlist(register int *vec, int etu, int *cost)
183 /* Need to check treaties here */
185 maxmil = (vec[I_CIVIL] / 2) - vec[I_MILIT];
187 enlisted = (etu * (10 + vec[I_MILIT]) * 0.05);
188 if (enlisted > maxmil)
190 vec[I_CIVIL] -= enlisted;
191 vec[I_MILIT] += enlisted;
193 *cost = enlisted * 3;
197 /* Fallout is calculated here. */
200 meltitems(int etus, int fallout, int own, int *vec, int type, int x, int y,
206 for (n = 1; n <= I_MAX; n++) {
207 melt = roundavg(vec[n] * etus * (long)fallout /
208 (1000.0 * melt_item_denom[n]));
209 if (melt > 5 && own) {
210 if (type == EF_SECTOR)
211 wu(0, own, "Lost %d %s to radiation in %s.\n",
212 (melt < vec[n] ? melt : vec[n]), ichr[n].i_name,
214 else if (type == EF_LAND)
215 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
216 uid, (melt < vec[n] ? melt : vec[n]), ichr[n].i_name,
218 else if (type == EF_SHIP)
219 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
220 uid, (melt < vec[n] ? melt : vec[n]), ichr[n].i_name,
231 * do_fallout - calculate fallout for sectors.
233 * This is etu based. But, do limit HUGE kill offs in large ETU
234 * games, the melting etus rate is limited to 24 etus.
238 do_fallout(register struct sctstr *sp, register int etus)
246 getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
247 /* This check shouldn't be needed, but just in case. :) */
248 if (!sp->sct_fallout || !sp->sct_updated)
253 wu(0, 0, "Running fallout in %d,%d\n", sp->sct_x, sp->sct_y);
255 meltitems(etus, sp->sct_fallout, sp->sct_own, vec, EF_SECTOR,
256 sp->sct_x, sp->sct_y, 0);
257 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
258 for (i = 0; NULL != (lp = getlandp(i)); i++) {
261 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
263 getvec(VT_ITEM, tvec, (s_char *)lp, EF_LAND);
264 meltitems(etus, sp->sct_fallout, lp->lnd_own, tvec, EF_LAND,
265 lp->lnd_x, lp->lnd_y, lp->lnd_uid);
266 putvec(VT_ITEM, tvec, (s_char *)lp, EF_LAND);
268 for (i = 0; NULL != (spp = getshipp(i)); i++) {
271 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
273 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
275 getvec(VT_ITEM, tvec, (s_char *)spp, EF_SHIP);
276 meltitems(etus, sp->sct_fallout, spp->shp_own, tvec, EF_SHIP,
277 spp->shp_x, spp->shp_y, spp->shp_uid);
278 putvec(VT_ITEM, tvec, (s_char *)spp, EF_SHIP);
281 if ((sp->sct_fallout > 20) && chance(100))
283 #endif /* GODZILLA */
287 spread_fallout(struct sctstr *sp, int etus)
295 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
296 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
297 if (ap->sct_type == SCT_SANCT)
299 inc = roundavg(etus * fallout_spread * (sp->sct_fallout)) - 1;
301 if (sp->sct_fallout) {
302 wu(0, 0, "Fallout from sector %d,%d to %d,%d is %d=%d*%e*%d\n",
303 sp->sct_x, sp->sct_y, sp->sct_x + diroff[n][0],
304 sp->sct_y + diroff[n][1], inc, etus,
305 fallout_spread, sp->sct_fallout);
310 ap->sct_fallout += inc;
315 decay_fallout(struct sctstr *sp, int etus)
321 decay = roundavg(((decay_per_etu + 6.0) * fallout_spread) *
322 (double)etus * (double)sp->sct_fallout);
325 if (decay || sp->sct_fallout)
326 wu(0, 0, "Fallout decay in %d,%d is %d from %d\n", sp->sct_x,
327 sp->sct_y, decay, sp->sct_fallout);
331 (decay < sp->sct_fallout) ? (sp->sct_fallout - decay) : 0;
334 #define SHOULD_PRODUCE(sp,t) (((sp->sct_type == t) || (t == -1)) ? 1 : 0)
337 * Produce only a set sector type for a specific nation
338 * (or all, if sector_type == -1)
342 produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
345 register struct sctstr *sp;
346 register struct natstr *np;
348 int work, cost, ecost, pcost, sctwork;
349 int n, desig, maxpop, neweff, amount;
351 for (n = 0; NULL != (sp = getsectid(n)); n++) {
352 if (sp->sct_type == SCT_WATER)
354 if (sp->sct_own != natnum)
356 if (sp->sct_updated != 0)
358 if (!SHOULD_PRODUCE(sp, sector_type))
361 if ((sp->sct_type == SCT_CAPIT) && (sp->sct_effic > 60)) {
362 p_sect[SCT_CAPIT][0]++;
363 p_sect[SCT_CAPIT][1] += etu;
366 if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
368 /* If everybody is dead, the sector reverts to unowned.
369 * This is also checked at the end of the production in
370 * they all starved or were plagued off.
372 if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
373 !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
374 makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
383 np = getnatp(natnum);
385 /* do_feed trys to supply. So, we need to enable cacheing
387 bp_enable_cachepath();
389 sctwork = do_feed(sp, np, vec, &work, bp, etu);
391 bp_disable_cachepath();
392 bp_clear_cachepath();
394 if (sp->sct_off || np->nat_money < 0) {
395 if (!player->simulation) {
396 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
401 if ((np->nat_priorities[sp->sct_type] == 0) &&
402 (sp->sct_type == sp->sct_newtype) &&
403 ((pchr[dchr[sp->sct_type].d_prd].p_cost != 0) ||
404 (sp->sct_type == SCT_ENLIST))) {
405 if (!player->simulation) {
406 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
407 logerror("Skipping %s production for country %s\n",
408 dchr[sp->sct_type].d_name, np->nat_cnam);
413 neweff = sp->sct_effic;
415 pcost = cost = ecost = 0;
417 desig = sp->sct_type;
419 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
422 upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
424 pt_bg_nmbr(bp, sp, I_LCM, vec[I_LCM]);
425 pt_bg_nmbr(bp, sp, I_HCM, vec[I_HCM]);
426 p_sect[SCT_EFFIC][0]++;
427 p_sect[SCT_EFFIC][1] += cost;
428 if (!player->simulation) {
429 np->nat_money -= cost;
430 /* No longer tear down infrastructure
431 if (sp->sct_type != desig) {
434 } else if (neweff < sp->sct_effic) {
435 sp->sct_road -= (sp->sct_road * (sp->sct_effic - neweff) / 100.0);
436 sp->sct_defense -= (sp->sct_defense * (sp->sct_effic - neweff) / 100.0);
437 if (sp->sct_road < 0)
439 if (sp->sct_defense < 0)
443 sp->sct_type = desig;
444 sp->sct_effic = neweff;
445 if (!opt_DEFENSE_INFRA)
446 sp->sct_defense = sp->sct_effic;
450 if ((np->nat_priorities[desig] == 0) &&
451 ((pchr[dchr[desig].d_prd].p_cost != 0) ||
452 (desig == SCT_ENLIST))) {
453 if (!player->simulation) {
454 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
455 logerror("Skipping %s production for country %s\n",
456 dchr[sp->sct_type].d_name, np->nat_cnam);
461 if (desig == SCT_ENLIST && neweff >= 60 &&
462 sp->sct_own == sp->sct_oldown) {
463 p_sect[desig][0] += enlist(vec, etu, &ecost);
464 p_sect[desig][1] += ecost;
465 if (!player->simulation)
466 np->nat_money -= ecost;
470 * now do the production (if sector effic >= 60%)
474 if (np->nat_money > 0 && dchr[desig].d_prd)
476 produce(np, sp, vec, work, desig, neweff, &pcost, &amount);
479 pt_bg_nmbr(bp, sp, I_MAX + 1, work);
480 p_sect[desig][0] += amount;
481 p_sect[desig][1] += pcost;
482 if (!player->simulation) {
483 maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
484 if (vec[I_CIVIL] > maxpop)
485 vec[I_CIVIL] = maxpop;
486 if (vec[I_UW] > maxpop)
488 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
489 sp->sct_avail = work;
490 np->nat_money -= pcost;