Differential D4235 Diff 18659 ps/trunk/binaries/data/mods/public/simulation/ai/common-api/map-module.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/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; | ||||
let r2 = dx*dx + dy*dy; | const r2 = dx * dx + dy2; | ||||
if (r2 >= maxDist2) | if (r2 >= maxDist2) | ||||
continue; | continue; | ||||
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)); | ||||
} | } | ||||
} | } | ||||
} | }; | ||||
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") | |||||
{ | { | ||||
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))); | ||||
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)); | ||||
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); | |||||
}; | }; | ||||
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]); | ||||
{ | |||||
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]); | ||||
{ | |||||
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); | ||||
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 94 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator