Skip to content

Commit

Permalink
Add the ability to regenerate cultures (#495)
Browse files Browse the repository at this point in the history
* Add the ability to regenerate cultures

- Added a button to the tools menu for regeneration.
- Regeneration button will handle initial generation of cultures and expansion afterwards.
- Pressing regenerate will warn the user.
- Small cleanup of trailing whitespace.

* Refreshing cultures editor updates culture centers

* Regenerating cultures refreshes the culture editor

* Added a function to refresh all open editors

* Reset burg and state cultures after regeneration

* Address the problem of potential data loss

Any errors while iterating the states or burgs could potentially lose the index 0 metadata stored in the arrays. This will instead track the index and ignore the 0th result.

* Religions update cultures on culture regeneration

Updated function names to be more similar and more descriptive
  • Loading branch information
mdirienzo authored and Azgaar committed Feb 4, 2021
1 parent cf0a446 commit fb28576
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,7 @@
<button id="regenerateStates" data-tip="Click to select new capitals and regenerate states. Military forces will be regenerated as well, burgs will remain as they are">States</button>
<button id="regenerateProvinces" data-tip="Click to regenerate provinces. States will remain as they are">Provinces</button>
<button id="regenerateReligions" data-tip="Click to regenerate religions">Religions</button>
<button id="regenerateCultures" data-tip="Click to regenerate cultures">Cultures</button>
<button id="regenerateMilitary" data-tip="Click to recalculate military forces based on current military options">Military</button>
<button id="regenerateIce" data-tip="Click to icebergs and glaciers">Ice</button>
<button id="regenerateMarkers" data-tip="Click to regenerate markers. Hold Ctrl and click to set markers number multiplier">Markers</button>
Expand Down Expand Up @@ -3456,7 +3457,7 @@
<div data-tip="Military personnel rate (% of state population). Depends on war alert. Click to sort" style="width: 3.7em" class="sortable" data-sortby="rate">Rate&nbsp;</div>
<div data-tip="War Alert. Modifier to military forces number, depends of political situation. Click to sort" class="sortable" data-sortby="alert">War Alert&nbsp;</div>
</div>

<div id="militaryBody" data-type="absolute"></div>
</div>

Expand Down Expand Up @@ -3486,7 +3487,7 @@
<div data-tip="Regiment emblem and name. Click to sort by name" style="width: 12em" class="sortable alphabetically" data-sortby="name">Name&nbsp;</div>
<div data-tip="Total military personnel (not considering crew). Click to sort" style="margin-left: .8em" id="regimentsTotal" class="sortable icon-sort-number-down" data-sortby="total">Total&nbsp;</div>
</div>

<div id="regimentsBody" data-type="absolute"></div>
</div>

Expand Down
28 changes: 27 additions & 1 deletion modules/burgs-and-states.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,32 @@
console.timeEnd("normalizeStates");
}

// Resets the cultures of all burgs and states to their
// cell or center cell's (respectively) culture.
const updateCultures = function () {
console.time('updateCulturesForBurgsAndStates');

// Assign the culture associated with the burgs cell.
pack.burgs = pack.burgs.map( (burg, index) => {
// Ignore metadata burg
if(index === 0) {
return burg;
}
return {...burg, culture: pack.cells.culture[burg.cell]};
});

// Assign the culture associated with the states' center cell.
pack.states = pack.states.map( (state, index) => {
// Ignore neutrals state
if(index === 0) {
return state;
}
return {...state, culture: pack.cells.culture[state.center]};
});

console.timeEnd('updateCulturesForBurgsAndStates');
}

// calculate and draw curved state labels for a list of states
const drawStateLabels = function(list) {
console.time("drawStateLabels");
Expand Down Expand Up @@ -1029,6 +1055,6 @@

return {generate, expandStates, normalizeStates, assignColors,
drawBurgs, specifyBurgs, defineBurgFeatures, drawStateLabels, collectStatistics,
generateCampaigns, generateDiplomacy, defineStateForms, getFullName, generateProvinces};
generateCampaigns, generateDiplomacy, defineStateForms, getFullName, generateProvinces, updateCultures};

})));
13 changes: 12 additions & 1 deletion modules/religions-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@
});
}

