From 06d4eff5c955e410f938b30014622604cbb8c4cd Mon Sep 17 00:00:00 2001 From: Vitor Hideyoshi Date: Sat, 5 Jul 2025 22:34:23 -0300 Subject: [PATCH] Changes to esbuild and adds initial creation of creaps --- .prettierrc | 2 +- esbuild.config.mjs | 12 + package-lock.json | 467 ++++++++++++++++++++++++++++++ package.json | 7 +- src/main.ts | 57 ++-- src/spawnHandler.ts | 84 +++++- src/types/creeps.ts | 31 ++ src/types/gameConfig.ts | 34 +++ src/utils/funcs/get_role_const.ts | 8 + 9 files changed, 669 insertions(+), 33 deletions(-) create mode 100644 esbuild.config.mjs create mode 100644 src/types/creeps.ts create mode 100644 src/types/gameConfig.ts create mode 100644 src/utils/funcs/get_role_const.ts diff --git a/.prettierrc b/.prettierrc index f554efe..d3d6f72 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,6 +1,6 @@ { "semi": true, - "tabWidth": 2, + "tabWidth": 4, "printWidth": 120, "singleQuote": false, "trailingComma": "none", diff --git a/esbuild.config.mjs b/esbuild.config.mjs new file mode 100644 index 0000000..e8ab925 --- /dev/null +++ b/esbuild.config.mjs @@ -0,0 +1,12 @@ +import { build } from 'esbuild'; + +build({ + entryPoints: ['src/main.ts'], + bundle: true, + target: 'es2018', // Screeps supports ES2018 well + platform: 'node', + format: 'cjs', + outdir: 'dist', + sourcemap: true, + logLevel: 'info', +}).catch(() => process.exit(1)); diff --git a/package-lock.json b/package-lock.json index fc5aa4d..c17e6d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "@typescript-eslint/parser": "^8.35.1", "@typescript-eslint/typescript-estree": "^8.35.1", "chai": "^5.2.0", + "esbuild": "^0.25.5", "eslint": "^9.30.1", "eslint-config-prettier": "^10.1.5", "eslint-import-resolver-typescript": "^4.4.4", @@ -92,6 +93,431 @@ "tslib": "^2.4.0" } }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.5.tgz", + "integrity": "sha512-9o3TMmpmftaCMepOdA5k/yDw8SfInyzWWTjYTFCX3kPSDJMROQTb8jg+h9Cnwnmm1vOzvxN7gIfB5V2ewpjtGA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.25.5.tgz", + "integrity": "sha512-AdJKSPeEHgi7/ZhuIPtcQKr5RQdo6OO2IL87JkianiMYMPbCtot9fxPbrMiBADOWWm3T2si9stAiVsGbTQFkbA==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.25.5.tgz", + "integrity": "sha512-VGzGhj4lJO+TVGV1v8ntCZWJktV7SGCs3Pn1GRWI1SBFtRALoomm8k5E9Pmwg3HOAal2VDc2F9+PM/rEY6oIDg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.25.5.tgz", + "integrity": "sha512-D2GyJT1kjvO//drbRT3Hib9XPwQeWd9vZoBJn+bu/lVsOZ13cqNdDeqIF/xQ5/VmWvMduP6AmXvylO/PIc2isw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.25.5.tgz", + "integrity": "sha512-GtaBgammVvdF7aPIgH2jxMDdivezgFu6iKpmT+48+F8Hhg5J/sfnDieg0aeG/jfSvkYQU2/pceFPDKlqZzwnfQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.25.5.tgz", + "integrity": "sha512-1iT4FVL0dJ76/q1wd7XDsXrSW+oLoquptvh4CLR4kITDtqi2e/xwXwdCVH8hVHU43wgJdsq7Gxuzcs6Iq/7bxQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.25.5.tgz", + "integrity": "sha512-nk4tGP3JThz4La38Uy/gzyXtpkPW8zSAmoUhK9xKKXdBCzKODMc2adkB2+8om9BDYugz+uGV7sLmpTYzvmz6Sw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.25.5.tgz", + "integrity": "sha512-PrikaNjiXdR2laW6OIjlbeuCPrPaAl0IwPIaRv+SMV8CiM8i2LqVUHFC1+8eORgWyY7yhQY+2U2fA55mBzReaw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.25.5.tgz", + "integrity": "sha512-cPzojwW2okgh7ZlRpcBEtsX7WBuqbLrNXqLU89GxWbNt6uIg78ET82qifUy3W6OVww6ZWobWub5oqZOVtwolfw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.25.5.tgz", + "integrity": "sha512-Z9kfb1v6ZlGbWj8EJk9T6czVEjjq2ntSYLY2cw6pAZl4oKtfgQuS4HOq41M/BcoLPzrUbNd+R4BXFyH//nHxVg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.25.5.tgz", + "integrity": "sha512-sQ7l00M8bSv36GLV95BVAdhJ2QsIbCuCjh/uYrWiMQSUuV+LpXwIqhgJDcvMTj+VsQmqAHL2yYaasENvJ7CDKA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.25.5.tgz", + "integrity": "sha512-0ur7ae16hDUC4OL5iEnDb0tZHDxYmuQyhKhsPBV8f99f6Z9KQM02g33f93rNH5A30agMS46u2HP6qTdEt6Q1kg==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.25.5.tgz", + "integrity": "sha512-kB/66P1OsHO5zLz0i6X0RxlQ+3cu0mkxS3TKFvkb5lin6uwZ/ttOkP3Z8lfR9mJOBk14ZwZ9182SIIWFGNmqmg==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.25.5.tgz", + "integrity": "sha512-UZCmJ7r9X2fe2D6jBmkLBMQetXPXIsZjQJCjgwpVDz+YMcS6oFR27alkgGv3Oqkv07bxdvw7fyB71/olceJhkQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.25.5.tgz", + "integrity": "sha512-kTxwu4mLyeOlsVIFPfQo+fQJAV9mh24xL+y+Bm6ej067sYANjyEw1dNHmvoqxJUCMnkBdKpvOn0Ahql6+4VyeA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.25.5.tgz", + "integrity": "sha512-K2dSKTKfmdh78uJ3NcWFiqyRrimfdinS5ErLSn3vluHNeHVnBAFWC8a4X5N+7FgVE1EjXS1QDZbpqZBjfrqMTQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.25.5.tgz", + "integrity": "sha512-uhj8N2obKTE6pSZ+aMUbqq+1nXxNjZIIjCjGLfsWvVpy7gKCOL6rsY1MhRh9zLtUtAI7vpgLMK6DxjO8Qm9lJw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.25.5.tgz", + "integrity": "sha512-pwHtMP9viAy1oHPvgxtOv+OkduK5ugofNTVDilIzBLpoWAM16r7b/mxBvfpuQDpRQFMfuVr5aLcn4yveGvBZvw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.25.5.tgz", + "integrity": "sha512-WOb5fKrvVTRMfWFNCroYWWklbnXH0Q5rZppjq0vQIdlsQKuw6mdSihwSo4RV/YdQ5UCKKvBy7/0ZZYLBZKIbwQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.25.5.tgz", + "integrity": "sha512-7A208+uQKgTxHd0G0uqZO8UjK2R0DDb4fDmERtARjSHWxqMTye4Erz4zZafx7Di9Cv+lNHYuncAkiGFySoD+Mw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.25.5.tgz", + "integrity": "sha512-G4hE405ErTWraiZ8UiSoesH8DaCsMm0Cay4fsFWOOUcz8b8rC6uCvnagr+gnioEjWn0wC+o1/TAHt+It+MpIMg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.25.5.tgz", + "integrity": "sha512-l+azKShMy7FxzY0Rj4RCt5VD/q8mG/e+mDivgspo+yL8zW7qEwctQ6YqKX34DTEleFAvCIUviCFX1SDZRSyMQA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.25.5.tgz", + "integrity": "sha512-O2S7SNZzdcFG7eFKgvwUEZ2VG9D/sn/eIiz8XRZ1Q/DO5a3s76Xv0mdBzVM5j5R639lXQmPmSo0iRpHqUUrsxw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.25.5.tgz", + "integrity": "sha512-onOJ02pqs9h1iMJ1PQphR+VZv8qBMQ77Klcsqv9CNW2w6yLqoURLcgERAIurY6QE63bbLuqgP9ATqajFLK5AMQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.25.5.tgz", + "integrity": "sha512-TXv6YnJ8ZMVdX+SXWVBo/0p8LTcrUYngpWjvm91TMjjBQii7Oz11Lw5lbDV5Y0TzuhSJHwiH4hEtC1I42mMS0g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, "node_modules/@eslint-community/eslint-utils": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.7.0.tgz", @@ -2694,6 +3120,47 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/esbuild": { + "version": "0.25.5", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.25.5.tgz", + "integrity": "sha512-P8OtKZRv/5J5hhz0cUAdu/cLuPIKXpQl1R9pZtvmHWQvrAUVd0UNIPT4IB4W3rNOqVO0rlqHmCIbSwxh/c9yUQ==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.25.5", + "@esbuild/android-arm": "0.25.5", + "@esbuild/android-arm64": "0.25.5", + "@esbuild/android-x64": "0.25.5", + "@esbuild/darwin-arm64": "0.25.5", + "@esbuild/darwin-x64": "0.25.5", + "@esbuild/freebsd-arm64": "0.25.5", + "@esbuild/freebsd-x64": "0.25.5", + "@esbuild/linux-arm": "0.25.5", + "@esbuild/linux-arm64": "0.25.5", + "@esbuild/linux-ia32": "0.25.5", + "@esbuild/linux-loong64": "0.25.5", + "@esbuild/linux-mips64el": "0.25.5", + "@esbuild/linux-ppc64": "0.25.5", + "@esbuild/linux-riscv64": "0.25.5", + "@esbuild/linux-s390x": "0.25.5", + "@esbuild/linux-x64": "0.25.5", + "@esbuild/netbsd-arm64": "0.25.5", + "@esbuild/netbsd-x64": "0.25.5", + "@esbuild/openbsd-arm64": "0.25.5", + "@esbuild/openbsd-x64": "0.25.5", + "@esbuild/sunos-x64": "0.25.5", + "@esbuild/win32-arm64": "0.25.5", + "@esbuild/win32-ia32": "0.25.5", + "@esbuild/win32-x64": "0.25.5" + } + }, "node_modules/escalade": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.2.0.tgz", diff --git a/package.json b/package.json index eba0e30..be92a2f 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,9 @@ "version": "3.0.0", "description": "", "scripts": { - "build": "tsc --project tsconfig.json", - "push:main": "npm run build && screeps-api --server main upload dist/*", - "push:sim": "npm run build && screeps-api --server main upload --branch sim dist/*", + "build": "node esbuild.config.mjs", + "push:main": "npm run build && screeps-api --server main upload dist/*.js dist/**/*.js", + "push:sim": "npm run build && screeps-api --server main upload --branch sim dist/*.js dist/**/*.js", "format": "prettier --config .prettierrc 'src/**/*.ts' --write && eslint --fix src/" }, "repository": { @@ -34,6 +34,7 @@ "@typescript-eslint/parser": "^8.35.1", "@typescript-eslint/typescript-estree": "^8.35.1", "chai": "^5.2.0", + "esbuild": "^0.25.5", "eslint": "^9.30.1", "eslint-config-prettier": "^10.1.5", "eslint-import-resolver-typescript": "^4.4.4", diff --git a/src/main.ts b/src/main.ts index 3765321..c95849d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,7 @@ import SpawnHandler from "spawnHandler"; declare global { - /* + /* Example types, expand on these or remove them and add your own. Note: Values, properties defined here do no fully *exist* by this type definiton alone. You must also give them an implemention if you would like to use them. (ex. actually setting a `role` property in a Creeps memory) @@ -9,24 +9,25 @@ declare global { Types added in this `global` block are in an ambient, global context. This is needed because `main.ts` is a module file (uses import or export). Interfaces matching on name from @types/screeps will be merged. This is how you can extend the 'built-in' interfaces from @types/screeps. */ - // Memory extension samples - interface Memory { - uuid: number; - log: any; - } - - interface CreepMemory { - role: string; - room: string; - working: boolean; - } - - // Syntax for adding proprties to `global` (ex "global.log") - namespace NodeJS { - interface Global { - log: any; + // Memory extension samples + interface Memory { + uuid: number; + log: any; + spawnHandlers: { [name: string]: SpawnHandler }; + } + + interface CreepMemory { + role: string; + room: string; + working: boolean; + } + + // Syntax for adding proprties to `global` (ex "global.log") + namespace NodeJS { + interface Global { + log: any; + } } - } } // When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change @@ -42,10 +43,18 @@ declare global { // } // }); export const loop = () => { - for (const spawn of Object.values(Game.spawns)) { - // Create a handler for each spawn - const spawnHandler = new SpawnHandler(spawn); - // Run the handler - spawnHandler.run(); - } + Memory.spawnHandlers = Memory.spawnHandlers || {}; + + console.log(`Current game tick is ${Game.time}`); + + // Check if spawn still exists + + for (const spawn of Object.values(Game.spawns)) { + // Create a handler for each spawn + if (spawn.name in Memory.spawnHandlers) { + Memory.spawnHandlers[spawn.name] = new SpawnHandler(spawn); + } + // Run the handler + Memory.spawnHandlers[spawn.name].run(); + } }; diff --git a/src/spawnHandler.ts b/src/spawnHandler.ts index bc727e8..3602720 100644 --- a/src/spawnHandler.ts +++ b/src/spawnHandler.ts @@ -1,9 +1,83 @@ -class SpawnHandler { - constructor(private spawn: StructureSpawn) {} +import { CreepRequisition, CreepRole, CreepRoles, RoleDefinition } from "types/creeps"; +import { DEFAULT_GAME_CONFIG } from "types/gameConfig"; +import { get_role_const as get_role_cost } from "utils/funcs/get_role_const"; - public run(): void { - console.log(`Spawn ${this.spawn.name} in room ${this.spawn.room.name} is ready.`); - } +class SpawnHandler { + constructor(private spawn: StructureSpawn) {} + + public run(): void { + if (this.spawn.spawning) { + console.log(`Spawn ${this.spawn.name} is currently spawning a creep.`); + return; + } + + const creepRequisition = this.checksNeedsCreeps(); + if (Object.values(creepRequisition).every(count => count <= 0)) { + console.log(`Spawn ${this.spawn.name} has no creep needs.`); + return; + } + + const totalCreeps = Object.values(Game.creeps).length; + if (totalCreeps >= DEFAULT_GAME_CONFIG.maxCreeps) { + console.log(`Spawn ${this.spawn.name} cannot spawn more creeps, limit reached.`); + return; + } + + const rolesToSpawn = this.sortCreepRolesByPriority(creepRequisition); + + for (const role of rolesToSpawn) { + if (this.spawn.store[RESOURCE_ENERGY] < get_role_cost(role)) { + console.log(`Spawn ${this.spawn.name} does not have enough energy to spawn a ${role.name}.`); + continue; + } + + const newName = `${role.name}_${Game.time}`; + const spawnResult = this.spawn.spawnCreep(role.body, newName, { + memory: { + role: role.name, + room: this.spawn.room.name, + working: false + } + }); + if (spawnResult === OK) { + console.log(`Spawn ${this.spawn.name} successfully spawned a new ${role.name}: ${newName}.`); + return; // Exit after spawning one creep + } else { + console.error(`Spawn ${this.spawn.name} failed to spawn a new ${role.name}: ${spawnResult}`); + } + } + } + + private checksNeedsCreeps(): CreepRequisition { + const creepCounts: Record = {}; + for (const creep of Object.values(Game.creeps)) { + const role = creep.memory.role; + creepCounts[role] = (creepCounts[role] || 0) + 1; + } + + const requisition: CreepRequisition = { + harvester: 0, + upgrader: 0, + builder: 0 + }; + for (const role in DEFAULT_GAME_CONFIG.minCreepsPerRole) { + if (!(role in CreepRoles)) { + console.warn(`Unknown creep role: ${role}`); + continue; + } + const roleType = role as CreepRole; + requisition[roleType] = DEFAULT_GAME_CONFIG.minCreepsPerRole[roleType] - (creepCounts[role] || 0); + } + + return requisition; + } + + private sortCreepRolesByPriority(requisition: CreepRequisition): RoleDefinition[] { + return Object.keys(requisition) + .filter(role => requisition[role as CreepRole] > 0) + .map(role => CreepRoles[role as CreepRole]) + .sort((a, b) => a.priority - b.priority); + } } export default SpawnHandler; diff --git a/src/types/creeps.ts b/src/types/creeps.ts new file mode 100644 index 0000000..53ec1c7 --- /dev/null +++ b/src/types/creeps.ts @@ -0,0 +1,31 @@ +export type RoleDefinition = { + name: string; + body: BodyPartConstant[]; + priority: number; +} + + +export const CreepRoles = { + harvester: { + name: "harvester", + body: [WORK, CARRY, MOVE], + priority: 1 + }, + upgrader: { + name: "upgrader", + body: [WORK, CARRY, MOVE], + priority: 2 + }, + builder: { + name: "builder", + body: [WORK, CARRY, MOVE], + priority: 3 + } +} satisfies Record; + + +export type CreepRole = keyof typeof CreepRoles; + + + +export type CreepRequisition = Record; diff --git a/src/types/gameConfig.ts b/src/types/gameConfig.ts new file mode 100644 index 0000000..85a8a8e --- /dev/null +++ b/src/types/gameConfig.ts @@ -0,0 +1,34 @@ +import { CreepRequisition } from "./creeps"; + + + +/** + * Configuration for the game, defining limits and minimum requirements for creeps. + * Used to manage the overall game state and ensure proper role distribution. + */ +export type GameConfig = { + /** The maximum number of creeps allowed in the game. */ + maxCreeps: number; + + /** + * The minimum number of creeps required for each role. + * Creeps will be spawned to meet these minimums before allocating new roles. + */ + minCreepsPerRole: CreepRequisition; +}; + + +/** + * Default game configuration with maximum creeps and minimum creeps per role. + * This configuration is used to initialize the game state and ensure that the game runs smoothly. + * + * @type {GameConfig} + */ +export const DEFAULT_GAME_CONFIG: GameConfig = { + maxCreeps: 50, + minCreepsPerRole: { + harvester: 5, + upgrader: 5, + builder: 5 + } +}; diff --git a/src/utils/funcs/get_role_const.ts b/src/utils/funcs/get_role_const.ts new file mode 100644 index 0000000..317dfd4 --- /dev/null +++ b/src/utils/funcs/get_role_const.ts @@ -0,0 +1,8 @@ +import { RoleDefinition } from "types/creeps" + +export const get_role_const = (role: RoleDefinition) => { + return role.body.reduce((cost, part) => { + cost += BODYPART_COST[part] || 0; + return cost; + }, 0) +}