Skip to content

Commit

Permalink
create heal units
Browse files Browse the repository at this point in the history
  • Loading branch information
anan474 committed Jul 9, 2024
1 parent 42b9921 commit 170a5d0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
1 change: 1 addition & 0 deletions athena/lib/__tests__/determineUnitsToCreate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const player1 = map.getCurrentPlayer();
const options = {
canCreateBuildUnits: true,
canCreateCaptureUnits: true,
canCreateHealingUnits: true,
canCreateSupplyUnits: true,
canCreateTransportUnits: true,
};
Expand Down
78 changes: 73 additions & 5 deletions athena/lib/determineUnitsToCreate.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import ImmutableMap from '@nkzw/immutable-map';
import { PotentialUnitAbilities } from '../../dionysus/lib/getPossibleUnitAbilities.tsx';
import needsSupply from '../../dionysus/lib/needsSupply.tsx';
import { BuildableTiles, MinFunds } from '../info/Building.tsx';
import { Ability, UnitInfo } from '../info/Unit.tsx';
import { BuildableTiles, MinFunds, Shelter } from '../info/Building.tsx';
import { Ability, Medic, SupportShip, UnitInfo } from '../info/Unit.tsx';
import Building from '../map/Building.tsx';
import { getEntityInfoGroup } from '../map/Entity.tsx';
import Player, { PlayerID } from '../map/Player.tsx';
import { MaxHealth } from '../map/Configuration.tsx';
import { EntityType, getEntityInfoGroup } from '../map/Entity.tsx';
import Player from '../map/Player.tsx';
import Unit from '../map/Unit.tsx';
import Vector from '../map/Vector.tsx';
import MapData from '../MapData.tsx';
import calculateFunds, {
calculateTotalPossibleFunds,
} from './calculateFunds.tsx';
import getHealCost from './getHealCost.tsx';

export default function determineUnitsToCreate(
map: MapData,
currentPlayer: Player | PlayerID,
currentPlayer: Player,
playerUnits: ReadonlyArray<Unit>,
buildableUnits: ReadonlyArray<UnitInfo>,
{
canCreateBuildUnits,
canCreateCaptureUnits,
canCreateHealingUnits,
canCreateSupplyUnits,
canCreateTransportUnits,
}: PotentialUnitAbilities = {
canCreateBuildUnits: true,
canCreateCaptureUnits: true,
canCreateHealingUnits: true,
canCreateSupplyUnits: true,
canCreateTransportUnits: true,
},
Expand Down Expand Up @@ -57,6 +61,56 @@ export default function determineUnitsToCreate(
: _structures;

const totalFunds = calculateTotalPossibleFunds(map);

const canCreateMedic = buildableUnits.some((info) => info == Medic);
const shelterCount = map.buildings
.filter((building) => building.info == Shelter)
.count();
const medicCount = playerUnits.filter((unit) => unit.info == Medic).length;
const createMedicScoreSubtractor = (medicCount + shelterCount) * 100;
const createMedicScore =
playerUnits
.filter((unit) => Medic.configuration.healTypes?.has(unit.info.type))
.map((unit) => 20 + MaxHealth - unit.health)
.reduce((total, score) => total + score, 0) - createMedicScoreSubtractor;
const healableByMedic = playerUnits
.filter((unit) => Medic.configuration.healTypes?.has(unit.info.type))
.sort((unitA, unitB) => unitB.health - unitA.health);
const leastHealableCostByMedic = healableByMedic.length
? getHealCost(healableByMedic[0], currentPlayer)
: null;
const medicHaveEnoughFundToHealLeastUnit =
calculateFunds(map, currentPlayer) >
Medic.getCostFor(currentPlayer) + (leastHealableCostByMedic ?? 0);

const canCreateSupportShip = buildableUnits.some(
(info) => info == SupportShip,
);
const createSupportShipScore =
playerUnits
.filter(
(unit) =>
SupportShip.configuration.healTypes?.has(unit.info.type) &&
unit.info.type != EntityType.Infantry,
)
.map((unit) => 20 + MaxHealth - unit.health)
.reduce((total, score) => total + score, 0) -
playerUnits.filter((unit) => unit.info == SupportShip).length * 100;
const healableBySupportShip = playerUnits
.filter(
(unit) =>
SupportShip.configuration.healTypes?.has(unit.info.type) &&
unit.info.type != EntityType.Infantry,
)
.sort((unitA, unitB) => unitB.health - unitA.health);
const leastHealableCostBySupportShip = healableBySupportShip.length
? getHealCost(healableBySupportShip[0], currentPlayer)
: null;
const supportShipHaveEnoughFundToHealLeastUnit =
calculateFunds(map, currentPlayer) >
SupportShip.getCostFor(currentPlayer) +
(leastHealableCostBySupportShip ?? 0);

const unitsWithCreateBuildingsAbility = playerUnits.filter((unit) =>
unit.info.hasAbility(Ability.CreateBuildings),
);
Expand All @@ -81,6 +135,20 @@ export default function determineUnitsToCreate(
);

if (
canCreateHealingUnits &&
medicHaveEnoughFundToHealLeastUnit &&
canCreateMedic &&
createMedicScore >= 100
) {
return buildableUnits.filter((info) => info == Medic);
} else if (
canCreateHealingUnits &&
supportShipHaveEnoughFundToHealLeastUnit &&
canCreateSupportShip &&
createSupportShipScore >= 100
) {
return buildableUnits.filter((info) => info == SupportShip);
} else if (
canCreateSupplyUnits &&
unitsWithSupplyNeeds.length &&
unitsWithSupplyAbility.length <= playerUnits.length * 0.05 &&
Expand Down
8 changes: 8 additions & 0 deletions dionysus/lib/getPossibleUnitAbilities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Player from '@deities/athena/map/Player.tsx';
export type PotentialUnitAbilities = Readonly<{
canCreateBuildUnits: boolean;
canCreateCaptureUnits: boolean;
canCreateHealingUnits: boolean;
canCreateSupplyUnits: boolean;
canCreateTransportUnits: boolean;
}>;
Expand All @@ -26,6 +27,7 @@ export default function getPossibleUnitAbilities(
unitInfos: ReadonlyArray<UnitInfo>,
) {
let canCreateBuildUnits = false;
let canCreateHealingUnits = false;
let canCreateCaptureUnits = false;
let canCreateSupplyUnits = false;
let canCreateTransportUnits = false;
Expand All @@ -35,6 +37,9 @@ export default function getPossibleUnitAbilities(
canCreateCaptureUnits = true;
}

if (unit.hasAbility(Ability.Heal)) {
canCreateHealingUnits = true;
}
if (unit.hasAbility(Ability.CreateBuildings)) {
canCreateBuildUnits = true;
}
Expand All @@ -49,13 +54,15 @@ export default function getPossibleUnitAbilities(

if (
canCreateBuildUnits &&
canCreateHealingUnits &&
canCreateCaptureUnits &&
canCreateSupplyUnits &&
canCreateTransportUnits
) {
return {
canCreateBuildUnits,
canCreateCaptureUnits,
canCreateHealingUnits,
canCreateSupplyUnits,
canCreateTransportUnits,
};
Expand All @@ -65,6 +72,7 @@ export default function getPossibleUnitAbilities(
return {
canCreateBuildUnits,
canCreateCaptureUnits,
canCreateHealingUnits,
canCreateSupplyUnits,
canCreateTransportUnits,
};
Expand Down

0 comments on commit 170a5d0

Please sign in to comment.