
var Module;

if (typeof Module === 'undefined') Module = eval('(function() { try { return Module || {} } catch(e) { return {} } })()');

if (!Module.expectedDataFileDownloads) {
  Module.expectedDataFileDownloads = 0;
  Module.finishedDataFileDownloads = 0;
}
Module.expectedDataFileDownloads++;
(function() {
 var loadPackage = function(metadata) {

  var PACKAGE_PATH;
  if (typeof window === 'object') {
    PACKAGE_PATH = window['encodeURIComponent'](window.location.pathname.toString().substring(0, window.location.pathname.toString().lastIndexOf('/')) + '/');
  } else if (typeof location !== 'undefined') {
      // worker
      PACKAGE_PATH = encodeURIComponent(location.pathname.toString().substring(0, location.pathname.toString().lastIndexOf('/')) + '/');
    } else {
      throw 'using preloaded data can only be done on a web page or in a web worker';
    }
    var PACKAGE_NAME = 'game.data';
    var REMOTE_PACKAGE_BASE = 'game.data';
    if (typeof Module['locateFilePackage'] === 'function' && !Module['locateFile']) {
      Module['locateFile'] = Module['locateFilePackage'];
      Module.printErr('warning: you defined Module.locateFilePackage, that has been renamed to Module.locateFile (using your locateFilePackage for now)');
    }
    var REMOTE_PACKAGE_NAME = typeof Module['locateFile'] === 'function' ?
    Module['locateFile'](REMOTE_PACKAGE_BASE) :
    ((Module['filePackagePrefixURL'] || '') + REMOTE_PACKAGE_BASE);

    var REMOTE_PACKAGE_SIZE = metadata.remote_package_size;
    var PACKAGE_UUID = metadata.package_uuid;

    function fetchRemotePackage(packageName, packageSize, callback, errback) {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', packageName, true);
      xhr.responseType = 'arraybuffer';
      xhr.onprogress = function(event) {
        var url = packageName;
        var size = packageSize;
        if (event.total) size = event.total;
        if (event.loaded) {
          if (!xhr.addedTotal) {
            xhr.addedTotal = true;
            if (!Module.dataFileDownloads) Module.dataFileDownloads = {};
            Module.dataFileDownloads[url] = {
              loaded: event.loaded,
              total: size
            };
          } else {
            Module.dataFileDownloads[url].loaded = event.loaded;
          }
          var total = 0;
          var loaded = 0;
          var num = 0;
          for (var download in Module.dataFileDownloads) {
            var data = Module.dataFileDownloads[download];
            total += data.total;
            loaded += data.loaded;
            num++;
          }
          total = Math.ceil(total * Module.expectedDataFileDownloads/num);
          if (Module['setStatus']) Module['setStatus']('Downloading data... (' + loaded + '/' + total + ')');
        } else if (!Module.dataFileDownloads) {
          if (Module['setStatus']) Module['setStatus']('Downloading data...');
        }
      };
      xhr.onerror = function(event) {
        throw new Error("NetworkError for: " + packageName);
      }
      xhr.onload = function(event) {
        if (xhr.status == 200 || xhr.status == 304 || xhr.status == 206 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0
          var packageData = xhr.response;
          callback(packageData);
        } else {
          throw new Error(xhr.statusText + " : " + xhr.responseURL);
        }
      };
      xhr.send(null);
    };

    function handleError(error) {
      console.error('package error:', error);
    };

    function runWithFS() {

      function assert(check, msg) {
        if (!check) throw msg + new Error().stack;
      }
      Module['FS_createPath']('/', 'audio', true, true);
      Module['FS_createPath']('/', 'fonts', true, true);
      Module['FS_createPath']('/', 'img', true, true);
      Module['FS_createPath']('/img', 'player', true, true);
      Module['FS_createPath']('/img', 'ui', true, true);
      Module['FS_createPath']('/img', 'world', true, true);
      Module['FS_createPath']('/img/world', 'decal', true, true);
      Module['FS_createPath']('/', 'libs', true, true);
      Module['FS_createPath']('/libs', 'AnAL', true, true);
      Module['FS_createPath']('/libs/AnAL', 'docs', true, true);
      Module['FS_createPath']('/libs/AnAL/docs', 'files', true, true);
      Module['FS_createPath']('/libs', 'shine', true, true);

      function DataRequest(start, end, crunched, audio) {
        this.start = start;
        this.end = end;
        this.crunched = crunched;
        this.audio = audio;
      }
      DataRequest.prototype = {
        requests: {},
        open: function(mode, name) {
          this.name = name;
          this.requests[name] = this;
          Module['addRunDependency']('fp ' + this.name);
        },
        send: function() {},
        onload: function() {
          var byteArray = this.byteArray.subarray(this.start, this.end);

          this.finish(byteArray);

        },
        finish: function(byteArray) {
          var that = this;

        Module['FS_createDataFile'](this.name, null, byteArray, true, true, true); // canOwn this data in the filesystem, it is a slide into the heap that will never change
        Module['removeRunDependency']('fp ' + that.name);

        this.requests[this.name] = null;
      }
    };

    var files = metadata.files;
    for (i = 0; i < files.length; ++i) {
      new DataRequest(files[i].start, files[i].end, files[i].crunched, files[i].audio).open('GET', files[i].filename);
    }


    var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
    var IDB_RO = "readonly";
    var IDB_RW = "readwrite";
    var DB_NAME = "EM_PRELOAD_CACHE";
    var DB_VERSION = 1;
    var METADATA_STORE_NAME = 'METADATA';
    var PACKAGE_STORE_NAME = 'PACKAGES';
    function openDatabase(callback, errback) {
      try {
        var openRequest = indexedDB.open(DB_NAME, DB_VERSION);
      } catch (e) {
        return errback(e);
      }
      openRequest.onupgradeneeded = function(event) {
        var db = event.target.result;

        if(db.objectStoreNames.contains(PACKAGE_STORE_NAME)) {
          db.deleteObjectStore(PACKAGE_STORE_NAME);
        }
        var packages = db.createObjectStore(PACKAGE_STORE_NAME);

        if(db.objectStoreNames.contains(METADATA_STORE_NAME)) {
          db.deleteObjectStore(METADATA_STORE_NAME);
        }
        var metadata = db.createObjectStore(METADATA_STORE_NAME);
      };
      openRequest.onsuccess = function(event) {
        var db = event.target.result;
        callback(db);
      };
      openRequest.onerror = function(error) {
        errback(error);
      };
    };

    /* Check if there's a cached package, and if so whether it's the latest available */
    function checkCachedPackage(db, packageName, callback, errback) {
      var transaction = db.transaction([METADATA_STORE_NAME], IDB_RO);
      var metadata = transaction.objectStore(METADATA_STORE_NAME);

      var getRequest = metadata.get("metadata/" + packageName);
      getRequest.onsuccess = function(event) {
        var result = event.target.result;
        if (!result) {
          return callback(false);
        } else {
          return callback(PACKAGE_UUID === result.uuid);
        }
      };
      getRequest.onerror = function(error) {
        errback(error);
      };
    };

    function fetchCachedPackage(db, packageName, callback, errback) {
      var transaction = db.transaction([PACKAGE_STORE_NAME], IDB_RO);
      var packages = transaction.objectStore(PACKAGE_STORE_NAME);

      var getRequest = packages.get("package/" + packageName);
      getRequest.onsuccess = function(event) {
        var result = event.target.result;
        callback(result);
      };
      getRequest.onerror = function(error) {
        errback(error);
      };
    };

    function cacheRemotePackage(db, packageName, packageData, packageMeta, callback, errback) {
      var transaction_packages = db.transaction([PACKAGE_STORE_NAME], IDB_RW);
      var packages = transaction_packages.objectStore(PACKAGE_STORE_NAME);

      var putPackageRequest = packages.put(packageData, "package/" + packageName);
      putPackageRequest.onsuccess = function(event) {
        var transaction_metadata = db.transaction([METADATA_STORE_NAME], IDB_RW);
        var metadata = transaction_metadata.objectStore(METADATA_STORE_NAME);
        var putMetadataRequest = metadata.put(packageMeta, "metadata/" + packageName);
        putMetadataRequest.onsuccess = function(event) {
          callback(packageData);
        };
        putMetadataRequest.onerror = function(error) {
          errback(error);
        };
      };
      putPackageRequest.onerror = function(error) {
        errback(error);
      };
    };

    function processPackageData(arrayBuffer) {
      Module.finishedDataFileDownloads++;
      assert(arrayBuffer, 'Loading data file failed.');
      assert(arrayBuffer instanceof ArrayBuffer, 'bad input to processPackageData');
      var byteArray = new Uint8Array(arrayBuffer);
      var curr;

        // copy the entire loaded file into a spot in the heap. Files will refer to slices in that. They cannot be freed though
        // (we may be allocating before malloc is ready, during startup).
        if (Module['SPLIT_MEMORY']) Module.printErr('warning: you should run the file packager with --no-heap-copy when SPLIT_MEMORY is used, otherwise copying into the heap may fail due to the splitting');
        var ptr = Module['getMemory'](byteArray.length);
        Module['HEAPU8'].set(byteArray, ptr);
        DataRequest.prototype.byteArray = Module['HEAPU8'].subarray(ptr, ptr+byteArray.length);

        var files = metadata.files;
        for (i = 0; i < files.length; ++i) {
          DataRequest.prototype.requests[files[i].filename].onload();
        }
        Module['removeRunDependency']('datafile_game.data');

      };
      Module['addRunDependency']('datafile_game.data');

      if (!Module.preloadResults) Module.preloadResults = {};

      function preloadFallback(error) {
        console.error(error);
        console.error('falling back to default preload behavior');
        fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE, processPackageData, handleError);
      };

      openDatabase(
        function(db) {
          checkCachedPackage(db, PACKAGE_PATH + PACKAGE_NAME,
            function(useCached) {
              Module.preloadResults[PACKAGE_NAME] = {fromCache: useCached};
              if (useCached) {
                console.info('loading ' + PACKAGE_NAME + ' from cache');
                fetchCachedPackage(db, PACKAGE_PATH + PACKAGE_NAME, processPackageData, preloadFallback);
              } else {
                console.info('loading ' + PACKAGE_NAME + ' from remote');
                fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE,
                  function(packageData) {
                    cacheRemotePackage(db, PACKAGE_PATH + PACKAGE_NAME, packageData, {uuid:PACKAGE_UUID}, processPackageData,
                      function(error) {
                        console.error(error);
                        processPackageData(packageData);
                      });
                  }
                  , preloadFallback);
              }
            }
            , preloadFallback);
        }
        , preloadFallback);

      if (Module['setStatus']) Module['setStatus']('Downloading...');

    }
    if (Module['calledRun']) {
      runWithFS();
    } else {
      if (!Module['preRun']) Module['preRun'] = [];
      Module["preRun"].push(runWithFS); // FS is not initialized yet, wait for it
    }

  }
  loadPackage({"package_uuid":"abd35cb6-2017-4b61-9d3a-f74cf1190700","remote_package_size":2743820,"files":[{"filename":"/.DS_Store","crunched":0,"start":0,"end":6148,"audio":false},{"filename":"/audio/Creepy_Percussion_10.mp3","crunched":0,"start":6148,"end":56507,"audio":true},{"filename":"/audio/MechDrone1.mp3","crunched":0,"start":56507,"end":327720,"audio":true},{"filename":"/audio/MechDrone1.ogg","crunched":0,"start":327720,"end":510210,"audio":true},{"filename":"/audio/MechDrone6.mp3","crunched":0,"start":510210,"end":781523,"audio":true},{"filename":"/audio/MechDrone6.ogg","crunched":0,"start":781523,"end":960341,"audio":true},{"filename":"/audio/attack1.wav","crunched":0,"start":960341,"end":1002453,"audio":true},{"filename":"/audio/attack2.wav","crunched":0,"start":1002453,"end":1044487,"audio":true},{"filename":"/audio/attack3.wav","crunched":0,"start":1044487,"end":1086521,"audio":true},{"filename":"/audio/attack4.wav","crunched":0,"start":1086521,"end":1136961,"audio":true},{"filename":"/audio/bomb.wav","crunched":0,"start":1136961,"end":1216661,"audio":true},{"filename":"/audio/break.wav","crunched":0,"start":1216661,"end":1386353,"audio":true},{"filename":"/audio/click.wav","crunched":0,"start":1386353,"end":1444691,"audio":true},{"filename":"/audio/close.wav","crunched":0,"start":1444691,"end":1461359,"audio":true},{"filename":"/audio/death.wav","crunched":0,"start":1461359,"end":1570553,"audio":true},{"filename":"/audio/explosion.wav","crunched":0,"start":1570553,"end":1598075,"audio":true},{"filename":"/audio/jump.wav","crunched":0,"start":1598075,"end":1918285,"audio":true},{"filename":"/audio/light.wav","crunched":0,"start":1918285,"end":1939499,"audio":true},{"filename":"/audio/open.wav","crunched":0,"start":1939499,"end":1956167,"audio":true},{"filename":"/audio/pickup.wav","crunched":0,"start":1956167,"end":2023741,"audio":true},{"filename":"/audio/simon.wav","crunched":0,"start":2023741,"end":2036031,"audio":true},{"filename":"/audio/step1.wav","crunched":0,"start":2036031,"end":2048019,"audio":true},{"filename":"/audio/step2.wav","crunched":0,"start":2048019,"end":2060007,"audio":true},{"filename":"/audio/step3.wav","crunched":0,"start":2060007,"end":2071801,"audio":true},{"filename":"/audio/step4.wav","crunched":0,"start":2071801,"end":2083473,"audio":true},{"filename":"/audio/wormhit.wav","crunched":0,"start":2083473,"end":2134271,"audio":true},{"filename":"/audio/wormkill.wav","crunched":0,"start":2134271,"end":2258069,"audio":true},{"filename":"/conf.lua","crunched":0,"start":2258069,"end":2258349,"audio":false},{"filename":"/editor.lua","crunched":0,"start":2258349,"end":2276270,"audio":false},{"filename":"/fonts/dotty.ttf","crunched":0,"start":2276270,"end":2328118,"audio":false},{"filename":"/fonts/morningtype.ttf","crunched":0,"start":2328118,"end":2433250,"audio":false},{"filename":"/fonts/pixeled.ttf","crunched":0,"start":2433250,"end":2471790,"audio":false},{"filename":"/geometry.lua","crunched":0,"start":2471790,"end":2471980,"audio":false},{"filename":"/img/icon.png","crunched":0,"start":2471980,"end":2474854,"audio":false},{"filename":"/img/player/attack.png","crunched":0,"start":2474854,"end":2478122,"audio":false},{"filename":"/img/player/canact.png","crunched":0,"start":2478122,"end":2481008,"audio":false},{"filename":"/img/player/fall.png","crunched":0,"start":2481008,"end":2483931,"audio":false},{"filename":"/img/player/idle.png","crunched":0,"start":2483931,"end":2486818,"audio":false},{"filename":"/img/player/idleanim.png","crunched":0,"start":2486818,"end":2489800,"audio":false},{"filename":"/img/player/rise.png","crunched":0,"start":2489800,"end":2492688,"audio":false},{"filename":"/img/player/sit.png","crunched":0,"start":2492688,"end":2495574,"audio":false},{"filename":"/img/player/sleep.png","crunched":0,"start":2495574,"end":2498444,"audio":false},{"filename":"/img/player/walk.png","crunched":0,"start":2498444,"end":2501373,"audio":false},{"filename":"/img/player/walkcanact.png","crunched":0,"start":2501373,"end":2504300,"audio":false},{"filename":"/img/player/wave.png","crunched":0,"start":2504300,"end":2507258,"audio":false},{"filename":"/img/ui/mouse.png","crunched":0,"start":2507258,"end":2510835,"audio":false},{"filename":"/img/world/decal/bomb.png","crunched":0,"start":2510835,"end":2513703,"audio":false},{"filename":"/img/world/decal/chain.png","crunched":0,"start":2513703,"end":2516604,"audio":false},{"filename":"/img/world/decal/pedestal.png","crunched":0,"start":2516604,"end":2519447,"audio":false},{"filename":"/img/world/decal/skulleating.png","crunched":0,"start":2519447,"end":2522465,"audio":false},{"filename":"/img/world/decal/talisman.png","crunched":0,"start":2522465,"end":2525352,"audio":false},{"filename":"/img/world/decal/wormdead.png","crunched":0,"start":2525352,"end":2528231,"audio":false},{"filename":"/img/world/doorclosing.png","crunched":0,"start":2528231,"end":2531248,"audio":false},{"filename":"/img/world/dooropening.png","crunched":0,"start":2531248,"end":2534246,"audio":false},{"filename":"/img/world/doorsign.png","crunched":0,"start":2534246,"end":2537065,"audio":false},{"filename":"/img/world/healthpack.png","crunched":0,"start":2537065,"end":2539964,"audio":false},{"filename":"/img/world/jumppaddown.png","crunched":0,"start":2539964,"end":2542845,"audio":false},{"filename":"/img/world/jumppadup.png","crunched":0,"start":2542845,"end":2545779,"audio":false},{"filename":"/img/world/junk.png","crunched":0,"start":2545779,"end":2548688,"audio":false},{"filename":"/img/world/lens.png","crunched":0,"start":2548688,"end":2551569,"audio":false},{"filename":"/img/world/lensshine.png","crunched":0,"start":2551569,"end":2554576,"audio":false},{"filename":"/img/world/lever.png","crunched":0,"start":2554576,"end":2557450,"audio":false},{"filename":"/img/world/skull.png","crunched":0,"start":2557450,"end":2560326,"audio":false},{"filename":"/img/world/worm.png","crunched":0,"start":2560326,"end":2563443,"audio":false},{"filename":"/img/world/wormbody.png","crunched":0,"start":2563443,"end":2566333,"audio":false},{"filename":"/libs/AnAL/AnAL.lua","crunched":0,"start":2566333,"end":2572211,"audio":false},{"filename":"/libs/AnAL/docs/files/AnAL.html","crunched":0,"start":2572211,"end":2580124,"audio":false},{"filename":"/libs/AnAL/docs/files/animation.html","crunched":0,"start":2580124,"end":2588047,"audio":false},{"filename":"/libs/AnAL/docs/index.html","crunched":0,"start":2588047,"end":2589358,"audio":false},{"filename":"/libs/AnAL/docs/luadoc.css","crunched":0,"start":2589358,"end":2594734,"audio":false},{"filename":"/libs/oldColor.lua","crunched":0,"start":2594734,"end":2595129,"audio":false},{"filename":"/libs/shine/LICENSE","crunched":0,"start":2595129,"end":2596201,"audio":false},{"filename":"/libs/shine/README.md","crunched":0,"start":2596201,"end":2597996,"audio":false},{"filename":"/libs/shine/crt.lua","crunched":0,"start":2597996,"end":2602291,"audio":false},{"filename":"/libs/shine/desaturate.lua","crunched":0,"start":2602291,"end":2604335,"audio":false},{"filename":"/libs/shine/filmgrain.lua","crunched":0,"start":2604335,"end":2606820,"audio":false},{"filename":"/libs/shine/init.lua","crunched":0,"start":2606820,"end":2610133,"audio":false},{"filename":"/libs/shine/scanlines.lua","crunched":0,"start":2610133,"end":2614240,"audio":false},{"filename":"/libs/shine/separate_chroma.lua","crunched":0,"start":2614240,"end":2616294,"audio":false},{"filename":"/main.lua","crunched":0,"start":2616294,"end":2618252,"audio":false},{"filename":"/map.lua","crunched":0,"start":2618252,"end":2675565,"audio":false},{"filename":"/pathfinding.lua","crunched":0,"start":2675565,"end":2678801,"audio":false},{"filename":"/player.lua","crunched":0,"start":2678801,"end":2693883,"audio":false},{"filename":"/ui.lua","crunched":0,"start":2693883,"end":2714436,"audio":false},{"filename":"/world.lua","crunched":0,"start":2714436,"end":2743820,"audio":false}]});

})();
