Skip to content

Commit

Permalink
Rough out AI
Browse files Browse the repository at this point in the history
  • Loading branch information
bdjewkes committed Sep 7, 2019
1 parent ece0c56 commit 4381bea
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ node_modules/
# Ruby
/.bundle
.DS_Store
build/
22 changes: 11 additions & 11 deletions client/app.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { sendAction, requestLogin } from "./server.js";
import { chatData, initializeChatListener } from "./chat.js";
import { printMessage, EventType } from './eventBox.js';
import { vector, Vector } from "./vectors.js"
import * as Vec from "./vectors.js"
import { RenderContext, World, Ability, Action } from "./domain.js";
import { drawRect, drawGrid, drawTile } from "./draw.js";
import { getTileset } from "./tileset.js";
import { setHealth, setUsername } from "./stats.js";
import { sanitize } from "./sanitizer.js";
import { initializeInputListeners } from "./inputs.js";
import { vector, Vector } from "./common/vectors.js";
import * as Vec from "./common/vectors.js";
import { sendAction, requestLogin } from "./common/server.js";
import { RenderContext, World, Ability, Action } from "./common/domain.js";
import { chatData, initializeChatListener } from "./player/chat.js";
import { printMessage, EventType } from './player/eventBox.js';
import { drawRect, drawGrid, drawTile } from "./player/draw.js";
import { getTileset } from "./player/tileset.js";
import { setHealth, setUsername } from "./player/stats.js";
import { sanitize } from "./player/sanitizer.js";
import { initializeInputListeners } from "./player/inputs.js";

declare var io: any;

Expand Down
40 changes: 40 additions & 0 deletions client/bot/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { World, Action } from "../common/domain.js";
import { sendAction, requestLogin } from "../common/server.js"
import * as io from "socket.io-client";

// hack -- to connect to the game, use this:
var socket = io("http://xp-game.codeselfstudy.com");
//var socket = io("http://localhost:5000");

interface AiConfig {
name: string,
respawnTime: number
}

export interface AiContext {
act(action: Action): void;
world: World;
}

/**
* Connect to the socket server, and spawn an ai entity. The ai entity is
* controlled via the `update` function, which is called each tick with the
* most recent game state, `World`.
*/
export function initialize(cfg: AiConfig,
update: (ctx: AiContext) => void) {
function respawn() {
requestLogin(socket, cfg.name);
}
function act(action: Action){
sendAction(socket, action);
}

socket.on('connect', () => respawn());
socket.on('despawn', () => {
setTimeout(respawn, cfg.respawnTime);
});
socket.on('world', (world: World) => {
update({act, world});
});
}
24 changes: 24 additions & 0 deletions client/bot/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Direction } from "../common/vectors.js";
import { ActionKind } from "../common/domain.js";
import { initialize, AiContext } from "./base.js";

/**
* This is a super simple ai implementation that moves randomly
* every so often.
*/

function direction(): Direction{
let index = Math.floor(Math.random() * 4);
let direction: Direction[] = ["North", "East", "West", "South"];
return direction[index];
}

function update(context: AiContext){
var rand = Math.random();
var kind: ActionKind = Math.random() >= 0.5 ? "Move" : "Attack";
if(rand > 0.90){
context.act({kind: kind, direction: direction()});
}
}

initialize({name: "Dopey the Dwarf", respawnTime: 5000}, update);
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion client/chat.ts → client/player/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Updating the DOM is handled in the eventBox module.
*/
import { sendChatMessage } from "./server.js";
import { sendChatMessage } from "../common/server.js";
import { printMessage, EventType } from './eventBox.js';

// Represents a chat message
Expand Down
6 changes: 3 additions & 3 deletions client/draw.ts → client/player/draw.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RenderContext } from "./domain.js";
import { Vector, vector } from "./vectors.js";
import * as Vec from "./vectors.js";
import { RenderContext } from "../common/domain.js";
import { Vector, vector } from "../common/vectors.js";
import * as Vec from "../common/vectors.js";


export function drawGrid(c: RenderContext, viewOffset: Vector, width: number, height: number){
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion client/inputs.ts → client/player/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This module handles key presses.
*/
import { Action, ActionKind } from "./domain.js";
import { Action, ActionKind } from "../common/domain.js";

let KeyBindings = new Map<string, ActionKind>([
["a", "Attack"],
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion client/tileset.ts → client/player/tileset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tileset } from "./domain";
import { Tileset } from "../common/domain";


export async function getTileset(): Promise<Tileset> {
Expand Down
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"dependencies": {
"socket.io-client": "^2.2.0"
},
"devDependencies": {
"@types/node": "^12.7.3"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
14 changes: 14 additions & 0 deletions tsconfig-node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"sourceMap": true,
"target": "es6",
"module": "commonjs",
"outDir": "./build/"
},
"include": [
"./client/**/*"
],
"exclude":[
"node_modules"
]
}
8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"outDir": "./server/static/dist/"
},
"include": [
"./client/**/*"
"./client/*",
"./client/player/*",
"./client/common/*"
],
"exclude":[
"./client/bots/*",
"node_modules"
]
}

0 comments on commit 4381bea

Please sign in to comment.