-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameAPI.mjs
More file actions
183 lines (148 loc) · 5.96 KB
/
GameAPI.mjs
File metadata and controls
183 lines (148 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// import { PmoveQuake2Configuration } from '../../shared/Pmove.mjs';
import { cvarFlags } from '../../shared/Defs.mjs';
import { featureFlags, entityClasses as id1EntityClasses, ServerGameAPI as id1ServerGameAPI } from '../id1/GameAPI.mjs';
import EntityRegistry from '../id1/helper/Registry.mjs';
import { HealthItemEntity, HellwaveBackpackEntity } from './entity/Items.mjs';
import HellwavePlayer from './entity/Player.mjs';
import { WallEntity } from './entity/Props.mjs';
import { Superspike } from './entity/Weapons.mjs';
import { BuyZoneEntity, BuyZoneShuttersEntity, MonstersSpawnZoneEntity, PlayersSpawnZoneEntity } from './entity/Zones.mjs';
import GameManager from './GameManager.mjs';
import HellwaveStats from './helper/HellwaveStats.mjs';
/** @typedef {import("../../shared/GameInterfaces").Cvar} Cvar */
/** @typedef {import("../../shared/GameInterfaces").ServerEngineAPI} ServerEngineAPI */
// enable some features that stray from the original vanilla behavior
featureFlags.push(
'draw-bullet-hole-decals',
'correct-ballistic-grenades',
);
const entityClasses = [].concat(id1EntityClasses, [
HellwavePlayer,
HellwaveBackpackEntity,
HealthItemEntity,
WallEntity,
BuyZoneEntity,
BuyZoneShuttersEntity,
MonstersSpawnZoneEntity,
PlayersSpawnZoneEntity,
Superspike,
]);
// export const pmoveConfig = new PmoveQuake2Configuration();
export class ServerGameAPI extends id1ServerGameAPI {
static _entityRegistry = new EntityRegistry(entityClasses);
static _cvars = Object.assign({}, id1ServerGameAPI._cvars, {
rounds: null,
quiettime: null,
normaltime: null,
maxmonstersalive: null,
debug_spawnpoints: null,
});
/**
* Invoked by spawning a server or a changelevel. It will initialize the global game state.
* @param {ServerEngineAPI} engineAPI engine exports
*/
constructor(engineAPI) {
super(engineAPI);
this._serializer.startFields();
this.manager = new GameManager(this);
this._serializer.endFields();
}
_newGameStats() {
return new HellwaveStats(this, this.engine);
}
get rounds() {
return ServerGameAPI._cvars.rounds.value;
}
get quiettime() {
return ServerGameAPI._cvars.quiettime.value;
}
get normaltime() {
return ServerGameAPI._cvars.normaltime.value;
}
get maxmonstersalive() {
return ServerGameAPI._cvars.maxmonstersalive.value;
}
get debug_spawnpoints() {
return ServerGameAPI._cvars.debug_spawnpoints.value;
}
_precacheResources() {
super._precacheResources();
// we almost spawn all entities dynamically, we can force a precache for all of them
for (const entityClass of ServerGameAPI._entityRegistry.getAll()) {
if (entityClass.classname.startsWith('item_key')) {
continue;
}
if (entityClass.classname.startsWith('monster_') ||
entityClass.classname.startsWith('item_') ||
entityClass.classname.startsWith('weapon_')
) {
new entityClass(null, this); // forces a precache
}
}
// some of the precaches are dependent on the entity configuration, so we need to precache them here explicitly
this.engine.PrecacheModel('maps/b_bh100.bsp'); // mega health model
}
startFrame() {
super.startFrame();
this.manager.startFrame();
}
ClientConnect(clientEdict) {
const playerEntity = /** @type {HellwavePlayer} */(clientEdict.entity);
playerEntity.connected();
this.manager.clientConnected(playerEntity);
}
ClientDisconnect(clientEdict) {
const playerEntity = /** @type {HellwavePlayer} */(clientEdict.entity);
playerEntity.disconnected();
this.manager.clientDisconnected(playerEntity);
}
ClientBegin(clientEdict) {
const playerEntity = /** @type {HellwavePlayer} */(clientEdict.entity);
this.manager.clientBeing(playerEntity);
}
/** @param {ServerEngineAPI} ServerEngineAPI engine API for server game code */
static Init(ServerEngineAPI) {
id1ServerGameAPI.Init(ServerEngineAPI);
Object.assign(this._cvars, id1ServerGameAPI._cvars);
this._cvars.rounds = ServerEngineAPI.RegisterCvar('hw_rounds', '12', 0, 'Number of rounds to play in a map. Must be set before the map starts. 0 = infinite rounds.');
this._cvars.quiettime = ServerEngineAPI.RegisterCvar('hw_quiet_time', '90', 0, 'Duration of quiet phase in seconds. During quiet phase players can buy items.');
this._cvars.normaltime = ServerEngineAPI.RegisterCvar('hw_normal_time', '90', 0, 'How many seconds of normal phase before action phase. Set to 0 to disable normal phase.');
this._cvars.maxmonstersalive = ServerEngineAPI.RegisterCvar('hw_monsters_alive', '20', 0, 'Maximum number of monsters alive at a time per player. 0 = no limit.');
this._cvars.debug_spawnpoints = ServerEngineAPI.RegisterCvar('hw_debug_spawnpoints', '0', cvarFlags.CHEAT, 'If set to 1, spawn points will be visualized with debug markers.');
}
init(mapname, serverflags) {
super.init(mapname, serverflags);
// make sure manager is subscribed
this.manager.subscribeToEvents();
// set the round limit
this.manager.round_number_limit = Math.max(1, Math.min(12, ServerGameAPI._cvars.rounds.value));
// configure pmove
// this.engine.SetPmoveConfiguration(pmoveConfig);
}
static GetMapList() {
return [
{ name: 'hw_doom', label: 'Doomed computer station', maxplayers: 4, pictures: [] },
{ name: 'hw_e1m2', label: 'Castle of the damned', maxplayers: 4, pictures: [] },
];
}
static GetStartServerList() {
return [
{ label: 'Castle of the Damned', callback: (engineAPI) => engineAPI.AppendConsoleText(`
hostname "Hellwave: Castle of the Damned"
deathmatch 0
coop 1
samelevel 1
maxplayers 4
map hw_e1m2
`) },
{ label: 'Doomed Computer Station', callback: (engineAPI) => engineAPI.AppendConsoleText(`
hostname "Hellwave: Doomed Computer Station"
deathmatch 0
coop 1
samelevel 1
maxplayers 4
map hw_doom
`) },
];
}
};