int main(int argc, char *argv[]) { int *ids; loginit(NULL); Rng r; int usdargs = rng(&r, argc, argv); argc -= usdargs; argv += usdargs; int n = idargs(argc, argv, &ids); if (argc < 3) fatal("usage: enmgen [-s <seed>] <ID>+ <num>"); long num = strtol(argv[argc-1], NULL, 10); if (num == LLONG_MIN || num == LLONG_MAX) fatal("Invalid number: %s", argv[argc-1]); Zone *zn = zoneread(stdin); if (!zn) die("Failed to read the zone: %s", miderrstr()); Loc ls[zn->lvl->d * zn->lvl->w * zn->lvl->h]; int nls = locs(zn, ls); int i; for (i = 0; i < num && nls > 0; i++) { int idind = rngintincl(&r, 0, n); int lind = rngintincl(&r, 0, nls); Loc l = ls[lind]; if (nls > 1) ls[lind] = ls[nls-1]; nls--; Enemy enm; if (!enemyinit(&enm, ids[idind], l.p.x, l.p.y)) fatal("Failed to initialize enemy with ID: %d", ids[idind]); if (!zoneaddenemy(zn, l.z, enm)) { nls = rmz(ls, nls, l.z); num--; } } if (i < num) fatal("Failed to place all items"); zonewrite(stdout, zn); zonefree(zn); xfree(ids); return 0; }
Zone *zoneget(int znum) { ignframetime(); char *zfile = zonefile(znum); FILE *f = fopen(zfile, "r"); if (!f) die("Unable to open the zone file [%s]: %s", zfile, miderrstr()); Zone *zn = zoneread(f); if (!zn) die("Failed to read the zone file [%s]: %s", zfile, miderrstr()); fclose(f); return zn; }
Zone *zonegen(Rng *r, int depth) { ignframetime(); FILE *fin = inzone; if (!fin) fin = zpipe(r, depth); Zone *z = zoneread(fin); if (!z) die("Failed to read the zone: %s", miderrstr()); if (inzone) { fclose(fin); inzone = NULL; } else { int ret = pipeclose(fin); if (ret == -1) die("Zone gen pipeline exited with failure: %s", miderrstr()); } return z; }