Skip to content

Commit

Permalink
Rudimentary difficulty setting
Browse files Browse the repository at this point in the history
  • Loading branch information
wraitii committed Jun 16, 2021
1 parent a7eba03 commit 0730b46
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 26 additions & 6 deletions gui/campaigns/grand_strategy/game/GameData.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class GameData
this.provinces = {};
this.tribes = {};

this.difficulty = "medium";

this.playerTribe = undefined;
this.playerHero = undefined;

Expand Down Expand Up @@ -45,6 +47,7 @@ class GameData
"turn": this.turn,
"playerTribe": this.playerTribe,
"playerHero": this.playerHero.Serialize(),
"difficulty": this.difficulty,
"tribes": tribes,
"provinces": pv,
"events": pastEvents
Expand All @@ -71,6 +74,9 @@ class GameData
this.playerHero = new Hero();
this.playerHero.Deserialize(data.playerHero);

if (data.difficulty)
this.difficulty = data.difficulty;

this.pastTurnEvents = [];
for (const evs of data.events)
{
Expand All @@ -87,10 +93,10 @@ class GameData
this.turnEvents = this.pastTurnEvents[this.pastTurnEvents.length - 1];
}

static createNewGame(playerData)
static createNewGame(playerData, difficulty)
{
g_GameData = new GameData();
g_GameData.initialiseGame(playerData);
g_GameData.initialiseGame(playerData, difficulty);
return g_GameData;
}

Expand Down Expand Up @@ -118,7 +124,7 @@ class GameData
CampaignRun.getCurrentRun().save();
}

initialiseGame(playerData)
initialiseGame(playerData, difficulty)
{
this.parseHistory();

Expand All @@ -144,6 +150,8 @@ class GameData
this.provinces[prov].setOwner(code);
}

this.difficulty = difficulty;

this.save();
}

Expand Down Expand Up @@ -228,7 +236,7 @@ class GameData
let aiID = 1 - playerID;
gameSettings.playerAI.set(aiID, {
"bot": "petra",
"difficulty": playerIsAttacker ? province.garrison / 2 : 5 - province.garrison / 2,
"difficulty": this.getAIDifficulty(province.garrison, playerIsAttacker),
"behavior": "random",
});
gameSettings.playerCiv.setValue(0, this.tribes[attackerTribe].civ);
Expand All @@ -253,6 +261,13 @@ class GameData
});
}

getAIDifficulty(garrison, playerIsAttacker)
{
const max = this.difficulty === "easy" ? 2 : this.difficulty == "medium" ? 4 : 5;
const min = this.difficulty === "easy" ? 0 : this.difficulty == "medium" ? 2 : 3;
return Math.max(min, Math.min(max, playerIsAttacker ? min + garrison : max - garrison));
}

changeGarrison(provinceCode, delta)
{
this.provinces[provinceCode].garrison = Math.max(0, Math.min(10,
Expand Down Expand Up @@ -301,11 +316,16 @@ class GameData
let tribe = this.tribes[code];
let targets = new Set();
for (let prov of tribe.controlledProvinces)
for (let pot of g_GameData.provinces[prov].getLinks())
{
const pv = g_GameData.provinces[prov];
if (pv.garrison < 2)
pv.garrison++;
for (let pot of pv.getLinks())
{
if (g_GameData.provinces[pot].ownerTribe !== code)
if (pv.ownerTribe !== code)
targets.add(pot);
}
}

if (randBool(0.5) && targets.size)
{
Expand Down
22 changes: 16 additions & 6 deletions gui/campaigns/grand_strategy/init/InitPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class InitPage
desc += "\nWelcome to 0 A.D.'s Grand Strategy campaign, where you will take control of a country through the eyes of a Hero character. Fortify your lands, conquer your neighbors, and lead your civilization to victory.";
Engine.GetGUIObjectByName("campaignDescription").caption = desc;

Engine.GetGUIObjectByName("campaignImage").sprite = "color:0 0 0";
Engine.GetGUIObjectByName("campaignImage").sprite = "stretched:campaigns/grand_strategy/art/banner.png";

Engine.GetGUIObjectByName("playerSettings").caption = "Customize your civilization";

Expand Down Expand Up @@ -47,6 +47,13 @@ class InitPage
this.difficultySelect.list = ["Easy", "Medium", "Hard"];
this.difficultySelect.list_data = ["easy", "medium", "hard"];
this.difficultySelect.selected = 0;
this.difficultySelect.onHoverChange = () => {
this.difficultySelect.tooltip = [
"AI players will range from Sandbox to Easy difficulty",
"AI players will range from Easy to Hard difficulty",
"AI players will range from Medium to Very Hard difficulty",
]?.[this.difficultySelect.hovered] ?? "";
};

this.customHeroName = false;
this.customTribeName = false;
Expand Down Expand Up @@ -141,10 +148,11 @@ class InitPage
"width": 450,
"height": 200,
"title": "Start Campaign?",
"message": sprintf("You have chosen to start a campaign as the %(tribe)s of the %(civ)s.\nConfirm?",
"message": sprintf("You have chosen to start a campaign as a %(civ)s tribe called the “%(tribe)s”, in %(prov)s.\nConfirm?",
{
"tribe": this.tribeName.caption,
"civ": this.civSelect.list[this.civSelect.selected],
"prov": this.provinceSelect.list[this.provinceSelect.selected],
}),
"buttonCaptions": ["No", "Yes"],
},
Expand All @@ -155,10 +163,12 @@ class InitPage
{
// Writes g_GameData
GameData.createNewGame({
"civ": this.civSelect.list_data[this.civSelect.selected],
"tribeName": this.tribeName.caption,
"startProvince": this.provinceSelect.list_data[this.provinceSelect.selected],
});
"civ": this.civSelect.list_data[this.civSelect.selected],
"tribeName": this.tribeName.caption,
"startProvince": this.provinceSelect.list_data[this.provinceSelect.selected],
},
this.difficultySelect.list_data[this.difficultySelect.selected]
);
Engine.SwitchGuiPage("campaigns/grand_strategy/page.xml", {
"filename": CampaignRun.getCurrentRunFilename()
});
Expand Down

0 comments on commit 0730b46

Please sign in to comment.