Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Merge pull request #68 from vthibault/memory-optimization

Memory optimization
  • Loading branch information...
vthibault committed Aug 6, 2014
2 parents efc853e + a821756 commit d33283cf25e4271e65b948224f10d9aa5f01ab27
@@ -23,27 +23,32 @@ function( Client, Preferences, Memory )
var _sounds = [];


/**
* Re-usable sounds
*/
var _cache = [];


/**
* @Constructor
*/
var Sound = {};
var SoundManager = {};


/**
* @var {float} sound volume
*
*/
Sound.volume = Preferences.Sound.volume;
SoundManager.volume = Preferences.Sound.volume;


/**
* Play a wav sound
*
* @param {string} filename
* @param {optional|number} vol (volume)
* @param {optional|boolean} auto-repeat
*/
Sound.play = function play( filename, vol, repeat ) {
SoundManager.play = function play( filename, vol ) {
var volume;

// Sound volume * Global volume
@@ -59,30 +64,39 @@ function( Client, Preferences, Memory )
return;
}

// Re-usable sound
var sound = getSoundFromCache(filename);
if (sound) {
sound.volume = Math.min(volume,1.0);
sound._volume = volume;
sound.tick = Date.now();
sound.play();
_sounds.push(sound);
return;
}

// Get the sound from client.
Client.loadFile( 'data/wav/' + filename, function( url ) {
var sound = document.createElement('audio');
var i, count = _sounds.length;
var sound, tick = Date.now();

// Wait a delay to replay a sound
for (i = 0; i < count; ++i) {
if (_sounds[i].src === url && _sounds[i].tick > tick - 100) {
return;
}
}

// Initialiaze the sound and play it
sound = document.createElement('audio');
sound.filename = filename;
sound.src = url;
sound.tick = tick;
sound.volume = Math.min(volume,1.0);
sound._volume = volume;
sound.play();

// Once the sound finish, remove it from memory
sound.addEventListener('ended', function Remove(){
var pos = _sounds.indexOf(this);
if (pos !== -1) {
if (repeat) {
sound.currentTime = 0;
sound.play();
}
else {
_sounds.splice( pos, 1 );
}
}
}, false);
sound.addEventListener('ended', onSoundEnded, false);
sound.play();

// Add it to the list
_sounds.push(sound);
@@ -95,7 +109,7 @@ function( Client, Preferences, Memory )
*
* @param {optional|string} filename to stop
*/
Sound.stop = function stop( filename )
SoundManager.stop = function stop( filename )
{
var i, count, list;

@@ -130,7 +144,7 @@ function( Client, Preferences, Memory )
*
* @param {number} volume
*/
Sound.setVolume = function setVolume( volume )
SoundManager.setVolume = function setVolume( volume )
{
var i, count = _sounds.length;
this.volume = Math.min( volume, 1.0);
@@ -144,9 +158,62 @@ function( Client, Preferences, Memory )
};


/**
* Move sound to cache.
* ff we have a request to play the same sound again, get it back
* Will avoid to re-create sound object at each request (re-usable object)
*/
function onSoundEnded()
{
var pos = _sounds.indexOf(this);

if (pos !== -1) {
_sounds.splice( pos, 1);
}

this.tick = Date.now();
_cache.push(this);
}


/**
* Remove sound from cache and return it
* Check at the same time to remove sound not used since some times.
*
* @param {string} filename
* @param {Audio} sound element
*/
function getSoundFromCache(filename)
{
var i, tick = Date.now(), count = _cache.length;
var out = null;

for (i = 0; i < count; i++) {
if (!out && _cache[i].filename === filename) {
out = _cache[i];
out.tick = tick;
_cache.splice(i, 1);
i--;
count--;
continue;
}

// remove
if (_cache[i].tick + 60000 < tick) {
_cache.splice(i, 1);
i--;
count--;
continue;
}
}

return out;
}


/**
* Export
*/
return Sound;
return SoundManager;

});
@@ -309,8 +309,8 @@ define(function( require )
Session.moveAction = pkt;

pkt = new PACKET.CZ.REQUEST_MOVE();
pkt.dest[0] = out[ count - 1 ][0];
pkt.dest[1] = out[ count - 1 ][1];
pkt.dest[0] = out[(count-1)*2 + 0];
pkt.dest[1] = out[(count-1)*2 + 1];
Network.sendPacket(pkt);
return true;
}
@@ -368,13 +368,7 @@ define(function( require )
case 1:
Sound.play( pkt.fileName );
break;
/*
// Play repeat
// Not supported !
case 1:
Sound.play( pkt.fileName, Sound.volume, true );
break;
*/

// From rathena, should stop a sound but doesn't seems to work in official client ?
case 2:
Sound.stop( pkt.fileName );
@@ -464,8 +464,8 @@ define(function( require )

// Move to position
pkt = new PACKET.CZ.REQUEST_MOVE();
pkt.dest[0] = out[ count - 1 ][0];
pkt.dest[1] = out[ count - 1 ][1];
pkt.dest[0] = out[(count-1)*2 + 0];
pkt.dest[1] = out[(count-1)*2 + 1];
Network.sendPacket(pkt);
};

@@ -524,8 +524,8 @@ define(function( require )
// Save the packet and move to the position
Session.moveAction = pkt;
pkt = new PACKET.CZ.REQUEST_MOVE();
pkt.dest[0] = out[ count - 1 ][0];
pkt.dest[1] = out[ count - 1 ][1];
pkt.dest[0] = out[(count-1)*2 + 0];
pkt.dest[1] = out[(count-1)*2 + 1];
Network.sendPacket(pkt);
};

@@ -322,8 +322,14 @@ define(function( require )
}

// Parse packet
var data = new packet.struct(fp, offset);
console.log( '%c[Network] Recv:', 'color:#900090', data, packet.callback ? '' : '(no callback)' );
if (!packet.instance) {
packet.instance = new packet.struct(fp, offset);
}
else {
packet.struct.call(packet.instance, fp, offset);
}

console.log( '%c[Network] Recv:', 'color:#900090', packet.instance, packet.callback ? '' : '(no callback)' );

// Support for "0" type
if (length) {
@@ -332,7 +338,7 @@ define(function( require )

// Call controller
if (packet.callback) {
packet.callback(data);
packet.callback(packet.instance);
}
}

@@ -36,9 +36,11 @@ define( function( require )
{
this.speed = 150;
this.tick = 0;
this.path = [];
this.path = new Int16Array(32*2);
this.pos = new Float32Array(3);
this.onEnd = null;
this.index = 0;
this.total = 0;
}


@@ -51,23 +53,24 @@ define( function( require )
* @param {number} to_y
* @param {number} range optional
*/
function WalkTo( from_x, from_y, to_x, to_y, range )
function walkTo( from_x, from_y, to_x, to_y, range )
{
var path = [];
var count = PathFinding.search( from_x | 0, from_y | 0, to_x | 0, to_y | 0, range || 0, path );
var path = this.walk.path;
var total = PathFinding.search( from_x | 0, from_y | 0, to_x | 0, to_y | 0, range || 0, path);

if (count) {
path.length = count;
path.shift();
this.walk.index = 1 * 2; // skip first index
this.walk.total = total * 2;

if (count === 2 && path[0][0] === from_x && path[0][1] === from_y){
if (total) {
// Same position
if (total === 2 &&
path[this.walk.index+0] === from_x &&
path[this.walk.index+1] === from_y){
return;
}

this.walk.pos.set(this.position);
this.walk.path = path;
this.walk.tick = Renderer.tick;

this.headDir = 0;

if (this.action !== this.ACTION.WALK) {
@@ -85,22 +88,24 @@ define( function( require )
/**
* Process walking
*/
function WalkProcess()
function walkProcess()
{
var pos = this.position;
var walk = this.walk;
var path = walk.path;
var index = walk.index;
var total = walk.total;

var x, y, speed;
var TICK = Renderer.tick;
var delay = 0;

if (path.length) {
if (index < total) {

// Calculate new position, base on time and walk speed.
while (path.length) {
x = path[0][0] - (walk.pos[0]);
y = path[0][1] - (walk.pos[1]);
while (index < total) {
x = path[index+0] - (walk.pos[0]);
y = path[index+1] - (walk.pos[1]);

// Seems like walking on diagonal is slower ?
speed = ( x && y ) ? walk.speed / 0.6 : walk.speed;
@@ -111,13 +116,16 @@ define( function( require )
}

walk.tick += speed;
walk.pos.set(path.shift());
walk.pos[0] = path[index+0];
walk.pos[1] = path[index+1];
index += 2;
}

// Calculate and store new position
// TODO: check the min() part.

delay = Math.min(speed, TICK-walk.tick);
delay = Math.min(speed, TICK-walk.tick);
walk.index = index;

// Should not happened, avoid division by 0
if (!delay) {
@@ -129,7 +137,7 @@ define( function( require )
pos[2] = Altitude.getCellHeight( pos[0], pos[1] );

// Update player direction while walking
if (path.length) {
if (index < total) {
this.direction = DIRECTION[(x>0?1:x<0?-1:0)+1][(y>0?1:y<0?-1:0)+1];
return;
}
@@ -165,8 +173,8 @@ define( function( require )
{
this.onWalkEnd = function onWalkEnd(){};
this.walk = new WalkStructure();
this.walkTo = WalkTo;
this.walkProcess = WalkProcess;
this.walkTo = walkTo;
this.walkProcess = walkProcess;
};
});

0 comments on commit d33283c

Please sign in to comment.
You can’t perform that action at this time.