mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-03-22 10:55:19 -04:00
Forward simulation optimization
This commit is contained in:
parent
6d8d119c94
commit
66cc427417
3 changed files with 34 additions and 56 deletions
app
|
@ -223,7 +223,7 @@ self.retrieveValueFromFrame = function retrieveValueFromFrame(args) {
|
|||
var flowStates = self.debugWorld.userCodeMap[currentThangID][currentSpellID].flow.states;
|
||||
//we have to go to the second last flowState as we run the world for one additional frame
|
||||
//to collect the flow
|
||||
value = _.last(flowStates[flowStates.length - 2].statements).variables[prop];
|
||||
value = _.last(flowStates[flowStates.length - 1].statements).variables[prop];
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
|
@ -260,11 +260,12 @@ self.retrieveValueFromFrame = function retrieveValueFromFrame(args) {
|
|||
};
|
||||
self.enableFlowOnThangSpell(args.currentThangID, args.currentSpellID, args.userCodeMap);
|
||||
self.setupDebugWorldToRunUntilFrame(args);
|
||||
self.debugWorld.loadFramesUntilFrame(
|
||||
args.frame,
|
||||
self.debugWorld.loadFrames(
|
||||
retrieveProperty.bind({},args.currentThangID, args.currentSpellID, args.variableChain),
|
||||
self.onDebugWorldError,
|
||||
self.onDebugWorldProgress
|
||||
self.onDebugWorldProgress,
|
||||
false,
|
||||
args.frame
|
||||
);
|
||||
};
|
||||
|
||||
|
@ -297,8 +298,8 @@ self.setupDebugWorldToRunUntilFrame = function (args) {
|
|||
var stringifiedUserCodeMap = JSON.stringify(args.userCodeMap);
|
||||
var userCodeMapHasChanged = ! _.isEqual(self.currentUserCodeMapCopy, stringifiedUserCodeMap);
|
||||
self.currentUserCodeMapCopy = stringifiedUserCodeMap;
|
||||
if (!self.debugWorld || userCodeMapHasChanged || args.frame != self.currentDebugWorldFrame) {
|
||||
self.invalidateCache();
|
||||
if (args.frame != self.currentDebugWorldFrame) self.invalidateCache();
|
||||
if (!self.debugWorld || userCodeMapHasChanged || args.frame < self.currentDebugWorldFrame) {
|
||||
try {
|
||||
self.debugWorld = new World(args.worldName, args.userCodeMap);
|
||||
if (args.level)
|
||||
|
@ -314,33 +315,33 @@ self.setupDebugWorldToRunUntilFrame = function (args) {
|
|||
return;
|
||||
}
|
||||
Math.random = self.debugWorld.rand.randf; // so user code is predictable
|
||||
|
||||
self.debugWorld.totalFrames = args.frame; //hack to work around error checking
|
||||
self.currentDebugWorldFrame = args.frame;
|
||||
}
|
||||
self.debugWorld.totalFrames = args.frame; //hack to work around error checking
|
||||
self.currentDebugWorldFrame = args.frame;
|
||||
};
|
||||
self.runDebugWorldUntilFrame = function (args) {
|
||||
self.setupDebugWorldToRunUntilFrame(args);
|
||||
|
||||
self.debugWorld.loadFramesUntilFrame(args.frame, self.onDebugWorldLoaded, self.onDebugWorldError, self.onDebugWorldProgress);
|
||||
|
||||
};
|
||||
|
||||
self.onDebugWorldLoaded = function onDebugWorldLoaded() {
|
||||
console.log("World loaded!");
|
||||
};
|
||||
|
||||
self.onDebugWorldError = function onDebugWorldError(error) {
|
||||
if(error instanceof Aether.problems.UserCodeProblem) {
|
||||
if(!self.debugPostedErrors[error.key]) {
|
||||
var problem = error.serialize();
|
||||
self.postMessage({type: 'user-code-problem', problem: problem});
|
||||
self.debugPostedErrors[error.key] = problem;
|
||||
if(error.isUserCodeProblem) {
|
||||
var errorKey = error.userInfo.key;
|
||||
if(!errorKey || !self.debugPostedErrors[errorKey]) {
|
||||
self.postMessage({type: 'user-code-problem', problem: error});
|
||||
self.debugPostedErrors[errorKey] = error;
|
||||
}
|
||||
}
|
||||
else {
|
||||
console.log("Non-UserCodeError:", error.toString() + "\n" + error.stack || error.stackTrace);
|
||||
}
|
||||
/* We don't actually have the recoverable property any more; hmm
|
||||
if(!self.firstWorld && !error.recoverable) {
|
||||
self.abort();
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
return true;
|
||||
};
|
||||
|
||||
|
|
|
@ -575,8 +575,6 @@ module.exports = Surface = class Surface extends CocoClass
|
|||
@paths.parent.removeChild @paths
|
||||
@paths = null
|
||||
|
||||
# Screenshot
|
||||
|
||||
screenshot: (scale=0.25, format='image/jpeg', quality=0.8, zoom=2) ->
|
||||
# Quality doesn't work with image/png, just image/jpeg and image/webp
|
||||
[w, h] = [@camera.canvasWidth, @camera.canvasHeight]
|
||||
|
@ -586,6 +584,5 @@ module.exports = Surface = class Surface extends CocoClass
|
|||
#console.log "Screenshot with scale", scale, "format", format, "quality", quality, "was", Math.floor(imageData.length / 1024), "kB"
|
||||
screenshot = document.createElement("img")
|
||||
screenshot.src = imageData
|
||||
#$('body').append(screenshot)
|
||||
@stage.uncache()
|
||||
imageData
|
||||
|
|
|
@ -71,14 +71,18 @@ module.exports = class World
|
|||
(@runtimeErrors ?= []).push error
|
||||
(@unhandledRuntimeErrors ?= []).push error
|
||||
|
||||
loadFrames: (loadedCallback, errorCallback, loadProgressCallback, skipDeferredLoading) ->
|
||||
loadFrames: (loadedCallback, errorCallback, loadProgressCallback, skipDeferredLoading, loadUntilFrame) ->
|
||||
return if @aborted
|
||||
unless @thangs.length
|
||||
console.log "Warning: loadFrames called on empty World (no thangs)."
|
||||
t1 = now()
|
||||
@t0 ?= t1
|
||||
if frameToLoadUntil
|
||||
frameToLoadUntil = loadUntilFrame + 1
|
||||
else
|
||||
frameToLoadUntil = @totalFrames
|
||||
i = @frames.length
|
||||
while i < @totalFrames
|
||||
while i < frameToLoadUntil
|
||||
try
|
||||
@getFrame(i)
|
||||
++i # increment this after we have succeeded in getting the frame, otherwise we'll have to do that frame again
|
||||
|
@ -95,43 +99,19 @@ module.exports = class World
|
|||
if t2 - @t0 > 1000
|
||||
console.log(' Loaded', i, 'of', @totalFrames, "(+" + (t2 - @t0).toFixed(0) + "ms)")
|
||||
@t0 = t2
|
||||
continueFn = => @loadFrames(loadedCallback, errorCallback, loadProgressCallback, skipDeferredLoading)
|
||||
continueFn = =>
|
||||
if loadUntilFrame
|
||||
@loadFrames(loadedCallback,errorCallback,loadProgressCallback, skipDeferredLoading, loadUntilFrame)
|
||||
else
|
||||
@loadFrames(loadedCallback, errorCallback, loadProgressCallback, skipDeferredLoading)
|
||||
if skipDeferredLoading
|
||||
continueFn()
|
||||
else
|
||||
setTimeout(continueFn, 0)
|
||||
return
|
||||
@ended = true
|
||||
system.finish @thangs for system in @systems
|
||||
loadProgressCallback? 1
|
||||
loadedCallback()
|
||||
|
||||
loadFramesUntilFrame: (frameToLoadUntil, loadedCallback, errorCallback, loadProgressCallback) ->
|
||||
return if @aborted
|
||||
unless @thangs.length
|
||||
console.log "Warning: loadFrames called on empty World"
|
||||
t1 = now()
|
||||
@t0 ?= t1
|
||||
i = @frames.length
|
||||
while i <= frameToLoadUntil #state is gathered at next frame
|
||||
try
|
||||
@getFrame(i)
|
||||
++i # increment this after we have succeeded in getting the frame, otherwise we'll have to do that frame again
|
||||
catch error
|
||||
# Not an Aether.errors.UserCodeError; maybe we can't recover
|
||||
@addError error
|
||||
for error in (@unhandledRuntimeErrors ? [])
|
||||
return unless errorCallback error # errorCallback tells us whether the error is recoverable
|
||||
@unhandledRuntimeErrors = []
|
||||
t2 = now()
|
||||
if t2 - t1 > PROGRESS_UPDATE_INTERVAL
|
||||
loadProgressCallback? i / @totalFrames
|
||||
t1 = t2
|
||||
if t2 - @t0 > 1000
|
||||
console.log(' Loaded', i, 'of', frameToLoadUntil, "(+" + (t2 - @t0).toFixed(0) + "ms)")
|
||||
@t0 = t2
|
||||
setTimeout((=> @loadFrames(loadedCallback, errorCallback, loadProgressCallback)), 0)
|
||||
return
|
||||
unless loadUntilFrame
|
||||
@ended = true
|
||||
system.finish @thangs for system in @systems
|
||||
loadProgressCallback? 1
|
||||
loadedCallback()
|
||||
|
||||
|
|
Loading…
Reference in a new issue