From cb717b41d08cf310964f1913b7b5b8634612a57a Mon Sep 17 00:00:00 2001 From: Andrea Rendine Date: Mon, 11 May 2015 21:05:08 +0200 Subject: [PATCH] function for fire hut event and more Effect of hut fire is associated with new Outside method .destroyHuts(). --- script/events/outside.js | 5 +---- script/outside.js | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/script/events/outside.js b/script/events/outside.js index 1ffa340..d54a1c4 100644 --- a/script/events/outside.js +++ b/script/events/outside.js @@ -79,10 +79,7 @@ Events.Outside = [ notification: _('a fire has started'), blink: true, onLoad: function() { - var population = $SM.get('game.population', true); - var huts = $SM.get('game.buildings["hut"]', true); - $SM.set('game.buildings["hut"]', (huts - 1)); - Outside.killVillagers(4); + Outside.destroyHuts(1); }, buttons: { 'mourn': { diff --git a/script/outside.js b/script/outside.js index 039ba87..6ab8e2b 100644 --- a/script/outside.js +++ b/script/outside.js @@ -8,6 +8,7 @@ var Outside = { _GATHER_DELAY: 60, _TRAPS_DELAY: 90, _POP_DELAY: [0.5, 3], + _HUT_ROOM: 4, _INCOME: { 'gatherer': { @@ -173,7 +174,7 @@ var Outside = { }, getMaxPopulation: function() { - return $SM.get('game.buildings["hut"]', true) * 4; + return $SM.get('game.buildings["hut"]', true) * Outside._HUT_ROOM; }, increasePopulation: function() { @@ -219,6 +220,36 @@ var Outside = { } }, + destroyHuts: function(num, allowEmpty) { + var dead = 0; + for(var i = 0; i < num; i++){ + var population = $SM.get('game.population', true); + var rate = population / Outside._HUT_ROOM; + var full = Math.floor(rate); + // by default this is used to destroy full or half-full huts + // pass allowEmpty to include empty huts in the armageddon + var huts = (allowEmpty) ? $SM.get('game.buildings["hut"]', true) : Math.ceil(rate); + if(!huts) { + break; + } + // random can be 0 but not 1; however, 0 as a target is useless + var target = Math.floor(Math.random() * huts) + 1; + var inhabitants = 0; + if(target <= full){ + inhabitants = Outside._HUT_ROOM; + } else if(target == full + 1){ + inhabitants = population % Outside._HUT_ROOM; + } + $SM.set('game.buildings["hut"]', ($SM.get('game.buildings["hut"]') - 1)); + if(inhabitants){ + Outside.killVillagers(inhabitants); + dead += inhabitants; + } + } + // this method returns the total number of victims, for further actions + return dead; + }, + schedulePopIncrease: function() { var nextIncrease = Math.floor(Math.random()*(Outside._POP_DELAY[1] - Outside._POP_DELAY[0])) + Outside._POP_DELAY[0]; Engine.log('next population increase scheduled in ' + nextIncrease + ' minutes');