Fixed various bugs with debug worker

This commit is contained in:
Michael Schmatz 2014-05-01 11:12:44 -07:00
parent 544cc62cdb
commit c9ea301b29
3 changed files with 21 additions and 31 deletions

View file

@ -33,9 +33,9 @@ if (!Function.prototype.bind) {
// assign global window so that Brunch's require (in world.js) can go into it
self.window = self;
self.workerID = "Worker";
self.workerID = "DebugWorker";
self.logLimit = 200;
self.logLimit = 2000;
self.logsLogged = 0;
var console = {
log: function() {
@ -85,41 +85,25 @@ var GoalManager = self.require('lib/world/GoalManager');
self.getCurrentFrame = function getCurrentFrame(args) { return self.world.frames.length; };
self.runWorld = function runWorld(args) {
self.postedErrors = {};
self.t0 = new Date();
self.firstWorld = args.firstWorld;
self.postedErrors = false;
self.logsLogged = 0;
try {
self.world = new World(args.worldName, args.userCodeMap);
if(args.level)
self.world.loadFromLevel(args.level, true);
self.goalManager = new GoalManager(self.world);
self.goalManager.setGoals(args.goals);
self.goalManager.setCode(args.userCodeMap);
self.goalManager.worldGenerationWillBegin();
self.world.setGoalManager(self.goalManager);
}
catch (error) {
self.onWorldError(error);
return;
}
Math.random = self.world.rand.randf; // so user code is predictable
self.world.loadFrames(self.onWorldLoaded, self.onWorldError, self.onWorldLoadProgress);
};
//optimize this later
self.currentUserCodeMap = {};
self.currentWorldFrame = 0;
self.runWorldUntilFrame = function runWorldUntilFrame(args) {
console.log("Running world until frame " + args.frame);
self.postedErrors = {};
self.t0 = new Date();
self.firstWorld = args.firstWorld;
self.postedErrors = false;
self.logsLogged = 0;
if (!self.world)
var userCodeMapHasChanged = _.isEqual(self.currentUserCodeMap, args.userCodeMap);
self.currentUserCodeMap = args.userCodeMap;
console.log("User codemap has changed: " + userCodeMapHasChanged);
if (!self.world || userCodeMapHasChanged || args.frame < self.currentWorldFrame)
{
try {
self.world = new World(args.worldName, args.userCodeMap);
self.world = new World(args.worldName, self.currentUserCodeMap);
if(args.level)
self.world.loadFromLevel(args.level, true);
self.goalManager = new GoalManager(self.world);
@ -138,6 +122,8 @@ self.runWorldUntilFrame = function runWorldUntilFrame(args) {
self.world.totalFrames = args.frame; //hack to work around error checking
self.world.loadFramesUntilFrame(args.frame, self.onWorldLoaded, self.onWorldError, self.onWorldLoadProgress);
self.currentWorldFrame = args.frame;
};
self.onWorldLoaded = function onWorldLoaded() {
@ -204,6 +190,7 @@ self.reportIn = function reportIn() {
}
self.addEventListener('message', function(event) {
console.log("received message!")
self[event.data.func](event.data.args);
});

View file

@ -25,6 +25,7 @@ module.exports = class God
@fillWorkerPool()
#TODO: have this as a constructor option
@debugWorker = @createDebugWorker()
@currentUserCodeMap = {}
onTomeCast: (e) ->
return if @dead
@ -71,7 +72,7 @@ module.exports = class God
when "worker-initialized"
worker.initialized = true
when 'new-debug-world'
console.log "Created new debug world!"
console.log "New Debug world!"
when 'console-log'
console.log "|" + @id + "'s " + @id + "|", event.data.args...
@ -131,7 +132,7 @@ module.exports = class God
func : 'runWorldUntilFrame'
args:
worldName: @level.name
userCodeMap: @getUserCodeMap()
userCodeMap: @currentUserCodeMap
level: @level
firstWorld: @firstWorld
goals: @goalManager?.getGoals()
@ -170,6 +171,7 @@ module.exports = class God
for spellKey, spell of @spells
for thangID, spellThang of spell.thangs
(userCodeMap[thangID] ?= {})[spell.name] = spellThang.aether.serialize()
@currentUserCodeMap = userCodeMap
userCodeMap
destroy: ->

View file

@ -40,6 +40,7 @@ module.exports = class World
else if frameIndex
frame = frames[frameIndex - 1].getNextFrame()
frames.push frame
console.log "Pushed frame #{frameIndex}"
else
frame = frames[0]
@age = frameIndex * @dt
@ -129,7 +130,6 @@ module.exports = class World
@t0 = t2
setTimeout((=> @loadFrames(loadedCallback, errorCallback, loadProgressCallback)), 0)
return
@ended = true
loadProgressCallback? 1
loadedCallback()
@ -278,6 +278,7 @@ module.exports = class World
serialize: ->
# Code hotspot; optimize it
console.log("Frames length: #{@frames.length}, total frames: #{@totalFrames}")
if @frames.length < @totalFrames then throw new Error("World Should Be Over Before Serialization")
[transferableObjects, nontransferableObjects] = [0, 0]
o = {name: @name, totalFrames: @totalFrames, maxTotalFrames: @maxTotalFrames, frameRate: @frameRate, dt: @dt, victory: @victory, userCodeMap: {}, trackedProperties: {}}