function updateCultures() {
console.time('updateCulturesForReligions');
pack.religions = pack.religions.map( (religion, index) => {
if(index === 0) {
return religion;
}
return {...religion, culture: pack.cells.culture[religion.center]};
});
console.timeEnd('updateCulturesForReligions');
}

// assign a unique two-letters code (abbreviation)
function getCode(rawName) {
const name = rawName.replace("Old ", ""); // remove Old prefix
Expand Down Expand Up @@ -350,6 +361,6 @@
return type() + " of the " + generateMeaning();
};

return {generate, add, getDeityName, expandReligions};
return {generate, add, getDeityName, expandReligions, updateCultures};

})));
1 change: 1 addition & 0 deletions modules/ui/cultures-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function editCultures() {
function refreshCulturesEditor() {
culturesCollectStatistics();
culturesEditorAddLines();
drawCultureCenters();
}

function culturesCollectStatistics() {
Expand Down
13 changes: 13 additions & 0 deletions modules/ui/editors.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,4 +630,17 @@ function selectIcon(initial, callback) {
Apply: function() {callback(input.value||"⠀"); $(this).dialog("close")},
Close: function() {callback(initial); $(this).dialog("close")}}
});
}

// Calls the refresh functionality on all editors currently open.
function refreshAllEditors() {
console.time('refreshAllEditors');
if (document.getElementById('culturesEditorRefresh').offsetParent) culturesEditorRefresh.click();
if (document.getElementById('biomesEditorRefresh').offsetParent) biomesEditorRefresh.click();
if (document.getElementById('diplomacyEditorRefresh').offsetParent) diplomacyEditorRefresh.click();
if (document.getElementById('provincesEditorRefresh').offsetParent) provincesEditorRefresh.click();
if (document.getElementById('religionsEditorRefresh').offsetParent) religionsEditorRefresh.click();
if (document.getElementById('statesEditorRefresh').offsetParent) statesEditorRefresh.click();
if (document.getElementById('zonesEditorRefresh').offsetParent) zonesEditorRefresh.click();
console.timeEnd('refreshAllEditors');
}
16 changes: 13 additions & 3 deletions modules/ui/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ toolsContent.addEventListener("click", function(event) {
});

function processFeatureRegeneration(event, button) {
if (button === "regenerateStateLabels") {BurgsAndStates.drawStateLabels(); if (!layerIsOn("toggleLabels")) toggleLabels();} else
if (button === "regenerateReliefIcons") {ReliefIcons(); if (!layerIsOn("toggleRelief")) toggleRelief();} else
if (button === "regenerateRoutes") {Routes.regenerate(); if (!layerIsOn("toggleRoutes")) toggleRoutes();} else
if (button === "regenerateStateLabels") {BurgsAndStates.drawStateLabels(); if (!layerIsOn("toggleLabels")) toggleLabels();} else
if (button === "regenerateReliefIcons") {ReliefIcons(); if (!layerIsOn("toggleRelief")) toggleRelief();} else
if (button === "regenerateRoutes") {Routes.regenerate(); if (!layerIsOn("toggleRoutes")) toggleRoutes();} else
if (button === "regenerateRivers") regenerateRivers(); else
if (button === "regeneratePopulation") recalculatePopulation(); else
if (button === "regenerateBurgs") regenerateBurgs(); else
if (button === "regenerateStates") regenerateStates(); else
if (button === "regenerateProvinces") regenerateProvinces(); else
if (button === "regenerateReligions") regenerateReligions(); else
if (button === "regenerateCultures") regenerateCultures(); else
if (button === "regenerateMilitary") regenerateMilitary(); else
if (button === "regenerateIce") regenerateIce(); else
if (button === "regenerateMarkers") regenerateMarkers(event); else
Expand Down Expand Up @@ -244,6 +245,15 @@ function regenerateReligions() {
if (!layerIsOn("toggleReligions")) toggleReligions(); else drawReligions();
}

function regenerateCultures() {
Cultures.generate();
Cultures.expand();
BurgsAndStates.updateCultures();
Religions.updateCultures();
if (!layerIsOn("toggleCultures")) toggleCultures(); else drawCultures();
refreshAllEditors();
}

function regenerateMilitary() {
Military.generate();
if (!layerIsOn("toggleMilitary")) toggleMilitary();
Expand Down

0 comments on commit fb28576

Please sign in to comment.