Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/maps/random/rmgen/library.js
Show First 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | |||||
function min(a, b) | function min(a, b) | ||||
{ | { | ||||
return a < b ? a : b; | return a < b ? a : b; | ||||
} | } | ||||
/** | /** | ||||
* Retries the given function with those arguments as often as specified. | * Retries the given function with those arguments as often as specified. | ||||
*/ | */ | ||||
function retryPlacing(placeFunc, placeArgs, retryFactor, amount, getResult) | function retryPlacing(placeFunc, placeArgs, retryFactor, amount, getResult, behaveWrong = false) | ||||
{ | { | ||||
let maxFail = amount * retryFactor; | let maxFail = amount * retryFactor; | ||||
let results = []; | let results = []; | ||||
let good = 0; | let good = 0; | ||||
let bad = 0; | let bad = 0; | ||||
while (good < amount && bad <= maxFail) | while (good < amount && bad <= maxFail) | ||||
{ | { | ||||
let result = placeFunc(placeArgs); | let result = placeFunc(placeArgs); | ||||
if (result !== undefined) | if (result !== undefined || behaveWrong && (placeArgs.placer instanceof SimpleGroup || placeArgs.placer instanceof RandomGroup)) | ||||
elexis: So actually we only have to call the `*Deprecated` functions if the place is one of those 2… | |||||
{ | { | ||||
++good; | ++good; | ||||
if (getResult) | if (getResult) | ||||
results.push(result); | results.push(result); | ||||
} | } | ||||
else | else | ||||
++bad; | ++bad; | ||||
} | } | ||||
Show All 27 Lines | |||||
*/ | */ | ||||
function randomizePlacerCoordinatesFromAreas(placer, areas) | function randomizePlacerCoordinatesFromAreas(placer, areas) | ||||
{ | { | ||||
let pt = pickRandom(pickRandom(areas).points); | let pt = pickRandom(pickRandom(areas).points); | ||||
placer.x = pt.x; | placer.x = pt.x; | ||||
placer.z = pt.z; | placer.z = pt.z; | ||||
} | } | ||||
// TODO this is a hack to simulate the old behaviour of those functions | |||||
// until all old maps are changed to use the correct version of these functions | |||||
function createAreasDeprecated(centeredPlacer, painter, constraint, amount, retryFactor = 10) | |||||
{ | |||||
return createAreas(centeredPlacer, painter, constraint, amount, retryFactor, true); | |||||
} | |||||
function createAreasInAreasDeprecated(centeredPlacer, painter, constraint, amount, retryFactor, areas) | |||||
{ | |||||
return createAreasInAreas(centeredPlacer, painter, constraint, amount, retryFactor, areas, true); | |||||
} | |||||
function createObjectGroupsDeprecated(placer, player, constraint, amount, retryFactor = 10) | |||||
{ | |||||
return createObjectGroups(placer, player, constraint, amount, retryFactor, true); | |||||
} | |||||
function createObjectGroupsByAreasDeprecated(placer, player, constraint, amount, retryFactor, areas) | |||||
{ | |||||
return createObjectGroupsByAreas(placer, player, constraint, amount, retryFactor, areas, true); | |||||
} | |||||
/** | /** | ||||
* Attempts to place the given number of areas in random places of the map. | * Attempts to place the given number of areas in random places of the map. | ||||
* Returns actually placed areas. | * Returns actually placed areas. | ||||
*/ | */ | ||||
function createAreas(centeredPlacer, painter, constraint, amount, retryFactor = 10) | function createAreas(centeredPlacer, painter, constraint, amount, retryFactor, behaveDeprecated = false) | ||||
{ | { | ||||
if (retryFactor === undefined) | |||||
retryFactor = 10; | |||||
let placeFunc = function (args) { | let placeFunc = function (args) { | ||||
randomizePlacerCoordinates(args.placer, args.halfMapSize); | randomizePlacerCoordinates(args.placer, args.halfMapSize); | ||||
return g_Map.createArea(args.placer, args.painter, args.constraint); | return g_Map.createArea(args.placer, args.painter, args.constraint); | ||||
}; | }; | ||||
let args = { | let args = { | ||||
"placer": centeredPlacer, | "placer": centeredPlacer, | ||||
"painter": painter, | "painter": painter, | ||||
"constraint": constraint, | "constraint": constraint, | ||||
"halfMapSize": g_Map.size / 2 | "halfMapSize": g_Map.size / 2 | ||||
}; | }; | ||||
return retryPlacing(placeFunc, args, retryFactor, amount, true); | return retryPlacing(placeFunc, args, retryFactor, amount, true, behaveDeprecated); | ||||
} | } | ||||
/** | /** | ||||
* Attempts to place the given number of areas in random places of the given areas. | * Attempts to place the given number of areas in random places of the given areas. | ||||
* Returns actually placed areas. | * Returns actually placed areas. | ||||
*/ | */ | ||||
function createAreasInAreas(centeredPlacer, painter, constraint, amount, retryFactor, areas) | function createAreasInAreas(centeredPlacer, painter, constraint, amount, retryFactor, areas, behaveDeprecated = false) | ||||
{ | { | ||||
if (!areas.length) | if (!areas.length) | ||||
return []; | return []; | ||||
let placeFunc = function (args) { | let placeFunc = function (args) { | ||||
randomizePlacerCoordinatesFromAreas(args.placer, args.areas); | randomizePlacerCoordinatesFromAreas(args.placer, args.areas); | ||||
return g_Map.createArea(args.placer, args.painter, args.constraint); | return g_Map.createArea(args.placer, args.painter, args.constraint); | ||||
}; | }; | ||||
let args = { | let args = { | ||||
"placer": centeredPlacer, | "placer": centeredPlacer, | ||||
"painter": painter, | "painter": painter, | ||||
"constraint": constraint, | "constraint": constraint, | ||||
"areas": areas, | "areas": areas, | ||||
"halfMapSize": g_Map.size / 2 | "halfMapSize": g_Map.size / 2 | ||||
}; | }; | ||||
return retryPlacing(placeFunc, args, retryFactor, amount, true); | return retryPlacing(placeFunc, args, retryFactor, amount, true, behaveDeprecated); | ||||
} | } | ||||
/** | /** | ||||
* Attempts to place the given number of groups in random places of the map. | * Attempts to place the given number of groups in random places of the map. | ||||
* Returns the number of actually placed groups. | * Returns the number of actually placed groups. | ||||
*/ | */ | ||||
function createObjectGroups(placer, player, constraint, amount, retryFactor = 10) | function createObjectGroups(placer, player, constraint, amount, retryFactor, behaveDeprecated = false) | ||||
{ | { | ||||
if (retryFactor === undefined) | |||||
retryFactor = 10; | |||||
let placeFunc = function (args) { | let placeFunc = function (args) { | ||||
randomizePlacerCoordinates(args.placer, args.halfMapSize); | randomizePlacerCoordinates(args.placer, args.halfMapSize); | ||||
return createObjectGroup(args.placer, args.player, args.constraint); | return createObjectGroup(args.placer, args.player, args.constraint); | ||||
}; | }; | ||||
let args = { | let args = { | ||||
"placer": placer, | "placer": placer, | ||||
"player": player, | "player": player, | ||||
"constraint": constraint, | "constraint": constraint, | ||||
"halfMapSize": g_Map.size / 2 - 3 | "halfMapSize": g_Map.size / 2 - 3 | ||||
}; | }; | ||||
return retryPlacing(placeFunc, args, retryFactor, amount, false); | return retryPlacing(placeFunc, args, retryFactor, amount, false, behaveDeprecated); | ||||
} | } | ||||
/** | /** | ||||
* Attempts to place the given number of groups in random places of the given areas. | * Attempts to place the given number of groups in random places of the given areas. | ||||
* Returns the number of actually placed groups. | * Returns the number of actually placed groups. | ||||
*/ | */ | ||||
function createObjectGroupsByAreas(placer, player, constraint, amount, retryFactor, areas) | function createObjectGroupsByAreas(placer, player, constraint, amount, retryFactor, areas, behaveDeprecated = false) | ||||
{ | { | ||||
if (!areas.length) | if (!areas.length) | ||||
return 0; | return 0; | ||||
let placeFunc = function (args) { | let placeFunc = function (args) { | ||||
randomizePlacerCoordinatesFromAreas(args.placer, args.areas); | randomizePlacerCoordinatesFromAreas(args.placer, args.areas); | ||||
return createObjectGroup(args.placer, args.player, args.constraint); | return createObjectGroup(args.placer, args.player, args.constraint); | ||||
}; | }; | ||||
let args = { | let args = { | ||||
"placer": placer, | "placer": placer, | ||||
"player": player, | "player": player, | ||||
"constraint": constraint, | "constraint": constraint, | ||||
"areas": areas | "areas": areas | ||||
}; | }; | ||||
return retryPlacing(placeFunc, args, retryFactor, amount, false); | return retryPlacing(placeFunc, args, retryFactor, amount, false, behaveDeprecated); | ||||
} | } | ||||
function createTerrain(terrain) | function createTerrain(terrain) | ||||
{ | { | ||||
if (!(terrain instanceof Array)) | if (!(terrain instanceof Array)) | ||||
return createSimpleTerrain(terrain); | return createSimpleTerrain(terrain); | ||||
return new RandomTerrain(terrain.map(t => createTerrain(t))); | return new RandomTerrain(terrain.map(t => createTerrain(t))); | ||||
▲ Show 20 Lines • Show All 290 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
So actually we only have to call the *Deprecated functions if the place is one of those 2 types (and potentially display a warning if deprecated is called from another type). (That will make the diff shorter, not easier to review, but better reviewing this once than changing every occurance and then still having to review the revert)