Index: binaries/data/mods/public/simulation/ai/common-api/map-module.js =================================================================== --- binaries/data/mods/public/simulation/ai/common-api/map-module.js +++ binaries/data/mods/public/simulation/ai/common-api/map-module.js @@ -52,6 +52,23 @@ return this.map[q[0] + this.width * q[1]]; }; +m.Map.prototype.runLoop = function(x0, x1, y0, y1, cx, cy, maxDist2, func) +{ + 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, func(w, r2)); + } + } +} + m.Map.prototype.addInfluence = function(cx, cy, maxDist, strength, type = "linear") { strength = strength ? strength : maxDist; @@ -66,52 +83,16 @@ if (type == "linear") { let str = strength / maxDist; - 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] + str * (maxDist - Math.sqrt(r2))); - } - } + this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => {this.map[w] + str * (maxDist - Math.sqrt(r2))}); } else if (type == "quadratic") { let str = strength / maxDist2; - 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] + str * (maxDist2 - r2)); - } - } + this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => {this.map[w] + str * (maxDist2 - r2)}); } else { - 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); - } - } + this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => {this.map[w] + strength}); } }; @@ -128,52 +109,16 @@ if (type == "linear") { let str = strength / maxDist; - 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, str * (maxDist - Math.sqrt(r2)) * this.map[w]); - } - } + this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => {str * (maxDist - Math.sqrt(r2)) * this.map[w]}); } else if (type == "quadratic") { let str = strength / maxDist2; - 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, str * (maxDist2 - r2) * this.map[w]); - } - } + this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => {str * (maxDist2 - r2) * this.map[w]}); } else { - 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); - } - } + this.runLoop(x0, x1, y0, y1, cx, cy, maxDist2, (w, r2) => {this.map[w] * strength}); } };