This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
152 additions
and
112 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
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
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
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
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
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,112 +1,144 @@ | ||
import React, { useEffect, useRef } from "react"; | ||
import { Card } from "antd"; | ||
import { Game } from "../logic/game/game"; | ||
import { keyCaptureStart, keyCaptureStop } from "../core/keycapture"; | ||
import { gameRender } from "../logic/game/render"; | ||
import React, { useEffect, useRef, useState } from "react"; | ||
import { Button, Card } from "antd"; | ||
import { GameAiEval } from "../logic/gameAi/GameAiEval"; | ||
import { range } from "../core/common"; | ||
import { GameAI } from "../logic/gameAi/GameAi"; | ||
import { IASelectionFunctionType } from "../logic/ai/ga/gaProcesGenerationFunction"; | ||
import { Render, Events } from "matter-js"; | ||
import { renderPoint, renderLine } from "../utils/rendererUtils"; | ||
import { Game } from "../logic/game/game"; | ||
import { NeuralNet } from "../_old/logic/ai/nn/nn"; | ||
|
||
|
||
const debug = false; | ||
|
||
type TRunnerProps = { | ||
capture?: boolean, | ||
snapshot: NeuralNet[] | undefined, | ||
}; | ||
|
||
type TRendererProps = { | ||
width: number, | ||
height: number, | ||
}; | ||
const height = 300; | ||
const width = 300; | ||
|
||
const Renderer: React.FC<TRendererProps> = ({ height, width }) => { | ||
export const PlayPage: React.FC<TRunnerProps> = ({ capture, snapshot }) => { | ||
const runningState = useState(false); | ||
const [evaler, setEvaler] = useState<GameAiEval | undefined>(undefined); | ||
const ref = useRef<HTMLDivElement>(undefined as any); | ||
const [renderer, setRenderer] = useState<Render | undefined>(undefined); | ||
const setRunning = runningState[1]; | ||
|
||
const canStart = !(runningState[0] || !snapshot); | ||
|
||
const init = () => { | ||
setTimeout(() => { | ||
// _init(); | ||
}, (2000)); | ||
const start = () => { | ||
if (!canStart) return; | ||
setRunning(true); | ||
setEvaler(new GameAiEval(snapshot!, Game.SETTINGS_DEFAULT)); | ||
} | ||
|
||
const _init = async () => { | ||
if (!ref.current) | ||
return; | ||
// const game = new Game(); | ||
const stop = () => { | ||
setRunning(false); | ||
} | ||
|
||
useEffect(() => { | ||
if (runningState[0]) return; | ||
if (renderer) | ||
Render.stop(renderer) | ||
setRenderer(undefined); | ||
if (ref.current) ref.current.innerHTML = ""; | ||
}, [renderer, runningState[0]]) | ||
|
||
useEffect(() => { | ||
if (!ref.current || !evaler || !runningState[0]) return; | ||
// if (!ref.current) return; | ||
const element = ref.current; | ||
|
||
const gameAi = new GameAI({ | ||
aiParams: { | ||
sensors: Game.SETTINGS_DEFAULT.ai.sensorSidesArrayAngle, | ||
}, | ||
gaInit: { | ||
popSize: 5, | ||
proccessFunction: { | ||
breedingParents: 1, | ||
mutationRate: .01, | ||
selection: { | ||
type: IASelectionFunctionType.percent, | ||
value: 5, | ||
} | ||
} | ||
}, | ||
games: 2, | ||
gameSettings: Game.SETTINGS_DEFAULT, | ||
nnInit: { hiddenLayers: [5], }, | ||
}, () => { | ||
console.log("game ended"); | ||
const game = evaler.game; | ||
// const game = new Game(); | ||
const engine = game.engine; | ||
|
||
// create renderer | ||
const render = Render.create({ | ||
element, | ||
engine, | ||
options: { | ||
width, | ||
height, | ||
|
||
background: "#cccccc", | ||
wireframeBackground: "#0f0f13", | ||
wireframes: false, | ||
|
||
...(debug | ||
? { | ||
wireframes: true, | ||
showVelocity: true, | ||
showCollisions: true, | ||
showSeparations: true, | ||
showAngleIndicator: true, | ||
} : {}), | ||
} as any, | ||
}); | ||
|
||
range(0).forEach(x => { | ||
console.log(`generation ${x} best: ${gameAi.gann.ga.population[0].fitness}`); | ||
gameAi.next(() => { | ||
console.log("game ended"); | ||
setRenderer(render); | ||
|
||
// Render.run(render); | ||
|
||
// fit the render viewport to the scene | ||
(Render as any).lookAt(render, { | ||
min: { x: 0, y: 0 }, | ||
max: { x: game.settings.map.size, y: game.settings.map.size }, | ||
}); | ||
|
||
Events.on(render, "afterRender", () => { | ||
game.gameState.players.forEach((_p, pi) => { | ||
const res = game.sensor(pi); | ||
const playerPos = game.gameState.players[pi].body.position; | ||
|
||
res.forEach(res => { | ||
renderPoint(render, res.point); | ||
renderLine(render, playerPos, res.point); | ||
}); | ||
}); | ||
}) | ||
}); | ||
|
||
// const botsNNs = range(2).map(() => GameAiEval.initializeRandomBot( | ||
// { hiddens: [8, 5, 3], sensors: GameAI.defaultInitParams.aiParams.sensors } | ||
// )) | ||
const targetDelta = game.settings.simulation.delta; | ||
|
||
const pop = gameAi.gann.ga.population; | ||
const botsNNs = [pop[0].dna, pop[1].dna]; | ||
let last = Date.now(); | ||
const step = () => { | ||
const now = Date.now(); | ||
const delta = now - last; | ||
if (delta < targetDelta) return; | ||
|
||
const gameAiEval = new GameAiEval(botsNNs, Game.SETTINGS_DEFAULT); | ||
last += delta; | ||
// game.next(); | ||
evaler.next(); | ||
step(); | ||
}; | ||
|
||
const aiInput = () => gameAiEval.calculateBotResponse(); | ||
const rafLoop = () => { | ||
step(); | ||
if (!game.isGameOver) { | ||
// if ([runningState[0]]) | ||
requestAnimationFrame(rafLoop); | ||
} else { | ||
|
||
// keyCaptureStart(); | ||
// const userInput = () => { | ||
// const walk = capturedKeys.has("ArrowUp"); | ||
// const rotate = capturedKeys.has("ArrowLeft") ? -1 | ||
// : capturedKeys.has("ArrowRight") ? 1 | ||
// : 0 | ||
// ; | ||
// const use = capturedKeys.has(" "); | ||
// return { players: [{ walk, rotate, use } as any] }; | ||
// }; | ||
// todo: print winner on canvas | ||
console.log(`winner is ${game.gameState.winner}`); | ||
} | ||
}; | ||
requestAnimationFrame(rafLoop); | ||
|
||
gameRender({ | ||
element, game: gameAiEval.game, height, width, debug, input: aiInput, | ||
}); | ||
}; | ||
useEffect(init, []); | ||
Render.run(render); | ||
}, [evaler]) | ||
|
||
return <> | ||
<div style={{ justifyContent: "center", display: "flex" }}> | ||
<div ref={ref} /> | ||
</div> | ||
</>; | ||
}; | ||
const button = runningState[0] | ||
? <Button onClick={stop}>Stop</Button> | ||
: <Button onClick={start} disabled={!canStart}>Play</Button> | ||
; | ||
|
||
export const PlayPage: React.FC<TRunnerProps> = ({ capture }) => { | ||
if (capture) | ||
keyCaptureStart(); | ||
else | ||
keyCaptureStop(); | ||
|
||
return <> | ||
<Card title="Play"> | ||
<Renderer {...{ width: 500, height: 500 }} /> | ||
<Card title="Play" extra={button}> | ||
<div style={{ justifyContent: "center", display: "flex" }}> | ||
<div ref={ref} /> | ||
</div> | ||
</Card> | ||
</>; | ||
}; |
Oops, something went wrong.