From df14c914e654737953c48ef675104bfb231eeee2 Mon Sep 17 00:00:00 2001 From: elexis Date: Mon, 6 Nov 2017 12:17:56 +0000 Subject: [PATCH] Move starting player entity rmgen functions from misc.js to a new player.js, refs #4804. Remove two duplicates of the starting entity placement code in the Fortress map of 208e642089 and placeCivDefaultEntities of 9c3e94acfd by just making the distance an argument, refs #4805. Equally to the starting cavalry, don't spawn special units like dogs or worker elephants on Fortress twice, but only the women and Infantry. Consider the weird kwargs iberWalls argument of placeCivDefaultEntities from 9c3e94acfd deprecated and just make the orientation a regular optional argument. Rename createStartingPlayerEntities to placeStartingEntities. This was SVN commit r20415. --- .../data/mods/public/maps/random/fortress.js | 22 ++---- .../mods/public/maps/random/rmgen/misc.js | 77 ------------------- .../mods/public/maps/random/rmgen/player.js | 49 ++++++++++++ .../data/mods/public/maps/random/wild_lake.js | 2 +- 4 files changed, 58 insertions(+), 92 deletions(-) create mode 100644 binaries/data/mods/public/maps/random/rmgen/player.js diff --git a/binaries/data/mods/public/maps/random/fortress.js b/binaries/data/mods/public/maps/random/fortress.js index 2030be04f5..6118b3a9bc 100644 --- a/binaries/data/mods/public/maps/random/fortress.js +++ b/binaries/data/mods/public/maps/random/fortress.js @@ -59,24 +59,18 @@ var [playerIDs, playerX, playerZ] = radialPlayerPlacement(); for (var i=0; i < numPlayers; i++) { + log("Creating base for player " + playerIDs[i] + "..."); playerX[i] *= mapSize; playerZ[i] *= mapSize; - var startEntities = getStartingEntities(i); - // Place starting entities - createStartingPlayerEntities(playerX[i], playerZ[i], i+1, startEntities); - var uDist = 8; - var uSpace = 2; - for (var j = 1; j < startEntities.length - 1; ++j) + for (let dist of [6, 8]) { - var uAngle = BUILDING_ORIENTATION - PI * (2-j) / 2; - var count = (startEntities[j].Count !== undefined ? startEntities[j].Count : 1); - for (var numberofentities = 0; numberofentities < count; numberofentities++) - { - var ux = playerX[i] + uDist * cos(uAngle) + numberofentities * uSpace * cos(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * cos(uAngle + PI/2)); - var uz = playerZ[i] + uDist * sin(uAngle) + numberofentities * uSpace * sin(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * sin(uAngle + PI/2)); - placeObject(ux, uz, startEntities[j].Template, i+1, uAngle); - } + let ents = getStartingEntities(i); + + if (dist == 8) + ents = ents.filter(ent => ent.Template.indexOf("female") != -1 || ent.Template.indexOf("infantry") != -1); + + placeStartingEntities(playerX[i], playerZ[i], i + 1, ents, dist); } // Create treasure diff --git a/binaries/data/mods/public/maps/random/rmgen/misc.js b/binaries/data/mods/public/maps/random/rmgen/misc.js index d2eac12eca..ec3134f949 100644 --- a/binaries/data/mods/public/maps/random/rmgen/misc.js +++ b/binaries/data/mods/public/maps/random/rmgen/misc.js @@ -1,80 +1,3 @@ -///////////////////////////////////////////////////////////////////////////////////////// -// createStartingPlayerEntities -// -// Creates the starting player entities -// fx&fz: position of player base -// playerid: id of player -// civEntities: use getStartingEntities(id-1) fo this one -// orientation: orientation of the main base building, default is BUILDING_ORIENTATION -// -/////////////////////////////////////////////////////////////////////////////////////////// -function createStartingPlayerEntities(fx, fz, playerid, civEntities, orientation = BUILDING_ORIENTATION) -{ - var uDist = 6; - var uSpace = 2; - placeObject(fx, fz, civEntities[0].Template, playerid, orientation); - for (var j = 1; j < civEntities.length; ++j) - { - var uAngle = orientation - PI * (2-j) / 2; - var count = (civEntities[j].Count !== undefined ? civEntities[j].Count : 1); - for (var numberofentities = 0; numberofentities < count; numberofentities++) - { - var ux = fx + uDist * cos(uAngle) + numberofentities * uSpace * cos(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * cos(uAngle + PI/2)); - var uz = fz + uDist * sin(uAngle) + numberofentities * uSpace * sin(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * sin(uAngle + PI/2)); - placeObject(ux, uz, civEntities[j].Template, playerid, uAngle); - } - } -} - -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// placeCivDefaultEntities -// -// Creates the default starting player entities depending on the players civ -// fx&fy: position of player base -// playerid: id of player -// kwargs: Takes some optional keyword arguments to tweek things -// 'iberWall': may be false, 'walls' (default) or 'towers'. Determines the defensive structures Iberians get as civ bonus -// 'orientation': angle of the main base building, default is BUILDING_ORIENTATION -// -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -function placeCivDefaultEntities(fx, fz, playerid, kwargs = {}) -{ - // Unpack kwargs - var iberWall = 'walls'; - if (getMapSize() <= 128) - iberWall = false; - if ('iberWall' in kwargs) - iberWall = kwargs['iberWall']; - var orientation = BUILDING_ORIENTATION; - if ('orientation' in kwargs) - orientation = kwargs['orientation']; - // Place default civ starting entities - var civ = getCivCode(playerid-1); - var civEntities = getStartingEntities(playerid-1); - var uDist = 6; - var uSpace = 2; - placeObject(fx, fz, civEntities[0].Template, playerid, orientation); - for (var j = 1; j < civEntities.length; ++j) - { - var uAngle = orientation - PI * (2-j) / 2; - var count = (civEntities[j].Count !== undefined ? civEntities[j].Count : 1); - for (var numberofentities = 0; numberofentities < count; numberofentities++) - { - var ux = fx + uDist * cos(uAngle) + numberofentities * uSpace * cos(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * cos(uAngle + PI/2)); - var uz = fz + uDist * sin(uAngle) + numberofentities * uSpace * sin(uAngle + PI/2) - (0.75 * uSpace * floor(count / 2) * sin(uAngle + PI/2)); - placeObject(ux, uz, civEntities[j].Template, playerid, uAngle); - } - } - // Add defensive structiures for Iberians as their civ bonus - if (civ == 'iber' && iberWall != false) - { - if (iberWall == 'towers') - placePolygonalWall(fx, fz, 15, ['entry'], 'tower', civ, playerid, orientation, 7); - else - placeGenericFortress(fx, fz, 20/*radius*/, playerid); - } -} - function placeDefaultChicken(playerX, playerZ, tileClass, constraint = undefined, template = "gaia/fauna_chicken") { for (let j = 0; j < 2; ++j) diff --git a/binaries/data/mods/public/maps/random/rmgen/player.js b/binaries/data/mods/public/maps/random/rmgen/player.js new file mode 100644 index 0000000000..875ce268aa --- /dev/null +++ b/binaries/data/mods/public/maps/random/rmgen/player.js @@ -0,0 +1,49 @@ +/** + * Places the given entities at the given location (typically a civic center and starting units). + * @param civEntities - An array of objects with the Template property and optionally a Count property. + * The first entity is placed in the center, the other ones surround it. + */ +function placeStartingEntities(fx, fz, playerID, civEntities, dist = 6, orientation = BUILDING_ORIENTATION) +{ + // Place the central structure + let i = 0; + let firstTemplate = civEntities[i].Template; + if (firstTemplate.startsWith("structures/")) + { + placeObject(fx, fz, firstTemplate, playerID, orientation); + ++i; + } + + // Place entities surrounding it + let space = 2; + for (let j = i; j < civEntities.length; ++j) + { + let angle = orientation - Math.PI * (1 - j / 2); + let count = civEntities[j].Count || 1; + + for (let num = 0; num < count; ++num) + placeObject( + fx + dist * Math.cos(angle) + space * (-num + 0.75 * Math.floor(count / 2)) * Math.sin(angle), + fz + dist * Math.sin(angle) + space * (num - 0.75 * Math.floor(count / 2)) * Math.cos(angle), + civEntities[j].Template, + playerID, + angle); + } +} + +/** + * Places the default starting entities as defined by the civilization definition and walls for Iberians. + */ +function placeCivDefaultEntities(fx, fz, playerID, kwargs, dist = 6, orientation = BUILDING_ORIENTATION) +{ + placeStartingEntities(fx, fz, playerID, getStartingEntities(playerID - 1), dist, orientation); + + let civ = getCivCode(playerID - 1); + if (civ == 'iber' && getMapSize() > 128) + { + if (kwargs && kwargs.iberWall == 'towers') + placePolygonalWall(fx, fz, 15, ['entry'], 'tower', civ, playerID, orientation, 7); + else if (!kwargs || kwargs.iberWall) + placeGenericFortress(fx, fz, 20, playerID); + } +} diff --git a/binaries/data/mods/public/maps/random/wild_lake.js b/binaries/data/mods/public/maps/random/wild_lake.js index a1b5ec9f3a..5b5e5caf6f 100644 --- a/binaries/data/mods/public/maps/random/wild_lake.js +++ b/binaries/data/mods/public/maps/random/wild_lake.js @@ -680,7 +680,7 @@ for (let i = 0; i < resourceSpots.length; ++i) { if (mercenaryCamps) { - createStartingPlayerEntities(resourceSpots[i].x, resourceSpots[i].y, 0, mercenaryCampGuards[currentBiome()]); + placeStartingEntities(resourceSpots[i].x, resourceSpots[i].y, 0, mercenaryCampGuards[currentBiome()]); rectangularSmoothToHeight(resourceSpots[i], 15, 15, g_Map.height[resourceSpots[i].x][resourceSpots[i].y], 0.5); --mercenaryCamps; }