void finish_sects(int etu) { static double *import_cost; struct sctstr *sp; int n; struct rusage rus1, rus2; if (import_cost == NULL) { logerror("First update since reboot, allocating buffer\n"); import_cost = malloc(WORLD_SZ() * sizeof(*import_cost)); if (import_cost == NULL) { logerror("malloc failed in finish_sects.\n"); return; } } logerror("delivering...\n"); /* Do deliveries */ for (n = 0; NULL != (sp = getsectid(n)); n++) { if (!sp->sct_own || sp->sct_type == SCT_SANCT) continue; if (nat_budget[sp->sct_own].money < 0) continue; dodeliver(sp); } logerror("done delivering\n"); logerror("assembling paths...\n"); getrusage(RUSAGE_SELF, &rus1); assemble_dist_paths(import_cost); getrusage(RUSAGE_SELF, &rus2); logerror("done assembling paths %g user %g system", rus2.ru_utime.tv_sec + rus2.ru_utime.tv_usec / 1e6 - (rus1.ru_utime.tv_sec + rus1.ru_utime.tv_usec / 1e6), rus2.ru_stime.tv_sec + rus2.ru_stime.tv_usec / 1e6 - (rus1.ru_stime.tv_sec + rus1.ru_stime.tv_usec / 1e6)); logerror("exporting..."); for (n = 0; NULL != (sp = getsectid(n)); n++) { if (!sp->sct_own || sp->sct_type == SCT_SANCT) continue; if (nat_budget[sp->sct_own].money < 0) continue; dodistribute(sp, EXPORT, import_cost[n]); } logerror("done exporting\n"); logerror("importing..."); for (n = 0; NULL != (sp = getsectid(n)); n++) { sp->sct_off = 0; if (!sp->sct_own || sp->sct_type == SCT_SANCT) continue; if (nat_budget[sp->sct_own].money < 0) continue; dodistribute(sp, IMPORT, import_cost[n]); } logerror("done importing\n"); }
void prepare_sects(int etu, struct bp *bp) { struct sctstr *sp; struct natstr *np; int n, civ_tax, uw_tax, mil_pay; memset(levels, 0, sizeof(levels)); /* Process all the fallout. */ if (opt_FALLOUT) { if (!player->simulation) { /* First, we determine which sectors to process fallout in */ for (n = 0; NULL != (sp = getsectid(n)); n++) sp->sct_updated = sp->sct_fallout != 0; /* Next, we process the fallout there */ for (n = 0; NULL != (sp = getsectid(n)); n++) if (sp->sct_updated) do_fallout(sp, etu); /* Next, we spread the fallout */ for (n = 0; NULL != (sp = getsectid(n)); n++) if (sp->sct_updated) spread_fallout(sp, etu); /* Next, we decay the fallout */ for (n = 0; NULL != (sp = getsectid(n)); n++) if (sp->sct_fallout) decay_fallout(sp, etu); } } for (n = 0; NULL != (sp = getsectid(n)); n++) { sp->sct_updated = 0; if (sp->sct_type == SCT_WATER) continue; bp_set_from_sect(bp, sp); np = getnatp(sp->sct_own); if (np->nat_stat != STAT_SANCT) { guerrilla(sp); do_plague(sp, np, etu); tax(sp, np, etu, &pops[sp->sct_own], &civ_tax, &uw_tax, &mil_pay); np->nat_money += civ_tax + uw_tax + mil_pay; if (sp->sct_type == SCT_BANK) np->nat_money += bank_income(sp, etu); } } for (n = 0; NULL != (np = getnatp(n)); n++) { np->nat_money += upd_slmilcosts(np->nat_cnum, etu); } }
static void assemble_dist_paths(double *import_cost) { struct sctstr *sp; struct sctstr *dist; int n; static int *job; int uid, i; coord dx = 1, dy = 0; /* invalid */ if (!job) job = malloc(WORLD_SZ() * sizeof(*job)); n = 0; for (uid = 0; NULL != (sp = getsectid(uid)); uid++) { import_cost[uid] = -1; if (sp->sct_dist_x == sp->sct_x && sp->sct_dist_y == sp->sct_y) continue; job[n++] = uid; } #ifdef PATH_FIND_STATS printf("dist path reuse %zu bytes, %d/%d used\n", WORLD_SZ() * sizeof(*job), n, WORLD_SZ()); #endif qsort(job, n, sizeof(*job), distcmp); for (i = 0; i < n; i++) { uid = job[i]; sp = getsectid(uid); dist = getsectp(sp->sct_dist_x, sp->sct_dist_y); if (CANT_HAPPEN(!dist)) continue; if (sp->sct_own != dist->sct_own) continue; if (sp->sct_dist_x != dx || sp->sct_dist_y != dy) { dx = sp->sct_dist_x; dy = sp->sct_dist_y; path_find_from(dx, dy, dist->sct_own, MOB_MOVE); } import_cost[uid] = path_find_to(sp->sct_x, sp->sct_y); } path_find_print_stats(); }
/* * Write a symbol to a file */ void writesymbol(struct sSymbol * pSym, FILE * f) { char symname[MAXSYMLEN * 2 + 1]; ULONG type; ULONG offset; SLONG sectid; if (pSym->nType & SYMF_IMPORT) { /* Symbol should be imported */ strcpy(symname, pSym->tzName); offset = 0; sectid = -1; type = SYM_IMPORT; } else if (pSym->nType & SYMF_EXPORT) { /* Symbol should be exported */ strcpy(symname, pSym->tzName); type = SYM_EXPORT; offset = pSym->nValue; if (pSym->nType & SYMF_CONST) sectid = -1; else sectid = getsectid(pSym->pSection); } else { /* Symbol is local to this file */ if (pSym->nType & SYMF_LOCAL) { strcpy(symname, pSym->pScope->tzName); strcat(symname, pSym->tzName); } else strcpy(symname, pSym->tzName); type = SYM_LOCAL; offset = pSym->nValue; sectid = getsectid(pSym->pSection); } fputstring(symname, f); fputc(type, f); if (type != SYM_IMPORT) { fputlong(sectid, f); fputlong(offset, f); } }
static int verify_sectors(int may_put) { int i; struct sctstr *sp; coord x, y; /* laziness: assumes sector file is EFF_MEM */ for (i = 0; (sp = getsectid(i)); i++) { sctoff2xy(&x, &y, sp->sct_uid); if (sp->sct_x != x || sp->sct_y != y) { sp->sct_x = x; sp->sct_y = y; if (may_put) putsect(sp); verify_fail(EF_SECTOR, i, NULL, 0, "bogus coordinates (fixed)"); } } return 0; }
/* * Produce for a specific nation */ void produce_sect(int natnum, int etu, struct bp *bp, int p_sect[][2]) { struct sctstr *sp; struct natstr *np; short buf[I_MAX + 1]; short *vec; int work, cost, ecost, pcost, sctwork; int n, desig, neweff, amount; for (n = 0; NULL != (sp = getsectid(n)); n++) { if (sp->sct_type == SCT_WATER) continue; if (sp->sct_own != natnum) continue; if (sp->sct_updated != 0) continue; np = getnatp(natnum); if (player->simulation) { /* work on a copy, which will be discarded */ memcpy(buf, sp->sct_item, sizeof(buf)); vec = buf; } else vec = sp->sct_item; /* If everybody is dead, the sector reverts to unowned. * This is also checked at the end of the production in * they all starved or were plagued off. */ if (vec[I_CIVIL] == 0 && vec[I_MILIT] == 0 && !has_units(sp->sct_x, sp->sct_y, sp->sct_own, NULL)) { if (!player->simulation) { makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y); sp->sct_own = 0; sp->sct_oldown = 0; } continue; } sp->sct_updated = 1; work = 0; sctwork = do_feed(sp, np, vec, &work, etu); bp_put_items(bp, sp, vec); if (sp->sct_off || np->nat_money < 0) continue; neweff = sp->sct_effic; amount = 0; pcost = cost = ecost = 0; desig = sp->sct_type; if (dchr[desig].d_maint) { cost = etu * dchr[desig].d_maint; p_sect[SCT_MAINT][0]++; p_sect[SCT_MAINT][1] += cost; if (!player->simulation) np->nat_money -= cost; } if ((sp->sct_effic < 100 || sp->sct_type != sp->sct_newtype) && np->nat_money >= 0) { neweff = upd_buildeff(np, sp, &work, vec, etu, &desig, sctwork, &cost); bp_put_items(bp, sp, vec); p_sect[SCT_EFFIC][0]++; p_sect[SCT_EFFIC][1] += cost; if (!player->simulation) { np->nat_money -= cost; sp->sct_type = desig; sp->sct_effic = neweff; } } if (desig == SCT_ENLIST && neweff >= 60 && sp->sct_own == sp->sct_oldown) { p_sect[desig][0] += enlist(vec, etu, &ecost); p_sect[desig][1] += ecost; if (!player->simulation) np->nat_money -= ecost; bp_put_items(bp, sp, vec); } /* * now do the production (if sector effic >= 60%) */ if (neweff >= 60) { if (np->nat_money >= 0 && dchr[desig].d_prd >= 0) work -= produce(np, sp, vec, work, desig, neweff, &pcost, &amount); bp_put_items(bp, sp, vec); } bp_put_avail(bp, sp, work); p_sect[desig][0] += amount; p_sect[desig][1] += pcost; if (!player->simulation) { sp->sct_avail = work; np->nat_money -= pcost; } } }