Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/common-api/map-module.js
Show First 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | ||||||||||
m.Map.prototype.point = function(p) | m.Map.prototype.point = function(p) | |||||||||
{ | { | |||||||||
let q = this.gamePosToMapPos(p); | let q = this.gamePosToMapPos(p); | |||||||||
q[0] = q[0] >= this.width ? this.width-1 : q[0] < 0 ? 0 : q[0]; | q[0] = q[0] >= this.width ? this.width-1 : q[0] < 0 ? 0 : q[0]; | |||||||||
q[1] = q[1] >= this.width ? this.width-1 : q[1] < 0 ? 0 : q[1]; | q[1] = q[1] >= this.width ? this.width-1 : q[1] < 0 ? 0 : q[1]; | |||||||||
return this.map[q[0] + this.width * q[1]]; | return this.map[q[0] + this.width * q[1]]; | |||||||||
}; | }; | |||||||||
m.Map.prototype.addInfluence = function(cx, cy, maxDist, strength, type = "linear") | m.Map.prototype.runLoop = function(x0, x1, y0, y1, cx, cy, maxDist2, func) | |||||||||
{ | ||||||||||
strength = strength ? strength : maxDist; | ||||||||||
let x0 = Math.floor(Math.max(0, cx - maxDist)); | ||||||||||
let y0 = Math.floor(Math.max(0, cy - maxDist)); | ||||||||||
let x1 = Math.floor(Math.min(this.width-1, cx + maxDist)); | ||||||||||
let y1 = Math.floor(Math.min(this.height-1, cy + maxDist)); | ||||||||||
let maxDist2 = maxDist * maxDist; | ||||||||||
// code duplicating for speed | ||||||||||
if (type == "linear") | ||||||||||
{ | { | |||||||||
let str = strength / maxDist; | ||||||||||
for (let y = y0; y < y1; ++y) | for (let y = y0; y < y1; ++y) | |||||||||
{ | { | |||||||||
let dy = y - cy; | const dy2 = (y - cy) * (y - cy); | |||||||||
const yw = y * this.width; | ||||||||||
for (let x = x0; x < x1; ++x) | for (let x = x0; x < x1; ++x) | |||||||||
{ | { | |||||||||
let dx = x - cx; | const dx = x - cx; | |||||||||
Stan: You're computing dy * dy one loop too much. | ||||||||||
let r2 = dx*dx + dy*dy; | const r2 = dx * dx + dy2; | |||||||||
if (r2 >= maxDist2) | if (r2 >= maxDist2) | |||||||||
continue; | continue; | |||||||||
Not Done Inline Actionssame for y *this.width. Stan: same for y *this.width. | ||||||||||
let w = x + y * this.width; | const w = x + yw; | |||||||||
this.set(w, this.map[w] + str * (maxDist - Math.sqrt(r2))); | this.set(w, func(w, r2)); | |||||||||
} | } | |||||||||
} | } | |||||||||
} | }; | |||||||||
Not Done Inline ActionsAdd that one while at it bb: Add that one while at it | ||||||||||
else if (type == "quadratic") | ||||||||||
{ | m.Map.prototype.addInfluence = function(cx, cy, maxDist, strength, type = "linear") | |||||||||
let str = strength / maxDist2; | ||||||||||
for (let y = y0; y < y1; ++y) | ||||||||||
{ | { | |||||||||
let dy = y - cy; | strength = strength ? strength : maxDist; | |||||||||
for (let x = x0; x < x1; ++x) | ||||||||||
const x0 = Math.floor(Math.max(0, cx - maxDist)); | ||||||||||
const y0 = Math.floor(Math.max(0, cy - maxDist)); | ||||||||||
const x1 = Math.floor(Math.min(this.width-1, cx + maxDist)); | ||||||||||
const y1 = Math.floor(Math.min(this.height-1, cy + maxDist)); | ||||||||||
const maxDist2 = maxDist * maxDist; | ||||||||||
if (type == "linear") | ||||||||||
Not Done Inline Actionswhile at it remove this comment should have been done in rP18352 bb: while at it remove this comment
should have been done in rP18352 | ||||||||||
Not Done Inline ActionsIf it is indeed so slow might need to be done in C++ Would be nice to know why duplicating was faster. Might be relevant https://stackoverflow.com/questions/80802/does-use-of-anonymous-functions-affect-performance#81329 In which case we might want to make those inline functions part of the prototype so they are not reinstantiated each time. Stan: If it is indeed so slow might need to be done in C++
Would be nice to know why duplicating was… | ||||||||||
{ | { | |||||||||
let dx = x - cx; | const str = strength / maxDist; | |||||||||
let r2 = dx*dx + dy*dy; | this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => this.map[w] + str * (maxDist - Math.sqrt(r2))); | |||||||||
Not Done Inline ActionsSeems like you got some parenthesis too much (the function won't do anything). Running patched vs non patched yields OOS's for me, likely related. bb: Seems like you got some parenthesis too much (the function won't do anything). Running patched… | ||||||||||
Not Done Inline Actions
Doesn't this work as well? (It is shorter code...) bb: Doesn't this work as well? (It is shorter code...) | ||||||||||
Not Done Inline Actions() => { return a; } () => a function () { return a; } // !== // Those functions return nothing. () => { a; } function () { a; } Stan: ```lang=js
() => { return a; }
() => a
function () { return a; }
// !==
// Those functions… | ||||||||||
if (r2 >= maxDist2) | ||||||||||
continue; | ||||||||||
let w = x + y * this.width; | ||||||||||
this.set(w, this.map[w] + str * (maxDist2 - r2)); | ||||||||||
} | ||||||||||
} | } | |||||||||
} | else if (type == "quadratic") | |||||||||
else | ||||||||||
{ | ||||||||||
for (let y = y0; y < y1; ++y) | ||||||||||
{ | ||||||||||
let dy = y - cy; | ||||||||||
for (let x = x0; x < x1; ++x) | ||||||||||
{ | { | |||||||||
let dx = x - cx; | const str = strength / maxDist2; | |||||||||
let r2 = dx*dx + dy*dy; | this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => this.map[w] + str * (maxDist2 - r2)); | |||||||||
Not Done Inline Actionssame bb: same | ||||||||||
if (r2 >= maxDist2) | ||||||||||
continue; | ||||||||||
let w = x + y * this.width; | ||||||||||
this.set(w, this.map[w] + strength); | ||||||||||
} | ||||||||||
} | ||||||||||
} | } | |||||||||
else | ||||||||||
this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => this.map[w] + strength); | ||||||||||
Not Done Inline Actionsalso bb: also | ||||||||||
}; | }; | |||||||||
m.Map.prototype.multiplyInfluence = function(cx, cy, maxDist, strength, type = "constant") | m.Map.prototype.multiplyInfluence = function(cx, cy, maxDist, strength, type = "constant") | |||||||||
{ | { | |||||||||
strength = strength ? +strength : +maxDist; | strength = strength ? +strength : +maxDist; | |||||||||
let x0 = Math.max(0, cx - maxDist); | const x0 = Math.max(0, cx - maxDist); | |||||||||
let y0 = Math.max(0, cy - maxDist); | const y0 = Math.max(0, cy - maxDist); | |||||||||
let x1 = Math.min(this.width, cx + maxDist); | const x1 = Math.min(this.width, cx + maxDist); | |||||||||
let y1 = Math.min(this.height, cy + maxDist); | const y1 = Math.min(this.height, cy + maxDist); | |||||||||
let maxDist2 = maxDist * maxDist; | const maxDist2 = maxDist * maxDist; | |||||||||
if (type == "linear") | if (type == "linear") | |||||||||
{ | { | |||||||||
let str = strength / maxDist; | const str = strength / maxDist; | |||||||||
for (let y = y0; y < y1; ++y) | this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => str * (maxDist - Math.sqrt(r2)) * this.map[w]); | |||||||||
Not Done Inline Actionstoo bb: too | ||||||||||
{ | ||||||||||
let dy = y - cy; | ||||||||||
for (let x = x0; x < x1; ++x) | ||||||||||
{ | ||||||||||
let dx = x - cx; | ||||||||||
let r2 = dx*dx + dy*dy; | ||||||||||
if (r2 >= maxDist2) | ||||||||||
continue; | ||||||||||
let w = x + y * this.width; | ||||||||||
this.set(w, str * (maxDist - Math.sqrt(r2)) * this.map[w]); | ||||||||||
} | ||||||||||
} | ||||||||||
} | } | |||||||||
else if (type == "quadratic") | else if (type == "quadratic") | |||||||||
{ | { | |||||||||
let str = strength / maxDist2; | const str = strength / maxDist2; | |||||||||
for (let y = y0; y < y1; ++y) | this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => str * (maxDist2 - r2) * this.map[w]); | |||||||||
Not Done Inline Actionsgot him? bb: got him? | ||||||||||
{ | ||||||||||
let dy = y - cy; | ||||||||||
for (let x = x0; x < x1; ++x) | ||||||||||
{ | ||||||||||
let dx = x - cx; | ||||||||||
let r2 = dx*dx + dy*dy; | ||||||||||
if (r2 >= maxDist2) | ||||||||||
continue; | ||||||||||
let w = x + y * this.width; | ||||||||||
this.set(w, str * (maxDist2 - r2) * this.map[w]); | ||||||||||
} | ||||||||||
} | ||||||||||
} | } | |||||||||
else | else | |||||||||
{ | this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => this.map[w] * strength); | |||||||||
Not Done Inline Actionsone more bb: one more | ||||||||||
for (let y = y0; y < y1; ++y) | ||||||||||
{ | ||||||||||
let dy = y - cy; | ||||||||||
for (let x = x0; x < x1; ++x) | ||||||||||
{ | ||||||||||
let dx = x - cx; | ||||||||||
let r2 = dx*dx + dy*dy; | ||||||||||
if (r2 >= maxDist2) | ||||||||||
continue; | ||||||||||
let w = x + y * this.width; | ||||||||||
this.set(w, this.map[w] * strength); | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
}; | }; | |||||||||
/** add to current map by the parameter map pixelwise */ | /** add to current map by the parameter map pixelwise */ | |||||||||
m.Map.prototype.add = function(map) | m.Map.prototype.add = function(map) | |||||||||
{ | { | |||||||||
for (let i = 0; i < this.length; ++i) | for (let i = 0; i < this.length; ++i) | |||||||||
this.set(i, this.map[i] + map.map[i]); | this.set(i, this.map[i] + map.map[i]); | |||||||||
}; | }; | |||||||||
▲ Show 20 Lines • Show All 85 Lines • ▼ Show 20 Lines | ||||||||||
m.Map.prototype.dumpIm = function(name = "default.png", threshold = this.maxVal) | m.Map.prototype.dumpIm = function(name = "default.png", threshold = this.maxVal) | |||||||||
{ | { | |||||||||
Engine.DumpImage(name, this.map, this.width, this.height, threshold); | Engine.DumpImage(name, this.map, this.width, this.height, threshold); | |||||||||
}; | }; | |||||||||
return m; | return m; | |||||||||
}(API3); | }(API3); | |||||||||
Lint: no-use-before-define 'API3' was used before it was defined. Lint: no-use-before-define: 'API3' was used before it was defined. |
Wildfire Games · Phabricator
You're computing dy * dy one loop too much.