GOWOG:
- Original project: github.com/giongto35/g…
- I adjusted: github.com/Kirk-Wang/g…
GOWOG is a mini, multiplayer Web game written using Golang.
Play the game
Demo:game.giongto35.com
AI experiment on Agent
Because the server, client, and message are separate, it is easy to communicate with the back end. This project is an AI agent written in Python that can learn to interact with the environment. The experiment uses neuroevolution to find a path through a maze.
YouTube Demo: www.youtube.com/watch?v=pWb…
Local Docker runs
run_local.sh
#! /bin/bash
docker build . --build-arg HOSTNAME=localhost:8080 -t gowog_local|| exit
docker stop gowog
docker rm gowog
docker run --privileged -d --name gowog -p 8080:8080 gowog_local server -prod client/build/
Copy the code
Execute, MY local is Mac:
./run_local.sh
Copy the code
Open http://localhost:8080
Local development
The game consists of two parts: the server and the client. The server uses Golang, and the client uses Node.js and Phaser game engines.
The server
For less adjusted items: Kirk-Wang/ Gowog
My local environment is go 1.14.
Local boot:
go run cmd/server/*
Copy the code
The server will listen on 8080.
The client
npm install
npm run dev -- --env.HOST_IP=localhost:8080 # HOST_IP -> Server address
Copy the code
Go to http://localhost:3000
Pay attention to
During development, the client runs on port 3000 and the server runs on port 8080.
In production and Docker environments, the Client is built and the Golang server returns the Client page on the same port 8080. Therefore, if we run the Docker environment, the game will run at http://localhost:8080 in the browser.
Communication agreement
The communication packet between server and client is based on protobuf. Install protoc to generate protobuf.
- Google. Making. IO/proto – lens /…
Every time you make a package singature change in server/message.proto. Please run:
cd server
./generate.sh
Copy the code
Game Front-end Design
This front-end project is based on:
- Github.com/RenaudROHLI…
├ ─ ─ client │ ├ ─ ─ index. The HTML │ ├ ─ ─ the SRC │ │ ├ ─ ─ config. Js: Javascript Config │ │ ├─ ├─.html │ │ ├─.js │ ├─ sprites │ │ ├─ Leaderboard. Leaderboard Object │ │ ├─Map.js: Map│ ├─ ├─ ├─ ├.js: └ │ ├─ ├.js: Shoot object │ ├─ States │ │ ├─ Boot. Js Boot screen │ │ ├─const.js │ │ │ ├ ─ ─ Game. Js: Game master │ │ │ ├ ─ ─ message_pb. Js: Protobuf Message │ │ │ ├ ─ ─ Splash, js │ │ │ └ ─ ─ utils. Js │ │ └ ─ ─ utils. JsCopy the code
Each object is a class inherited from Sprite. The player includes the shootManager, which generates a new bullet every time it is fired.
Game backend design
Components
There are five main entities in the game. Their status is private
entity | Private state | |
---|---|---|
Client |
websocket.Conn | Client holds the Websocket connection |
Hub |
Client | The Hub handles all traffic and contains all client lists |
ObjManager |
Player, Shoot, … | ObjManager contains all players and shoots and handles the game logic |
Game Master |
ObjManager, Hub | The Master Object consists of an ObjManager and a Hub |
Architecture
Different entities call each other through channels wrapped in functions.
Client and Server interaction design
Player Connect
Player Disconnect (Player disconnects)
Client input
Profile
Profile is a way to study Golang performance and find out slow components. When running the server, you can configure the server using the flags — CPUProfile and — memProfile.
cd server
go run cmd/server/* --cpuprofile --memprofile
Copy the code
The code structure
├ ─ ─ server │ ├ ─ ─ CMD │ │ └ ─ ─ server │ │ └ ─ ─ for server go: Entrypoint running server │ ├ ─ ─ game │ │ ├ ─ ─ common │ │ ├ ─ ─ the config │ │ │ └ ─ ─ 1. The map: Performers 0 and 1 │ ├─ │ ├─ Game. Go: Game master objects, Containing logic and communication │ │ ├─ Mappkg │ ├─ objManager │ │ ├─ PlayerPkg │ - Shape │ ├─ shootpkg │ │ ├ ─ ─ types. Go │ │ └ ─ ─ the ws │ │ ├ ─ ─ wsclient. Go │ │ └ ─ ─ wshub. Go │ ├ ─ ─ the generate. Sh: ├─ Message_proto ├─ Message_proto ├─ get protobuf for server + client + AI environment │ ├─ Message_proto ├─ Dockerfile └ ─ ─ run_local. ShCopy the code
AI training design scheme
The repository contains a CS2D environment that follows the openAI Gym format and training scripts. The training script uses NeuroEvolution to find the shortest path to a destination in a maze.
www.youtube.com/watch?v=pWb…
run
Follow the instructions to run the GoWOg environment. That is, local Docker runs:
./run_local.sh
Copy the code
Use virtualenv to set the PYTHon3 virtual environment.
- The installation
requirements.txt
Contains libraries.
Run the training script
python train_ga.py -n save_file_name
Copy the code
Save_fie_name is where the weights are saved. Next time, if we specify an existing file, it will continue training from the weights in the last run of that file.
Genetic Algorithm
Implementation of _cs2denv_ga.py _
For the purpose of machine learning, CS2D Agent is built on CS2D. It follows openAI Gym and supports basic agent methods, including reset(), step(), observation_space and action_space.
ObservationSpace is a one-dimensional array constructed from the update_Player message from the server
- Player position (
The player position
), player size (Player size
), number of columns (The number of columns
), number of rows (The number of rows
), block width (Block width
), block height (Block height
) - To the left, right, up, down to the nearest block (
block
). This input is to avoid collisions - The player’s position in the Binary block map. The map is
0
和1
的2
Dimensional array (0
Is empty,1
For the block) - binary block map
The reward is 1 / Distance. If the agent approaches the goal of 100 points, the reward is 1, and the plot ends.
NeuroEvolution
Implementation of _train_ga.py _
Neural networks get the best action by running the input (the observation space) through the Neural Network.
NeuroEvolution is AI that uses evolutionary algorithms to continuously improve artificial neural networks. For each iteration (generation), the program generates a new set of neural network weights based on the best Settings from the previous iteration. The process of generating an NN from a previous NN(neural network) is called Mutate, which adds random noise to each parameter in the neural network.
One particular improvement is that we store only the list of noise seeds applied to the neural network, not all the weights. Because all the randomizations are the same under the same seed, a seed can represent a mutation operator of a network. Instead of preserving the ownership values of each generation, we simply store a set of seeds from the beginning to the current generation, and then reconstruct the weights from this set of seeds to get the weights of all the neural networks.
The code is based On Maxim Lapan’s “Deep Reinforcement Learning Hands-on “.
- Github.com/PacktPublis…