
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']('/', 'fonts', true, true);
      Module['FS_createPath']('/', 'sound', true, true);
      Module['FS_createPath']('/sound', 'music', true, true);
      Module['FS_createPath']('/', 'textures', true, true);
      Module['FS_createPath']('/textures', 'grenade', true, true);
      Module['FS_createPath']('/textures', 'monkey', true, true);
      Module['FS_createPath']('/textures/monkey', 'hat', true, true);
      Module['FS_createPath']('/textures', 'ui', true, true);
      Module['FS_createPath']('/textures', 'world', 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":"b343dd28-77bc-4f45-b957-43008cd3b40b","remote_package_size":9535278,"files":[{"filename":"/.DS_Store","crunched":0,"start":0,"end":6148,"audio":false},{"filename":"/AnAL.lua","crunched":0,"start":6148,"end":12026,"audio":false},{"filename":"/Archive.zip","crunched":0,"start":12026,"end":3512382,"audio":false},{"filename":"/conf.lua","crunched":0,"start":3512382,"end":3512577,"audio":false},{"filename":"/exeicon.ico","crunched":0,"start":3512577,"end":3536135,"audio":false},{"filename":"/exeicon.png","crunched":0,"start":3536135,"end":3536990,"audio":false},{"filename":"/fonts/Pixeltype.ttf","crunched":0,"start":3536990,"end":3550690,"audio":false},{"filename":"/grenade.lua","crunched":0,"start":3550690,"end":3555553,"audio":false},{"filename":"/icon.png","crunched":0,"start":3555553,"end":3556169,"audio":false},{"filename":"/level.lua","crunched":0,"start":3556169,"end":3561099,"audio":false},{"filename":"/main.lua","crunched":0,"start":3561099,"end":3562282,"audio":false},{"filename":"/monkey.lua","crunched":0,"start":3562282,"end":3571054,"audio":false},{"filename":"/music.lua","crunched":0,"start":3571054,"end":3571517,"audio":false},{"filename":"/oldColor.lua","crunched":0,"start":3571517,"end":3571912,"audio":false},{"filename":"/sound/click.ogg","crunched":0,"start":3571912,"end":3582783,"audio":true},{"filename":"/sound/explosion1.ogg","crunched":0,"start":3582783,"end":3633477,"audio":true},{"filename":"/sound/explosion2.ogg","crunched":0,"start":3633477,"end":3685872,"audio":true},{"filename":"/sound/grenade1.ogg","crunched":0,"start":3685872,"end":3699955,"audio":true},{"filename":"/sound/horn.ogg","crunched":0,"start":3699955,"end":3746179,"audio":true},{"filename":"/sound/jump1.ogg","crunched":0,"start":3746179,"end":3757983,"audio":true},{"filename":"/sound/music/AlbumArtSmall.jpg","crunched":0,"start":3757983,"end":3765123,"audio":false},{"filename":"/sound/music/Folder.jpg","crunched":0,"start":3765123,"end":3793180,"audio":false},{"filename":"/sound/music/Grenades!.mp3","crunched":0,"start":3793180,"end":7918694,"audio":true},{"filename":"/sound/walk1.ogg","crunched":0,"start":7918694,"end":7926746,"audio":true},{"filename":"/textures/grenade/explosion.png","crunched":0,"start":7926746,"end":7954448,"audio":false},{"filename":"/textures/grenade/explosion.psd","crunched":0,"start":7954448,"end":8049118,"audio":false},{"filename":"/textures/grenade/explosionmark.png","crunched":0,"start":8049118,"end":8052197,"audio":false},{"filename":"/textures/grenade/explosionmark.psd","crunched":0,"start":8052197,"end":8096105,"audio":false},{"filename":"/textures/grenade/grenade.png","crunched":0,"start":8096105,"end":8096726,"audio":false},{"filename":"/textures/grenade/grenade.psd","crunched":0,"start":8096726,"end":8120476,"audio":false},{"filename":"/textures/monkey/hat/1.png","crunched":0,"start":8120476,"end":8121019,"audio":false},{"filename":"/textures/monkey/hat/2.png","crunched":0,"start":8121019,"end":8121630,"audio":false},{"filename":"/textures/monkey/hat/3.png","crunched":0,"start":8121630,"end":8122266,"audio":false},{"filename":"/textures/monkey/hat/4.png","crunched":0,"start":8122266,"end":8122783,"audio":false},{"filename":"/textures/monkey/monkey_idle.png","crunched":0,"start":8122783,"end":8123546,"audio":false},{"filename":"/textures/monkey/monkey_idle.psd","crunched":0,"start":8123546,"end":8143918,"audio":false},{"filename":"/textures/monkey/monkey_walking.png","crunched":0,"start":8143918,"end":8144847,"audio":false},{"filename":"/textures/monkey/monkey_walking.psd","crunched":0,"start":8144847,"end":8167095,"audio":false},{"filename":"/textures/monkey/p1marker.png","crunched":0,"start":8167095,"end":8167676,"audio":false},{"filename":"/textures/monkey/p1marker.psd","crunched":0,"start":8167676,"end":8189202,"audio":false},{"filename":"/textures/monkey/p2marker.png","crunched":0,"start":8189202,"end":8189787,"audio":false},{"filename":"/textures/monkey/p2marker.psd","crunched":0,"start":8189787,"end":8211319,"audio":false},{"filename":"/textures/ui/controls1.png","crunched":0,"start":8211319,"end":8213999,"audio":false},{"filename":"/textures/ui/controls2.png","crunched":0,"start":8213999,"end":8216825,"audio":false},{"filename":"/textures/ui/menu.png","crunched":0,"start":8216825,"end":8222950,"audio":false},{"filename":"/textures/ui/menu.psd","crunched":0,"start":8222950,"end":8329620,"audio":false},{"filename":"/textures/ui/music.png","crunched":0,"start":8329620,"end":8330157,"audio":false},{"filename":"/textures/ui/noaudio.png","crunched":0,"start":8330157,"end":8330688,"audio":false},{"filename":"/textures/ui/pause.png","crunched":0,"start":8330688,"end":8336530,"audio":false},{"filename":"/textures/ui/restarthint.psd","crunched":0,"start":8336530,"end":8424540,"audio":false},{"filename":"/textures/ui/restarthintmob.png","crunched":0,"start":8424540,"end":8430657,"audio":false},{"filename":"/textures/ui/restarthintpc.png","crunched":0,"start":8430657,"end":8436830,"audio":false},{"filename":"/textures/ui/sound.png","crunched":0,"start":8436830,"end":8437377,"audio":false},{"filename":"/textures/ui/startscreen.psd","crunched":0,"start":8437377,"end":8650577,"audio":false},{"filename":"/textures/ui/startscreenmob.png","crunched":0,"start":8650577,"end":8663789,"audio":false},{"filename":"/textures/ui/startscreenpc.png","crunched":0,"start":8663789,"end":8672528,"audio":false},{"filename":"/textures/world/background1.png","crunched":0,"start":8672528,"end":8677928,"audio":false},{"filename":"/textures/world/background1.psd","crunched":0,"start":8677928,"end":8762681,"audio":false},{"filename":"/textures/world/background1_drawtext.png","crunched":0,"start":8762681,"end":8768514,"audio":false},{"filename":"/textures/world/background1_drawtext.psd","crunched":0,"start":8768514,"end":8851202,"audio":false},{"filename":"/textures/world/background1_fighttext.png","crunched":0,"start":8851202,"end":8857120,"audio":false},{"filename":"/textures/world/background1_fighttext.psd","crunched":0,"start":8857120,"end":8934968,"audio":false},{"filename":"/textures/world/background1_front.png","crunched":0,"start":8934968,"end":8940771,"audio":false},{"filename":"/textures/world/background1_front.psd","crunched":0,"start":8940771,"end":9018637,"audio":false},{"filename":"/textures/world/background1_num1.png","crunched":0,"start":9018637,"end":9024376,"audio":false},{"filename":"/textures/world/background1_num1.psd","crunched":0,"start":9024376,"end":9101114,"audio":false},{"filename":"/textures/world/background1_num2.png","crunched":0,"start":9101114,"end":9106886,"audio":false},{"filename":"/textures/world/background1_num2.psd","crunched":0,"start":9106886,"end":9184292,"audio":false},{"filename":"/textures/world/background1_num3.png","crunched":0,"start":9184292,"end":9190062,"audio":false},{"filename":"/textures/world/background1_num3.psd","crunched":0,"start":9190062,"end":9268102,"audio":false},{"filename":"/textures/world/background1_p1wontext.png","crunched":0,"start":9268102,"end":9273972,"audio":false},{"filename":"/textures/world/background1_p1wontext.psd","crunched":0,"start":9273972,"end":9356588,"audio":false},{"filename":"/textures/world/background1_p2wontext.png","crunched":0,"start":9356588,"end":9362486,"audio":false},{"filename":"/textures/world/background1_p2wontext.psd","crunched":0,"start":9362486,"end":9448286,"audio":false},{"filename":"/textures/world/walltile1.png","crunched":0,"start":9448286,"end":9448780,"audio":false},{"filename":"/textures/world/walltile1.psd","crunched":0,"start":9448780,"end":9466687,"audio":false},{"filename":"/textures/world/walltile2.png","crunched":0,"start":9466687,"end":9467178,"audio":false},{"filename":"/textures/world/walltile2.psd","crunched":0,"start":9467178,"end":9489113,"audio":false},{"filename":"/textures/world/walltile3.png","crunched":0,"start":9489113,"end":9489607,"audio":false},{"filename":"/textures/world/walltile3.psd","crunched":0,"start":9489607,"end":9508376,"audio":false},{"filename":"/ui.lua","crunched":0,"start":9508376,"end":9524632,"audio":false},{"filename":"/world.lua","crunched":0,"start":9524632,"end":9535278,"audio":false}]});

})();
