Skip to content

Commit

Permalink
Resources/cache structure rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Jbudone committed Apr 8, 2018
1 parent a37eeb6 commit 91ce077
Show file tree
Hide file tree
Showing 33 changed files with 2,191 additions and 910 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.swo
js/scripts/ready/

sprites/
js/SCRIPT.INJECTION.min.js
dist/
node_modules*
Expand Down
6 changes: 3 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function(grunt){
expand: true,
cwd: 'js/',
src: ['**/*.js','!lib/*'],
dest: 'dist/'
dest: 'dist/js'
}
]
}
Expand All @@ -36,7 +36,7 @@ module.exports = function(grunt){
expand: true,
cwd: 'js/',
src: ['**/*.js','!lib/*'],
dest: 'dist/'
dest: 'dist/js'
}
]
}
Expand All @@ -55,7 +55,7 @@ module.exports = function(grunt){
files: {
cwd: 'js', // set working folder / root to copy
src: '**/*', // copy all files and subfolders
dest: 'dist', // destination folder
dest: 'dist/js', // destination folder
expand: true // required when using cwd
}
},
Expand Down
40 changes: 36 additions & 4 deletions NOTES
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,28 @@ WORKING ON (was previously working on)
- need public/private resources (models, maps, raw sounds, etc.)
- /art, /sfx, /maps: private
- resources: public/build (all json files references shit from here) ; NOTE: some raw files here too (eg. data, json)
- we could have ALL data in /resources, then /build for built stuff
- json/data would also go through caching (EVERYTHING goes through caching, even if just a copy)
- this also allows us to force json changes through a JSON validation system
- OR we could put it under dist/ w/ scripts: dist/js, dist/resources

- /js: scripts
- /resources: dev resources (everything)
- /dist
- /js: compiled js
- /resources: cached/processed assets, validated json
- rename cacheBuilder to resourceBuilder; integrate w/ grunt

2) Rename cacheBuilder -> resourcesBuilder/Processor (rename other things accordingly)
3) Integrate to grunt
4) JSON validator; disallow server to run without validated JSON?
5) cacheBuilder: rawAsset "resources/" shouldn't be necessary, should be root folder for assets
-- reload npcs/json: auto move to dist and watch data files there
-- clean cache.json ( "dataList": [ "resources.json", "sheets.json", ... ] ), OR read data files from resources, maps from world, sheets from sheets, etc. and allow cacheOptions in there then get rid of cache.json entirely :O
- Grunt: watch areahashes.json for changes to copy over maps, etc?
- Should only need to rebuild a specific asset group (maps, images, sounds, etc.) or even specific asset
- ./resourceBuilder --checkNeedsRebuild (or a bash script) to go through all cache and see if a rebuild is required (without doing anything); resources.json contains rawAssetHash for each data, sheets.json, sounds.json, etc. so we can just compare hash w/ file to see if rebuild is needed. Run this on server startup, and don't start if rebuild is needed. Grunt watch resources folder for changes and run this script to see if rebuild is needed, and if so then rebuild. NOTE: Need to run resources.json last since updating a sheet will change sheets.json hash which in turn will update resources.json
-- resources/sprites: bunnyrat.json, bat.json, etc. resources/sprites/models/bunnyrat (actual model here); .gitignore resources/sprites/models
- OUTPUT param to override output (for testing/checking)
- Look into achievedXP: seems like we could surpass our achievedXP while not reaching our achievedLevel, and start messing with our achievedXP. Might even be worth it to netSerialize(owner) for the achievedXP/Level
- Fix model -> spritesheet camera/size issue:
Expand All @@ -437,15 +459,25 @@ WORKING ON (was previously working on)
- Fetch weapon info for calculation (otherwise defaults)
- Separate constants into rules?
- Symbolize item.types (for quicker comparisons than string cmp)
* - BUG: Sometimes enemy respawns w/ netSerialize of its health < 0; we need to not netSerialize ANYTHING before respawn
* - BUG: Refresh the game while buffed w/ deathsickness (error in deactivate buff on client)
* - BUG: Avatars: ordering was mixed up somehow (bat showed up before bunnyrat)
- Avatars: Make avatars larger to fit titles, but center the sprites such that they appear on the tile w/ a decent size
- Spawn bodyguard: spawns a bodyguard (w/ some characterTemplate) w/ AI that follows you and aggros enemies -- could have different AI strategies for different bodyguard types
* - Sheet Editor
- Scrolling through spritesheets/tilesheets
- Resizing canvas (max size)
- Anim preview overflows
- Anim flipX
- Scroll up in chatbox, when something new is added it automatically scrolls down (if we manually scroll up then should disable that)
- Mouse hovering enemy/item: bigger bounding box rather than just the tile
- Need a better mouse icon when hovering item/interactable/npc
- GodMode: cannot die
- Add character template, buffs, etc. to autoreload
- Validate JSON on startup: validate all JSON data files on startup to ensure we don't run into unexpected issues
* - Combat feel:
- enemy runs to you before you've reached them, so you are both out of place
- takes a while to autoattack enemy
- damage feels too late: you get hit at the same time that you kill the enemy?
- Reloading npcs:
- We *CANNOT* cache values from old npcs, or npcs in general, otherwise we keep a stale npcs
- brain/components/etc. of character need to have an initializeNPC routine (find a suitable name!) that runs on startup and when we reload NPCs


- Browser Debugging Snippets
Expand Down
2 changes: 1 addition & 1 deletion build-scripts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ node_modules/.bin/uglifyjs js/SCRIPT.INJECTION.js > js/SCRIPT.INJECTION.min.js
printf "\n" >> js/SCRIPT.INJECTION.min.js

#START=$(date +%s.%N)
for f in dist/scripts/*.js; do
for f in dist/js/scripts/*.js; do
if grep -q "^\s*\/\*\s*SCRIPTINJECT\s*\*\/" $f
then
sed -i '/\/\*\s*SCRIPTINJECT\s*\*\//r js/SCRIPT.INJECTION.min.js' $f
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
</div>
<script>
requirejs.config({
"baseUrl": "dist",
"baseUrl": "dist/js",
"paths": {
// "jquery": "//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min",
"jquery": "lib/jquery-2.1.1.min",
Expand Down
2 changes: 1 addition & 1 deletion js/SCRIPT.INJECTION.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var __scriptInject_definedVar = function(){
// NOTE: Unbuilt scripts have the /*SCRIPTINJECT*/ placeholder; these are replaced with the actual
// SCRIPT.INJECTION.js with build-scripts. If we modify SCRIPT.INJECTION.js then we'll need to rebuild
// scripts from scratch (to get the /*SCRIPTINJECT*/ placeholder back)
throw new Error("We've added a new scripting variable ("+key+") but forgot to declare it in SCRIPT.INJECTION.js. Also may need to remove and rebuild scripts with build-scripts (rm -r dist/scripts && ./build-scripts)");
throw new Error("We've added a new scripting variable ("+key+") but forgot to declare it in SCRIPT.INJECTION.js. Also may need to remove and rebuild scripts with build-scripts (rm -r dist/js/scripts && ./build-scripts)");
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion js/client/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ define(

FX.event('damaged', entity, { amount, health });

if (attackerEntity) {
if (attacker) {
ui.postMessage(`${attacker.npc.name} attacked ${entity.npc.name} for ${amount} damage (${entity.character.health} / ${entity.character.stats.health.curMax})`, styleType);
} else {
ui.postMessage(`${entity.npc.name} damaged for ${amount} by a null source (${entity.character.health} / ${entity.character.stats.health.curMax})`, styleType);
Expand Down
4 changes: 2 additions & 2 deletions js/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ define(function(){
websocket: 'ws://127.0.0.1:1338/',
websocketTest: 'ws://127.0.0.1:1339/',
http: (typeof location != "undefined" ? location.origin : 'http://myquest.local'), // NOTE: server doesn't need this
resources: 'resources/data/resources.json',
resourcesTest: 'resources/data/resources.json'
resources: 'dist/resources/data/resources.json',
resourcesTest: 'dist/resources/data/resources.json'
},

server: {
Expand Down
2 changes: 1 addition & 1 deletion js/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ define(function(){
/// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
/// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

addKeyGroup('ITM', ['USE', 'PICKUP', 'WORN_HELMET', 'WORN_CHEST', 'WIELD_LEFTHAND'], OPT_BITWISE);
addKeyGroup('ITM', ['USE', 'PICKUP', 'WORN_HELMET', 'WORN_CHEST', 'WIELD_LEFTHAND', 'ARMOR'], OPT_BITWISE);
_global['ITM_WEARABLE'] = ITM_WORN_HELMET | ITM_WORN_CHEST | ITM_WIELD_LEFTHAND; // FIXME: Need a cleaner way to do this; would also need it to work within group

/// %%%%%%%%%%%%%%%%%%%%%
Expand Down
4 changes: 3 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ define(
window.Profiler = Profiler;
window.ErrorReporter = ErrorReporter;

window.onerror = errorInGame;
window.addEventListener('error', (e) => {
errorInGame(e.error);
});


// ------------------------------------------------------------------------------------------------------ //
Expand Down
6 changes: 3 additions & 3 deletions js/resourceProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ define(() => {
// binary format, and process/read it accordingly
if (cacheNode.options.cached) {

const assetFile = cacheNode.asset;
const assetFile = '/dist/resources/' + cacheNode.asset;

if (!assetFile) {
failed(`Could not find cache for ${file} (${assetFile})`);
Expand Down Expand Up @@ -99,10 +99,10 @@ define(() => {
};

img.onerror = function() {
throw Err(`Error loading img`);
throw Err(`Error loading img: ${cacheNode.asset}`);
};

const url = cacheNode.asset;
const url = '/dist/resources/' + cacheNode.asset;
img.src = url;
}
});
Expand Down
10 changes: 5 additions & 5 deletions js/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ define(['loggable', 'resourceProcessor'], function(Loggable, ResourceProcessor){
}

this.Log("Loading resource: " + resourceID + "("+resource.file+")");
const file = 'resources/data/' + resource.file;
const file = 'dist/resources/data/' + resource.file;
this.read(file).then((function(data){
assets[resourceID] = data;

Expand Down Expand Up @@ -137,7 +137,7 @@ define(['loggable', 'resourceProcessor'], function(Loggable, ResourceProcessor){
if (script.script) {


var scriptFile = "dist/scripts/"+script.script;
var scriptFile = "dist/js/scripts/"+script.script;
script.name = scriptName;
Log("Loading script: "+scriptFile);
++scriptsToLoad;
Expand All @@ -162,7 +162,7 @@ define(['loggable', 'resourceProcessor'], function(Loggable, ResourceProcessor){
_.each(components, function(componentFile, componentName){
++scriptsToLoad;
Log("Loading script: "+componentFile);
require(["dist/scripts/"+componentFile], function(component){
require(["dist/js/scripts/"+componentFile], function(component){
--scriptsToLoad;
Log("Loaded script: "+this.name+"."+componentName+" waiting on "+scriptsToLoad+" more..");
_interface.scripts[this.name].components[componentName] = component;
Expand Down Expand Up @@ -766,7 +766,7 @@ define(['loggable', 'resourceProcessor'], function(Loggable, ResourceProcessor){

const cacheNode = this.cache.cacheList.find((el) => el.name === imageRes);
assert(cacheNode, `Could not find cache for ${imageRes}`);
const url = cacheNode.asset;
const url = '/dist/resources/' + cacheNode.asset;
img.src = url;

// FIXME: Cache asset, then just return that cached image; we only need 1 instance of each image, but
Expand All @@ -782,7 +782,7 @@ define(['loggable', 'resourceProcessor'], function(Loggable, ResourceProcessor){

const cacheNode = this.cache.cacheList.find((el) => el.name === soundRes);
assert(cacheNode, `Could not find cache for ${soundRes}`);
const src = cacheNode.asset;
const src = '/dist/resources/' + cacheNode.asset;

const request = new XMLHttpRequest();
request.open('GET', src, true);
Expand Down
38 changes: 27 additions & 11 deletions js/scripts/character.ai.combat.melee.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ define(
_script = this;
},

calculateDamage: (attackInfo, target) => {

// FIXME: NPCs need a default inventory
//const weapon = weapons.length > 0 ? weapons[0] : null;
calculateDamage: (attackInfo, defensiveInfo, target) => {

let wpnDamage = attackInfo.wpnDmg,
wpnLevel = attackInfo.wpnLvl;
wpnLevel = attackInfo.wpnLvl,
AC = defensiveInfo.AC;


let strContribution = 0.4;
Expand All @@ -52,8 +50,8 @@ define(


// Resist
let AC = 2.0; // FIXME: Fetch from character armour
let hit = damage * (damage) / (AC + damage);
console.log(`Damage (${damage}), AC (${AC}), hit (${hit})`);

hit = Math.round(hit);
return hit;
Expand All @@ -66,17 +64,18 @@ define(
return false;
}

// FIXME: Abstract damage
const damageData = {},
attackInfo = {
wpnDmg: 1,
wpnLvl: 1,
AC: 2.0
wpnLvl: 1
},
defensiveInfo = {
AC: 0
};

if (character.inventory) {
const weapons = character.inventory.getEquipped('WIELD_LEFTHAND');

const weapons = character.inventory.getEquipped('WIELD_LEFTHAND');
if (weapons.length > 0) {
const weapon = weapons[0];

Expand All @@ -95,8 +94,25 @@ define(
attackInfo.wpnLvl = _.defaultTo(character.entity.npc.attackInfo.wpnLvl, attackInfo.wpnDmg);
}
}

if (target.inventory) {

const armors = target.inventory.getEquipped('ARMOR');
for (let i = 0; i < armors.length; ++i) {
const armor = armors[i];
attackInfo.AC += armor.args.ac;
}
} else {

// Default attackInfo on npc?
if (target.entity.npc.attackInfo) {
defensiveInfo.AC = _.defaultTo(target.entity.npc.attackInfo.AC, defensiveInfo.AC);
}
}


const damage = this.calculateDamage(attackInfo, target);
// FIXME: Abstract damage
const damage = this.calculateDamage(attackInfo, defensiveInfo, target);
this.Log(` I TOTALLY SCRATCHED YOU FOR ${damage}`, LOG_DEBUG);

target.damage(damage, character, damageData);
Expand Down
2 changes: 1 addition & 1 deletion js/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ requirejs(['keys', 'environment'], (Keys, Environment) => {
// Load Areas
_.each(json.areas, (areaFile, areaID) => {
loading(`area ${areaID}`);
fs.readFile(`resources/maps/${areaFile}`, (e, areaRawData) => {
fs.readFile(`dist/resources/maps/${areaFile}`, (e, areaRawData) => {

if (e) errorInGame(e);

Expand Down
4 changes: 2 additions & 2 deletions js/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ var jsdom = require('jsdom').jsdom;
++x;
let connectOn = 9222 + x;

const botOptions = ['./dist/test/bot2.js'];
const botOptions = ['./dist/js/test/bot2.js'];
if (options.debug) {
botOptions.unshift('--inspect=' + connectOn, '--debug-brk');
}

const bot = spawn('node', botOptions, { stdio: ['pipe', 'pipe', 'pipe', 'ipc'] });

// Detached/Unref doesn't seem to kill bot when we kill parent and attempt to kill bot
//const bot = spawn('node', ['--inspect', '--debug-brk', './dist/test/bot2.js'], { detached: true, stdio: ['pipe', 'pipe', 'pipe', 'ipc'] });
//const bot = spawn('node', ['--inspect', '--debug-brk', './dist/js/test/bot2.js'], { detached: true, stdio: ['pipe', 'pipe', 'pipe', 'ipc'] });
//bot.unref();

// Pipe stdout to process stdout
Expand Down
Loading

0 comments on commit 91ce077

Please sign in to comment.