Skip to content
1        10        20        30        40        50        60        70        80        90        100       1101import { WEAPONS, MODS, BASE_UPGRADES, CONTRACTS, START, RESOURCES, AMMO, PRESTIGE, PERKS, MAPS } from './config.js';23// Two kinds of save. The prestige meta survives every run. Each map keeps its own run until the pill ends it.4const META_KEY = 'zombie-sniper-prestige-v1';5const LEGACY_KEY = 'zombie-sniper-save-v1';6const runKey = (mapId) => `zombie-sniper-run-v1-${mapId}`;78// Survives every run: the prestige level, points banked by the pill, and bought perks.9export const meta = { prestige: 0, points: 0, perks: {} };1011// A new run. Perks bought in the menu shop are applied here.12function freshRun() {13  const owned = { hunter: true };14  const mods = {};15  for (const id of Object.keys(WEAPONS)) {16    mods[id] = { barrel: 0, trigger: 0, stock: 0, scope: 0, mag: 0 };17  }18  const upgrades = {};19  const ammo = { ...START.ammo };20  let money = START.money;21  for (const [id, lvl] of Object.entries(meta.perks)) {22    const perk = PERKS[id];23    if (!perk || !lvl) continue;24    if (perk.weapon) owned[perk.weapon] = true;25    if (perk.upgrade) upgrades[perk.upgrade] = true;26    if (perk.cash) money += perk.cash * lvl;27    if (perk.ammo) for (const k of Object.keys(ammo)) ammo[k] += perk.ammo * lvl;28  }29  return {30    runPoints: 0, // earned this run. Banked into meta.points when the pill is taken.31    money,32    resources: { ...START.resources },33    ammo,34    owned,35    mods,36    upgrades,37    equipped: 'hunter',38    stats: {39      kills: 0,40      kills_walker: 0,41      kills_runner: 0,42      kills_brute: 0,43      headshots: 0,44      longestKill: 0,45      shots: 0,46      earned: 0,47    },48    claimed: {},49    won: false,50  };51}5253export const state = freshRun();5455// The map whose run is being played. Nothing is saved until a run starts.56let activeMap = null;5758// The first build kept prestige inside the run save. Move it out once, so nobody loses points.59function moveLegacySave(data) {60  const { prestige = 0, points = 0, perks = {}, ...run } = data;61  Object.assign(meta, { prestige, points, perks });62  saveMeta();63  localStorage.setItem(runKey(MAPS[0].id), JSON.stringify(run));64  localStorage.removeItem(LEGACY_KEY);65}6667export function loadMeta() {68  try {69    const legacy = localStorage.getItem(LEGACY_KEY);70    if (legacy) moveLegacySave(JSON.parse(legacy));71    const raw = localStorage.getItem(META_KEY);72    if (raw) Object.assign(meta, JSON.parse(raw));73  } catch (e) {74    console.warn('Prestige could not be loaded', e);75  }76}7778export function saveMeta() {79  try {80    localStorage.setItem(META_KEY, JSON.stringify(meta));81  } catch (e) {82    console.warn('Prestige save failed', e);83  }84}8586// Whether a map has a run in progress, so the menu can say Continue instead of New run.87export function hasRun(mapId) {88  return localStorage.getItem(runKey(mapId)) !== null;89}9091// Starts a new run on a map, or resumes the one in progress.92export function startRun(mapId) {93  activeMap = mapId;94  const base = freshRun();95  try {96    const raw = localStorage.getItem(runKey(mapId));97    if (raw) {98      const data = JSON.parse(raw);99      Object.assign(state, base, data);100      for (const id of Object.keys(WEAPONS)) {101        state.mods[id] = { ...base.mods[id], ...(data.mods?.[id] || {}) };102      }103      state.resources = { ...base.resources, ...(data.resources || {}) };104      state.ammo = { ...base.ammo, ...(data.ammo || {}) };105      state.stats = { ...base.stats, ...(data.stats || {}) };106      return;107    }108  } catch (e) {109    console.warn('Run could not be loaded', e);110  }111  Object.assign(state, base);112  save();113}114115export function save() {116  if (!activeMap) return;117  try {118    localStorage.setItem(runKey(activeMap), JSON.stringify(state));119  } catch (e) {120    console.warn('Save failed', e);121  }122}123124// Multiplier on zombie HP for the current prestige level.125export function hpMult() {126  return 1 - Math.min(PRESTIGE.maxHpCut, meta.prestige * PRESTIGE.hpCut);127}128129// The pill, or death. Banks this run's points and ends the run. Only the pill raises the prestige level,130// and only after a win.131export function endRun({ died = false } = {}) {132  if (!activeMap) return false;133  if (state.won && !died) meta.prestige += 1;134  meta.points += state.runPoints;135  saveMeta();136  localStorage.removeItem(runKey(activeMap));137  activeMap = null;138  return true;139}140141export function earnPoints(n) {142  state.runPoints += n;143  save();144}145146export function perkLevel(id) {147  return meta.perks[id] || 0;148}149150// Spends banked prestige points on a perk. It applies from the next run.151export function buyPerk(id) {152  const perk = PERKS[id];153  const lvl = perkLevel(id);154  if (!perk || lvl >= perk.max || meta.points < perk.cost) return false;155  meta.points -= perk.cost;156  meta.perks[id] = lvl + 1;157  saveMeta();158  return true;159}160161export function canAfford(cost) {162  if (!cost) return true;163  if ((cost.money || 0) > state.money) return false;164  for (const [k, v] of Object.entries(cost)) {165    if (k === 'money') continue;166    if ((state.resources[k] || 0) < v) return false;167  }168  return true;169}170171export function pay(cost) {172  if (!canAfford(cost)) return false;173  if (!cost) return true;174  state.money -= cost.money || 0;175  for (const [k, v] of Object.entries(cost)) {176    if (k === 'money') continue;177    state.resources[k] -= v;178  }179  save();180  return true;181}182183export function addReward(reward) {184  if (!reward) return;185  if (reward.money) state.money += reward.money;186  for (const [k, v] of Object.entries(reward)) {187    if (k === 'money') continue;188    state.resources[k] = (state.resources[k] || 0) + v;189  }190}191192export function craftWeapon(id) {193  const w = WEAPONS[id];194  if (state.owned[id] || !w) return false;195  if (!pay(w.cost)) return false;196  state.owned[id] = true;197  save();198  return true;199}200201export function modLevel(weaponId, mod) {202  return state.mods[weaponId]?.[mod] || 0;203}204205export function upgradeMod(weaponId, mod) {206  const def = MODS[mod];207  const lvl = modLevel(weaponId, mod);208  if (lvl >= def.max) return false;209  if (!pay(def.cost(lvl))) return false;210  state.mods[weaponId][mod] = lvl + 1;211  save();212  return true;213}214215export function buyUpgrade(id) {216  const def = BASE_UPGRADES[id];217  if (!def || state.upgrades[id]) return false;218  if (!pay(def.cost)) return false;219  state.upgrades[id] = true;220  save();221  return true;222}223224export function craftAmmo(type) {225  const def = AMMO[type];226  if (!def) return false;227  if (!pay(def.craft)) return false;228  state.ammo[type] = (state.ammo[type] || 0) + def.yield;229  save();230  return true;231}232233export function sellResource(res, amount) {234  const have = state.resources[res] || 0;235  const n = Math.min(amount, have);236  if (n <= 0) return 0;237  state.resources[res] -= n;238  const gain = n * RESOURCES[res].sell;239  state.money += gain;240  save();241  return gain;242}243244export function claimContract(id) {245  const c = CONTRACTS.find((x) => x.id === id);246  if (!c || state.claimed[id] || contractProgress(c) < c.target) return false;247  state.claimed[id] = true;248  addReward(c.reward);249  save();250  return true;251}252253export function contractProgress(c) {254  return Math.min(state.stats[c.metric] || 0, c.target);255}256257export function recordKill(type, headshot, dist) {258  const s = state.stats;259  s.kills += 1;260  s['kills_' + type] = (s['kills_' + type] || 0) + 1;261  if (headshot) s.headshots += 1;262  s.longestKill = Math.max(s.longestKill, Math.round(dist));263}