Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
map_coordinates = function(obj){
var g,l,t,s,v,h,rv,
//size = 91.14378277661477,
//side = 50,
left_h = 46.48512749037782,
top_h = 39.8443949917523,
left_step_h = 69.585127490378,
top_step_h = 39.84439499,
left_v = 37.5992809922301,
top_v = 43.8658278242683,
left_step_v = 37.5992809922301,
top_step_v = 66.9658278242677;
if(!_.has(obj,'remove') && !_.isFunction(obj.remove) ){
return {page: Campaign().get('playerpageid'), col:0, row:0, grid: 'unknown', valid: false};
}
s = function(d){ return ~~((d/70) - 0.5) + 1; };
v = function(d,sd,sv){ return ( ~~d === ~~sd ) ? 1 : ~~((d-sd)/sv ) + 1; };
h = function(d,sd,sv){ return Math.round( (d-sd)/sv) + 1; };
g = getObj('page',obj.get('pageid')).get('grid_type');
l = obj.get('left');
t = obj.get('top');
rv = ('square' === g ) ? {page: obj.get('pageid'), col: s(l), row: s(t), grid: g, valid: true} :
('hex' === g ) ? {page: obj.get('pageid'), col: v(l,left_v,left_step_v), row: v(t,top_v,top_step_v), grid: g, valid: true} :
('hexr' === g ) ? {page: obj.get('pageid'), col: h(l,left_h,left_step_h), row: h(t,top_h,top_step_h), grid: g, valid: true} :
{page: obj.get('pageid'), col:0, row:0, grid: 'unknown', valid: false};
return rv;
},
map_adjacent = function(o){
var isat = map_coordinates(o),rv,cg=[],ma;
if(!isat.valid){
return {o_id: 'unknown', page: Campaign().get('playerpageid'), layer: 'unknown', col:0, row:0, grid: 'unknown', adjacent: {}, valid: false};
}
_.each(findObjs({_pageid: isat.page, _type: 'graphic'}), function(e) {
ma = map_coordinates(e);
if(ma.valid){
cg.push({id: e.get('id'), col: ma.col, row: ma.row, layer: e.get('layer')});
}
});
rv = {o_id: o.get('id'), page: o.get('pageid'), layer: o.get('layer'), col: isat.col, row: isat.row, grid: isat.grid, adjacent: {}, valid: true};
rv.adjacent = ('hex' === isat.grid) ? rv.adjacent = {
c0: _.where(cg, {col: isat.col, row: isat.row}),
c1: _.where(cg, {col: isat.col - 1, row: isat.row - 1}),
c2: _.where(cg, {col: isat.col + 1, row: isat.row - 1}),
c3: _.where(cg, {col: isat.col + 2, row: isat.row}),
c4: _.where(cg, {col: isat.col + 1, row: isat.row + 1}),
c5: _.where(cg, {col: isat.col - 1, row: isat.row + 1}),
c6: _.where(cg, {col: isat.col - 2, row: isat.row}),
c7: [],
c8: []
} : ('hexr' === isat.grid) ? rv.adjacent = {
c0: _.where(cg, {col: isat.col, row: isat.row}),
c1: _.where(cg, {col: isat.col - 1, row: isat.row - 1}),
c2: _.where(cg, {col: isat.col, row: isat.row - 2}),
c3: _.where(cg, {col: isat.col + 1, row: isat.row - 1}),
c4: _.where(cg, {col: isat.col + 1, row: isat.row + 1}),
c5: _.where(cg, {col: isat.col, row: isat.row + 2}),
c6: _.where(cg, {col: isat.col - 1, row: isat.row + 1}),
c7: [],
c8: []
} : ('square' === isat.grid) ? rv.adjacent = {
c0: _.where(cg, {col: isat.col, row: isat.row}),
c1: _.where(cg, {col: isat.col - 1, row: isat.row - 1}),
c2: _.where(cg, {col: isat.col, row: isat.row - 1}),
c3: _.where(cg, {col: isat.col + 1, row: isat.row - 1}),
c4: _.where(cg, {col: isat.col + 1, row: isat.row}),
c5: _.where(cg, {col: isat.col + 1, row: isat.row + 1}),
c6: _.where(cg, {col: isat.col, row: isat.row + 1}),
c7: _.where(cg, {col: isat.col - 1, row: isat.row + 1}),
c8: _.where(cg, {col: isat.col - 1, row: isat.row})
} : rv.adjacent = {};
return rv;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.