Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/maps/random/rmgen/Constraint.js
Context not available. | |||||
* @file A Constraint decides if a tile satisfies a condition defined by the class. | * @file A Constraint decides if a tile satisfies a condition defined by the class. | ||||
*/ | */ | ||||
const avoid = "avoid"; | |||||
const stay = "stay"; | |||||
const near = "near"; | |||||
const border = "border"; | |||||
/** | /** | ||||
elexis: Not global constants | |||||
Done Inline ActionsWhy not? I did this purely to avoid having to type quotation marks millions of times, because it's a serious pain as I found out firsthand. I don't see why they shouldn't just be reserved names. aeonios: Why not? I did this purely to avoid having to type quotation marks millions of times, because… | |||||
* This function returns constraints based on the specifications given by the arguments. | |||||
* | |||||
* Arguments: | |||||
* @param "avoid": {[tileclass1, mindist1, tileclass2, mindist2..etc]} avoid these tileclasses, by no less than the minimum defined distance in all directions. | |||||
* @param "stay": {[tileclass1, mindist1, tileclass2, mindist2..etc]} stay within the borders of these tileclasses, by at least the minimum defined distance in all directions. | |||||
* @param "near": {[tileclass1, maxdist1, tileclass2, maxdist2..etc]} stay near these tileclasses, by no farther than the maximum defined distance. | |||||
* @param "border": {[tileclass1, innerdist1, outerdist1, tileclass2, innerdist2, outerdist2..etc]} stay near the perimeter of these tileclasses, within the innerdist inside and the outerdist outside. | |||||
*/ | |||||
function getConstraints(args) | |||||
{ | |||||
let constraints = []; | |||||
let current; | |||||
current = args.avoid; | |||||
if (current) | |||||
for (let i = 0; i < current.length/2; ++i) | |||||
constraints.push(new AvoidTileClassConstraint(current[2*i], current[2*i+1])); | |||||
current = args.stay; | |||||
if (current) | |||||
for (let i = 0; i < current.length/2; ++i) | |||||
constraints.push(new StayInTileClassConstraint(current[2*i], current[2*i+1])); | |||||
current = args.near; | |||||
if (current) | |||||
for (let i = 0; i < current.length/2; ++i) | |||||
constraints.push(new NearTileClassConstraint(current[2*i], current[2*i+1])); | |||||
current = args.border; | |||||
if (current) | |||||
for (let i = 0; i < current.length/3; ++i) | |||||
constraints.push(new BorderTileClassConstraint(current[3*i], current[3*i+1], current[3*i+2])); | |||||
if (constraints.length == 1) | |||||
return constraints[0]; | |||||
else if (constraints.length == 0) | |||||
return new NullConstraint(); | |||||
return new AndConstraint(constraints); | |||||
} | |||||
/** | |||||
* Creates an area from a set of constraints. | |||||
*/ | |||||
function getAreaFromConstraints(args) | |||||
{ | |||||
return new Area(new MapBoundsPlacer().place(getConstraints(args))); | |||||
} | |||||
/** | |||||
* The NullConstraint is always satisfied. | * The NullConstraint is always satisfied. | ||||
*/ | */ | ||||
function NullConstraint() {} | function NullConstraint() {} | ||||
Not Done Inline ActionsI personally would not recommend proxy functions that are that short and I removed many of them (library.js). elexis: I personally would not recommend proxy functions that are that short and I removed many of them… | |||||
Done Inline ActionsIt saves on parenthesis. A lot. It's also a pain to sort out the various arguments when you've got all that other mess going on, and this is still way easier to use and learn than that. aeonios: It saves on parenthesis. A lot. It's also a pain to sort out the various arguments when you've… | |||||
Context not available. | |||||
function AndConstraint(constraints) | function AndConstraint(constraints) | ||||
{ | { | ||||
if (constraints instanceof Array) | if (constraints instanceof Array) | ||||
this.constraints = constraints | this.constraints = constraints; | ||||
else if (!constraints) | else if (!constraints) | ||||
this.constraints = []; | this.constraints = []; | ||||
else | else | ||||
Context not available. | |||||
AvoidAreasConstraint.prototype.allows = function(position) | AvoidAreasConstraint.prototype.allows = function(position) | ||||
{ | { | ||||
return this.areas.every(area => !area.contains(position)) | return this.areas.every(area => !area.contains(position)); | ||||
}; | }; | ||||
/** | /** | ||||
Context not available. |
Wildfire Games · Phabricator
Not global constants