-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathia_loop.rs
More file actions
436 lines (378 loc) · 11.5 KB
/
ia_loop.rs
File metadata and controls
436 lines (378 loc) · 11.5 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! STDB module used for benchmarks based on "realistic" workloads we are focusing in improving.
#![allow(clippy::too_many_arguments, unused_variables)]
use crate::Load;
use spacetimedb::{log, ReducerContext, SpacetimeType, Table};
use std::hash::{Hash, Hasher};
#[spacetimedb::table(name = velocity)]
pub struct Velocity {
#[primary_key]
pub entity_id: u32,
pub x: f32,
pub y: f32,
pub z: f32,
}
impl Velocity {
pub fn new(entity_id: u32, x: f32, y: f32, z: f32) -> Self {
Self { entity_id, x, y, z }
}
}
#[spacetimedb::table(name = position)]
pub struct Position {
#[primary_key]
pub entity_id: u32,
pub x: f32,
pub y: f32,
pub z: f32,
pub vx: f32,
pub vy: f32,
pub vz: f32,
}
impl Position {
pub fn new(entity_id: u32, x: f32, y: f32, z: f32) -> Self {
Self {
entity_id,
x,
y,
z,
vx: x + 10.0,
vy: y + 20.0,
vz: z + 30.0,
}
}
}
pub fn moment_milliseconds() -> u64 {
1
// Duration::from_micros(1000).as_millis() as u64
// or previously...
// Timestamp::from_micros_since_unix_epoch(1000)
// .duration_since(Timestamp::UNIX_EPOCH)
// .ok()
// .unwrap()
// .as_millis() as u64
}
#[derive(SpacetimeType, Debug, Clone, Copy)]
pub enum AgentAction {
Inactive,
Idle,
Evading,
Investigating,
Retreating,
Fighting,
}
#[spacetimedb::table(name = game_enemy_ai_agent_state)]
#[derive(Clone)]
pub struct GameEnemyAiAgentState {
#[primary_key]
pub entity_id: u64,
pub last_move_timestamps: Vec<u64>,
pub next_action_timestamp: u64,
pub action: AgentAction,
}
#[spacetimedb::table(name = game_targetable_state)]
#[derive(Clone)]
pub struct GameTargetableState {
#[primary_key]
pub entity_id: u64,
pub quad: i64,
}
#[spacetimedb::table(name = game_live_targetable_state)]
pub struct GameLiveTargetableState {
#[unique]
pub entity_id: u64,
#[index(btree)]
pub quad: i64,
}
#[spacetimedb::table(name = game_mobile_entity_state)]
pub struct GameMobileEntityState {
#[primary_key]
pub entity_id: u64,
#[index(btree)]
pub location_x: i32,
pub location_y: i32,
pub timestamp: u64,
}
#[spacetimedb::table(name = game_enemy_state)]
#[derive(Clone)]
pub struct GameEnemyState {
#[primary_key]
pub entity_id: u64,
pub herd_id: i32,
}
#[derive(SpacetimeType, Default, Copy, Clone, Debug, PartialEq)]
pub struct SmallHexTile {
pub x: i32,
pub z: i32,
pub dimension: u32,
}
#[spacetimedb::table(name = game_herd_cache)]
#[derive(Clone, Debug)]
pub struct GameHerdCache {
#[primary_key]
pub id: i32,
pub dimension_id: u32,
pub current_population: i32,
pub location: SmallHexTile,
pub max_population: i32,
pub spawn_eagerness: f32,
pub roaming_distance: i32,
}
fn calculate_hash<T: Hash>(t: &T) -> u64 {
let mut s = std::collections::hash_map::DefaultHasher::new();
t.hash(&mut s);
s.finish()
}
// ---------- insert bulk ----------
#[spacetimedb::reducer]
pub fn insert_bulk_position(ctx: &ReducerContext, count: u32) {
for id in 0..count {
ctx.db
.position()
.insert(Position::new(id, id as f32, (id + 5) as f32, (id * 5) as f32));
}
log::info!("INSERT POSITION: {count}");
}
#[spacetimedb::reducer]
pub fn insert_bulk_velocity(ctx: &ReducerContext, count: u32) {
for id in 0..count {
ctx.db
.velocity()
.insert(Velocity::new(id, id as f32, (id + 5) as f32, (id * 5) as f32));
}
log::info!("INSERT VELOCITY: {count}");
}
// Simulate
// ```
// UPDATE Position SET
// x = x + vx,
// y = y + vy,
// z = z + vz;
// ```
#[spacetimedb::reducer]
pub fn update_position_all(ctx: &ReducerContext, expected: u32) {
let mut count = 0;
for mut position in ctx.db.position().iter() {
position.x += position.vx;
position.y += position.vy;
position.z += position.vz;
let id = position.entity_id;
ctx.db.position().entity_id().update(position);
count += 1;
}
log::info!("UPDATE POSITION ALL: {expected}, processed: {count}");
}
// Simulate
// ```
// UPDATE Position
// SET
// x = Position.x + Velocity.x,
// y = Position.y + Velocity.y,
// z = Position.z + Velocity.z
// FROM Velocity
// WHERE Position.entity_id = Velocity.entity_id;
// ```
#[spacetimedb::reducer]
pub fn update_position_with_velocity(ctx: &ReducerContext, expected: u32) {
let mut count = 0;
for velocity in ctx.db.velocity().iter() {
let Some(mut position) = ctx.db.position().entity_id().find(velocity.entity_id) else {
continue;
};
position.x += velocity.x;
position.y += velocity.y;
position.z += velocity.z;
let id = position.entity_id;
ctx.db.position().entity_id().update(position);
count += 1;
}
log::info!("UPDATE POSITION BY VELOCITY: {expected}, processed: {count}");
}
// Simulations for a game loop
#[spacetimedb::reducer]
pub fn insert_world(ctx: &ReducerContext, players: u64) {
for (i, id) in (0..players).enumerate() {
let next_action_timestamp = if i & 2 == 2 {
moment_milliseconds() + 2000 // Check every 2secs
} else {
moment_milliseconds()
};
ctx.db.game_enemy_ai_agent_state().insert(GameEnemyAiAgentState {
entity_id: id,
next_action_timestamp,
last_move_timestamps: vec![id, 0, id * 2],
action: AgentAction::Idle,
});
ctx.db.game_live_targetable_state().insert(GameLiveTargetableState {
entity_id: id,
quad: id as i64,
});
ctx.db.game_targetable_state().insert(GameTargetableState {
entity_id: id,
quad: id as i64,
});
ctx.db.game_mobile_entity_state().insert(GameMobileEntityState {
entity_id: id,
location_x: id as i32,
location_y: id as i32,
timestamp: next_action_timestamp,
});
ctx.db.game_enemy_state().insert(GameEnemyState {
entity_id: id,
herd_id: id as i32,
});
ctx.db.game_herd_cache().insert(GameHerdCache {
id: id as i32,
dimension_id: id as u32,
current_population: id as i32 * 2,
max_population: id as i32 * 4,
spawn_eagerness: id as f32,
roaming_distance: id as i32,
location: SmallHexTile {
x: id as i32,
z: id as i32,
dimension: id as u32 * 2,
},
});
}
log::info!("INSERT WORLD PLAYERS: {players}");
}
fn get_targetables_near_quad(ctx: &ReducerContext, entity_id: u64, num_players: u64) -> Vec<GameTargetableState> {
let mut result = Vec::with_capacity(4);
for id in entity_id..num_players {
for t in ctx.db.game_live_targetable_state().quad().filter(&(id as i64)) {
result.push(
ctx.db
.game_targetable_state()
.entity_id()
.find(t.entity_id)
.expect("Identity not found"),
)
}
}
result
}
const MAX_MOVE_TIMESTAMPS: usize = 20;
fn move_agent(
ctx: &ReducerContext,
agent: &mut GameEnemyAiAgentState,
agent_coord: SmallHexTile,
current_time_ms: u64,
) {
let entity_id = agent.entity_id;
let enemy = ctx
.db
.game_enemy_state()
.entity_id()
.find(entity_id)
.expect("GameEnemyState Entity ID not found")
.clone();
ctx.db.game_enemy_state().entity_id().update(enemy);
agent.next_action_timestamp = current_time_ms + 2000;
// Keep track of the last [MAX_MOVE_TIMESTAMPS] movements
agent.last_move_timestamps.push(current_time_ms);
if agent.last_move_timestamps.len() > MAX_MOVE_TIMESTAMPS {
agent.last_move_timestamps.remove(0);
}
// Update targetable to the destination
let mut targetable = ctx
.db
.game_targetable_state()
.entity_id()
.find(entity_id)
.expect("GameTargetableState Entity ID not found");
let new_hash = calculate_hash(&targetable.quad) as i64;
targetable.quad = new_hash;
ctx.db.game_targetable_state().entity_id().update(targetable);
// If the entity is alive (which it should be),
// also update the `LiveTargetableState` used by `enemy_ai_agent_loop`.
if ctx
.db
.game_live_targetable_state()
.entity_id()
.find(entity_id)
.is_some()
{
ctx.db
.game_live_targetable_state()
.entity_id()
.update(GameLiveTargetableState {
entity_id,
quad: new_hash,
});
}
let mobile_entity = ctx
.db
.game_mobile_entity_state()
.entity_id()
.find(entity_id)
.expect("GameMobileEntityState Entity ID not found");
let mobile_entity = GameMobileEntityState {
entity_id,
location_x: mobile_entity.location_x + 1,
location_y: mobile_entity.location_y + 1,
timestamp: agent.next_action_timestamp,
};
ctx.db.game_enemy_ai_agent_state().entity_id().update(agent.clone());
ctx.db.game_mobile_entity_state().entity_id().update(mobile_entity);
}
fn agent_loop(
ctx: &ReducerContext,
mut agent: GameEnemyAiAgentState,
agent_targetable: GameTargetableState,
surrounding_agents: &[GameTargetableState],
current_time_ms: u64,
) {
let entity_id = agent.entity_id;
let coordinates = ctx
.db
.game_mobile_entity_state()
.entity_id()
.find(entity_id)
.expect("GameMobileEntityState Entity ID not found");
let agent_entity = ctx
.db
.game_enemy_state()
.entity_id()
.find(entity_id)
.expect("GameEnemyState Entity ID not found");
let agent_herd = ctx
.db
.game_herd_cache()
.id()
.find(agent_entity.herd_id)
.expect("GameHerdCache Entity ID not found");
let agent_herd_coordinates = agent_herd.location;
move_agent(ctx, &mut agent, agent_herd_coordinates, current_time_ms);
}
// We check only for a single pass in the game loop.
#[spacetimedb::reducer]
pub fn game_loop_enemy_ia(ctx: &ReducerContext, players: u64) {
let mut count = 0;
let current_time_ms = moment_milliseconds();
for mut agent in ctx.db.game_enemy_ai_agent_state().iter() {
let agent_targetable = ctx
.db
.game_targetable_state()
.entity_id()
.find(agent.entity_id)
.expect("No TargetableState for AgentState entity");
let surrounding_agents = get_targetables_near_quad(ctx, agent_targetable.entity_id, players);
agent.action = AgentAction::Fighting;
agent_loop(ctx, agent, agent_targetable, &surrounding_agents, current_time_ms);
count += 1;
}
log::info!("ENEMY IA LOOP PLAYERS: {players}, processed: {count}");
}
#[spacetimedb::reducer]
pub fn init_game_ia_loop(ctx: &ReducerContext, initial_load: u32) {
let load = Load::new(initial_load);
insert_bulk_position(ctx, load.biggest_table);
insert_bulk_velocity(ctx, load.big_table);
update_position_all(ctx, load.biggest_table);
update_position_with_velocity(ctx, load.big_table);
insert_world(ctx, load.num_players as u64);
}
#[spacetimedb::reducer]
pub fn run_game_ia_loop(ctx: &ReducerContext, initial_load: u32) {
let load = Load::new(initial_load);
game_loop_enemy_ia(ctx, load.num_players as u64);
}