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"
58 extern float levels[MAXNOC][4];
61 dodeliver(struct sctstr *sp, int *vec)
71 if (sp->sct_mobil <= 0)
73 if (getvec(VT_DEL, del, (s_char *)sp, EF_SECTOR) <= 0)
76 plague = getvar(V_PSTAGE, (s_char *)sp, EF_SECTOR);
77 for (i = 1; i <= I_MAX; i++) {
80 thresh = del[i] & ~0x7;
82 n = deliver(sp, &ichr[i], dir, thresh, vec[i], plague);
86 if (sp->sct_mobil <= 0)
94 * Increase sector efficiency if old type == new type.
95 * decrease sector efficiency if old type != new type.
96 * Return amount of work used.
99 upd_buildeff(struct natstr *np, register struct sctstr *sp, int *workp,
100 int *vec, int etu, int *desig, int sctwork, int *cost)
102 register int work_cost = 0;
103 int buildeff_work = (int)(*workp / 2);
104 int n, hcms, lcms, neweff;
105 u_char old_type = *desig;
108 neweff = sp->sct_effic;
110 if (*desig != sp->sct_newtype) {
112 * Tear down existing sector.
113 * Easier to destroy than to build.
115 work_cost = (sp->sct_effic + 3) / 4;
116 if (work_cost > buildeff_work)
117 work_cost = buildeff_work;
118 buildeff_work -= work_cost;
119 n = sp->sct_effic - work_cost * 4;
122 *desig = sp->sct_newtype;
127 if (!n && dchr[old_type].d_pkg == UPKG &&
128 dchr[*desig].d_pkg != UPKG) {
129 int maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
130 if (vec[I_CIVIL] > maxpop)
131 vec[I_CIVIL] = maxpop;
132 if (vec[I_UW] > maxpop)
134 *workp = (vec[I_CIVIL] * sctwork) / 100.0
135 + (vec[I_MILIT] * 2 / 5.0) + vec[I_UW];
136 *workp = roundavg((etu * (*workp)) / 100.0);
138 buildeff_work = min((int)(*workp / 2), buildeff_work);
142 if (np->nat_priorities[*desig]) {
143 if (*desig == sp->sct_newtype) {
144 work_cost = 100 - neweff;
145 if (work_cost > buildeff_work)
146 work_cost = buildeff_work;
148 if (dchr[*desig].d_lcms > 0) {
150 lcms /= dchr[*desig].d_lcms;
151 if (work_cost > lcms)
154 if (dchr[*desig].d_hcms > 0) {
156 hcms /= dchr[*desig].d_hcms;
157 if (work_cost > hcms)
162 *cost += work_cost * dchr[*desig].d_build;
163 buildeff_work -= work_cost;
165 if ((dchr[*desig].d_lcms > 0) || (dchr[*desig].d_hcms > 0)) {
166 vec[I_LCM] -= work_cost * dchr[*desig].d_lcms;
167 vec[I_HCM] -= work_cost * dchr[*desig].d_hcms;
171 *workp = *workp / 2 + buildeff_work;
177 * enlistment sectors are special; they require military
178 * to convert civ into mil in large numbers.
179 * Conversion will happen much more slowly without
180 * some mil initially.
183 enlist(register int *vec, int etu, int *cost)
188 /* Need to check treaties here */
190 maxmil = (vec[I_CIVIL] / 2) - vec[I_MILIT];
192 enlisted = (etu * (10 + vec[I_MILIT]) * 0.05);
193 if (enlisted > maxmil)
195 vec[I_CIVIL] -= enlisted;
196 vec[I_MILIT] += enlisted;
198 *cost = enlisted * 3;
202 /* Fallout is calculated here. */
204 extern int melt_item_denom[];
207 meltitems(int etus, int fallout, int own, int *vec, int type, int x, int y,
213 for (n = 1; n <= I_MAX; n++) {
214 melt = roundavg(vec[n] * etus * (long)fallout /
215 (1000.0 * melt_item_denom[n]));
216 if (melt > 5 && own) {
217 if (type == EF_SECTOR)
218 wu(0, own, "Lost %d %s to radiation in %s.\n",
219 (melt < vec[n] ? melt : vec[n]), ichr[n].i_name,
221 else if (type == EF_LAND)
222 wu(0, own, "Unit #%d lost %d %s to radiation in %s.\n",
223 uid, (melt < vec[n] ? melt : vec[n]), ichr[n].i_name,
225 else if (type == EF_SHIP)
226 wu(0, own, "Ship #%d lost %d %s to radiation in %s.\n",
227 uid, (melt < vec[n] ? melt : vec[n]), ichr[n].i_name,
238 * do_fallout - calculate fallout for sectors.
240 * This is etu based. But, do limit HUGE kill offs in large ETU
241 * games, the melting etus rate is limited to 24 etus.
245 do_fallout(register struct sctstr *sp, register int etus)
254 getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
255 getvec(VT_COND, cvec, (s_char *)sp, EF_SECTOR);
256 /* This check shouldn't be needed, but just in case. :) */
257 if (!cvec[C_FALLOUT] || !sp->sct_updated)
262 wu(0, 0, "Running fallout in %d,%d\n", sp->sct_x, sp->sct_y);
264 meltitems(etus, cvec[C_FALLOUT], sp->sct_own, vec, EF_SECTOR,
265 sp->sct_x, sp->sct_y, 0);
266 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
267 for (i = 0; NULL != (lp = getlandp(i)); i++) {
270 if (lp->lnd_x != sp->sct_x || lp->lnd_y != sp->sct_y)
272 getvec(VT_ITEM, tvec, (s_char *)lp, EF_LAND);
273 meltitems(etus, cvec[C_FALLOUT], lp->lnd_own, tvec, EF_LAND,
274 lp->lnd_x, lp->lnd_y, lp->lnd_uid);
275 putvec(VT_ITEM, tvec, (s_char *)lp, EF_LAND);
277 for (i = 0; NULL != (spp = getshipp(i)); i++) {
280 if (spp->shp_x != sp->sct_x || spp->shp_y != sp->sct_y)
282 if (mchr[(int)spp->shp_type].m_flags & M_SUB)
284 getvec(VT_ITEM, tvec, (s_char *)spp, EF_SHIP);
285 meltitems(etus, cvec[C_FALLOUT], spp->shp_own, tvec, EF_SHIP,
286 spp->shp_x, spp->shp_y, spp->shp_uid);
287 putvec(VT_ITEM, tvec, (s_char *)spp, EF_SHIP);
290 if ((cvec[C_FALLOUT] > 20) && chance(100))
292 #endif /* GODZILLA */
296 spread_fallout(struct sctstr *sp, int etus)
306 getvec(VT_COND, cvec, (s_char *)sp, EF_SECTOR);
307 for (n = DIR_FIRST; n <= DIR_LAST; n++) {
308 ap = getsectp(sp->sct_x + diroff[n][0], sp->sct_y + diroff[n][1]);
309 getvec(VT_COND, tvec, (char *)ap, EF_SECTOR);
310 if (ap->sct_type == SCT_SANCT)
312 inc = roundavg(etus * fallout_spread * (cvec[C_FALLOUT])) - 1;
314 if (cvec[C_FALLOUT]) {
315 wu(0, 0, "Fallout from sector %d,%d to %d,%d is %d=%d*%e*%d\n",
316 sp->sct_x, sp->sct_y, sp->sct_x + diroff[n][0],
317 sp->sct_y + diroff[n][1], inc, etus,
318 fallout_spread, cvec[C_FALLOUT]);
323 tvec[C_FALLOUT] += inc;
324 putvec(VT_COND, tvec, (char *)ap, EF_SECTOR);
329 decay_fallout(struct sctstr *sp, int etus)
336 getvec(VT_COND, cvec, (char *)sp, EF_SECTOR);
337 decay = roundavg(((decay_per_etu + 6.0) * fallout_spread) *
338 (double)etus * (double)cvec[C_FALLOUT]);
341 if (decay || cvec[C_FALLOUT])
342 wu(0, 0, "Fallout decay in %d,%d is %d from %d\n", sp->sct_x,
343 sp->sct_y, decay, cvec[C_FALLOUT]);
347 (decay < cvec[C_FALLOUT]) ? (cvec[C_FALLOUT] - decay) : 0;
348 if (cvec[C_FALLOUT] < 0)
350 putvec(VT_COND, cvec, (s_char *)sp, EF_SECTOR);
353 #define SHOULD_PRODUCE(sp,t) (((sp->sct_type == t) || (t == -1)) ? 1 : 0)
356 * Produce only a set sector type for a specific nation
357 * (or all, if sector_type == -1)
361 produce_sect(int natnum, int etu, int *bp, long int (*p_sect)[2],
364 register struct sctstr *sp;
365 register struct natstr *np;
367 int work, cost, ecost, pcost, sctwork;
368 int n, desig, maxpop, neweff, amount;
370 for (n = 0; NULL != (sp = getsectid(n)); n++) {
371 if (sp->sct_type == SCT_WATER)
373 if (sp->sct_own != natnum)
375 if (sp->sct_updated != 0)
377 if (!SHOULD_PRODUCE(sp, sector_type))
380 if ((sp->sct_type == SCT_CAPIT) && (sp->sct_effic > 60)) {
381 p_sect[SCT_CAPIT][0]++;
382 p_sect[SCT_CAPIT][1] += etu;
385 if (getvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR) <= 0)
387 /* If everybody is dead, the sector reverts to unowned.
388 * This is also checked at the end of the production in
389 * they all starved or were plagued off.
391 if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 &&
392 !has_units(sp->sct_x, sp->sct_y, sp->sct_own, 0)) {
393 makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
402 np = getnatp(natnum);
404 /* do_feed trys to supply. So, we need to enable cacheing
406 bp_enable_cachepath();
408 sctwork = do_feed(sp, np, vec, &work, bp, etu);
410 bp_disable_cachepath();
411 bp_clear_cachepath();
413 if (sp->sct_off || np->nat_money < 0) {
414 if (!player->simulation) {
415 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
420 if ((np->nat_priorities[sp->sct_type] == 0) &&
421 (sp->sct_type == sp->sct_newtype) &&
422 ((pchr[dchr[sp->sct_type].d_prd].p_cost != 0) ||
423 (sp->sct_type == SCT_ENLIST))) {
424 if (!player->simulation) {
425 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
426 logerror("Skipping %s production for country %s\n",
427 dchr[sp->sct_type].d_name, np->nat_cnam);
432 neweff = sp->sct_effic;
434 pcost = cost = ecost = 0;
436 desig = sp->sct_type;
438 if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) &&
441 upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork,
443 pt_bg_nmbr(bp, sp, I_LCM, vec[I_LCM]);
444 pt_bg_nmbr(bp, sp, I_HCM, vec[I_HCM]);
445 p_sect[SCT_EFFIC][0]++;
446 p_sect[SCT_EFFIC][1] += cost;
447 if (!player->simulation) {
448 np->nat_money -= cost;
449 /* No longer tear down infrastructure
450 if (sp->sct_type != desig) {
453 } else if (neweff < sp->sct_effic) {
454 sp->sct_road -= (sp->sct_road * (sp->sct_effic - neweff) / 100.0);
455 sp->sct_defense -= (sp->sct_defense * (sp->sct_effic - neweff) / 100.0);
456 if (sp->sct_road < 0)
458 if (sp->sct_defense < 0)
462 sp->sct_type = desig;
463 sp->sct_effic = neweff;
464 if (!opt_DEFENSE_INFRA)
465 sp->sct_defense = sp->sct_effic;
469 if ((np->nat_priorities[desig] == 0) &&
470 ((pchr[dchr[desig].d_prd].p_cost != 0) ||
471 (desig == SCT_ENLIST))) {
472 if (!player->simulation) {
473 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
474 logerror("Skipping %s production for country %s\n",
475 dchr[sp->sct_type].d_name, np->nat_cnam);
480 if (desig == SCT_ENLIST && neweff >= 60 &&
481 sp->sct_own == sp->sct_oldown) {
482 p_sect[desig][0] += enlist(vec, etu, &ecost);
483 p_sect[desig][1] += ecost;
484 if (!player->simulation)
485 np->nat_money -= ecost;
489 * now do the production (if sector effic >= 60%)
493 if (np->nat_money > 0 && dchr[desig].d_prd)
495 produce(np, sp, vec, work, sctwork, desig, neweff,
499 pt_bg_nmbr(bp, sp, I_MAX + 1, work);
500 p_sect[desig][0] += amount;
501 p_sect[desig][1] += pcost;
502 if (!player->simulation) {
503 maxpop = max_pop(np->nat_level[NAT_RLEV], sp);
504 if (vec[I_CIVIL] > maxpop)
505 vec[I_CIVIL] = maxpop;
506 if (vec[I_UW] > maxpop)
508 putvec(VT_ITEM, vec, (s_char *)sp, EF_SECTOR);
509 sp->sct_avail = work;
510 np->nat_money -= pcost;