-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,161 additions
and
9,462 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"modelTopology":{"class_name":"Sequential","config":{"name":"sequential_1","layers":[{"class_name":"LSTM","config":{"name":"lstm_LSTM1","trainable":true,"batch_input_shape":[null,15,270],"dtype":"float32","units":30,"activation":"relu","recurrent_activation":"hard_sigmoid","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"recurrent_initializer":{"class_name":"Orthogonal","config":{"gain":1,"seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"unit_forget_bias":null,"kernel_regularizer":null,"recurrent_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"recurrent_constraint":null,"bias_constraint":null,"dropout":0,"recurrent_dropout":0,"implementation":null,"return_sequences":true,"return_state":false,"go_backwards":false,"stateful":false,"unroll":false}},{"class_name":"TimeDistributed","config":{"layer":{"class_name":"Dense","config":{"units":270,"activation":"linear","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense1","trainable":true}},"name":"time_distributed_TimeDistributed1","trainable":true}}]},"keras_version":"tfjs-layers 4.21.0","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["weights.bin"],"weights":[{"name":"lstm_LSTM1/kernel","shape":[270,120],"dtype":"float32"},{"name":"lstm_LSTM1/recurrent_kernel","shape":[30,120],"dtype":"float32"},{"name":"lstm_LSTM1/bias","shape":[120],"dtype":"float32"},{"name":"time_distributed_TimeDistributed1/kernel","shape":[30,270],"dtype":"float32"},{"name":"time_distributed_TimeDistributed1/bias","shape":[270],"dtype":"float32"}]}],"format":"layers-model","generatedBy":"TensorFlow.js tfjs-layers v4.21.0","convertedBy":null} | ||
{"modelTopology":{"class_name":"Sequential","config":{"name":"sequential_1","layers":[{"class_name":"LSTM","config":{"name":"lstm_LSTM1","trainable":true,"batch_input_shape":[null,15,150],"dtype":"float32","units":30,"activation":"relu","recurrent_activation":"hard_sigmoid","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"recurrent_initializer":{"class_name":"Orthogonal","config":{"gain":1,"seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"unit_forget_bias":null,"kernel_regularizer":null,"recurrent_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"recurrent_constraint":null,"bias_constraint":null,"dropout":0,"recurrent_dropout":0,"implementation":null,"return_sequences":true,"return_state":false,"go_backwards":false,"stateful":false,"unroll":false}},{"class_name":"TimeDistributed","config":{"layer":{"class_name":"Dense","config":{"units":270,"activation":"linear","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense1","trainable":true}},"name":"time_distributed_TimeDistributed1","trainable":true}}]},"keras_version":"tfjs-layers 4.21.0","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["weights.bin"],"weights":[{"name":"lstm_LSTM1/kernel","shape":[150,120],"dtype":"float32"},{"name":"lstm_LSTM1/recurrent_kernel","shape":[30,120],"dtype":"float32"},{"name":"lstm_LSTM1/bias","shape":[120],"dtype":"float32"},{"name":"time_distributed_TimeDistributed1/kernel","shape":[30,270],"dtype":"float32"},{"name":"time_distributed_TimeDistributed1/bias","shape":[270],"dtype":"float32"}]}],"format":"layers-model","generatedBy":"TensorFlow.js tfjs-layers v4.21.0","convertedBy":null} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
games/masterplan/src/screens/battle/game/game-world-render.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { GameWorld } from './game-world'; | ||
import { createTerrainTexture } from './terrain/terrain-renderer'; | ||
|
||
export class GameWorldRender { | ||
terrainCanvas: HTMLCanvasElement; | ||
|
||
constructor(gameWorld: GameWorld) { | ||
this.terrainCanvas = createTerrainTexture( | ||
gameWorld.terrain.width, | ||
gameWorld.terrain.height, | ||
gameWorld.terrain.heightMap, | ||
gameWorld.terrain.tileSize, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
games/masterplan/src/screens/battle/model/convert-input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Unit, UnitType } from '../../designer/designer-types'; | ||
import { MAX_COL, MAX_ROW } from '../consts'; | ||
import { TerrainData } from '../game/terrain/terrain-generator'; | ||
import { | ||
INPUT_CELL_INDEX_MAP, | ||
INPUT_CELL_UNIT_INDEX_MAP, | ||
INPUT_COLS, | ||
INPUT_ROWS, | ||
ModelInput, | ||
ModelInputCell, | ||
} from './types'; | ||
|
||
export function unitsToModelInput(units: Unit[], terrainData: TerrainData): ModelInput { | ||
const cols = INPUT_COLS; | ||
const rows = INPUT_ROWS; | ||
const data = Array.from({ length: rows }, () => | ||
Array.from({ length: cols }, () => Array(Object.keys(INPUT_CELL_INDEX_MAP).length).fill(0) as ModelInputCell), | ||
); | ||
|
||
let maxUnitCount = 0; | ||
|
||
for (const u of units) { | ||
for (let i = 0; i < u.sizeRow; i++) { | ||
for (let j = 0; j < u.sizeCol; j++) { | ||
const gameRow = MAX_ROW / 2 + u.row + i; | ||
const gameCol = MAX_COL / 2 + u.col + j; | ||
const inputRow = Math.min(Math.floor((gameRow * rows) / MAX_ROW), INPUT_ROWS - 1); | ||
const inputCol = Math.min(Math.floor((gameCol * cols) / MAX_COL), INPUT_COLS - 1); | ||
if (data[inputRow] && data[inputRow][inputCol]) { | ||
data[inputRow][inputCol][INPUT_CELL_INDEX_MAP[u.type]] = | ||
1 + (data[inputRow][inputCol][INPUT_CELL_INDEX_MAP[u.type]] || 0); | ||
maxUnitCount = Math.max(maxUnitCount, data[inputRow][inputCol][INPUT_CELL_INDEX_MAP[u.type]]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
const maxHeight = Math.max(...terrainData.heightMap.flat()); | ||
|
||
// Add terrain height to the input | ||
for (let row = 0; row < rows; row++) { | ||
for (let col = 0; col < cols; col++) { | ||
const gameRow = Math.floor((row * MAX_ROW) / rows); | ||
const gameCol = Math.floor((col * MAX_COL) / cols); | ||
const height = terrainData.heightMap[gameRow][gameCol]; | ||
data[row][col][INPUT_CELL_INDEX_MAP.terrainHeight] = height / maxHeight; | ||
} | ||
} | ||
|
||
// Normalize unit counts using maxUnitCount | ||
for (const row of data) { | ||
for (const cell of row) { | ||
for (const key in INPUT_CELL_UNIT_INDEX_MAP) { | ||
cell[INPUT_CELL_UNIT_INDEX_MAP[key as UnitType]] = | ||
cell[INPUT_CELL_UNIT_INDEX_MAP[key as UnitType]] / maxUnitCount; | ||
} | ||
} | ||
} | ||
|
||
return { cols, rows, data }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10
games/masterplan/src/screens/battle/model/predict-browser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { predict } from './tf-browser'; | ||
import { Unit } from '../../designer/designer-types'; | ||
import { modelInputToUnits, unitsToModelInput } from './convert'; | ||
import { unitsToModelInput } from './convert-input'; | ||
import { modelOutputToUnits } from './convert-output'; | ||
import { countSoldiers, trimUnits } from './units-trim'; | ||
import { consolidateUnits } from './units-consolidate'; | ||
import { TerrainData } from '../game/terrain/terrain-generator'; | ||
|
||
export async function predictCounterPlan(playerPlan: Unit[]): Promise<Unit[]> { | ||
const modelInput = unitsToModelInput(playerPlan); | ||
export async function predictCounterPlan(playerPlan: Unit[], terrainData: TerrainData): Promise<Unit[]> { | ||
const modelInput = unitsToModelInput(playerPlan, terrainData); | ||
const modelOutput = await predict(modelInput); | ||
return trimUnits(consolidateUnits(modelInputToUnits(modelOutput)), countSoldiers(playerPlan)); | ||
return trimUnits(consolidateUnits(modelOutputToUnits(modelOutput)), countSoldiers(playerPlan)); | ||
} |
Oops, something went wrong.