-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathia_loop.cs
More file actions
376 lines (315 loc) · 11.4 KB
/
ia_loop.cs
File metadata and controls
376 lines (315 loc) · 11.4 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
using SpacetimeDB;
namespace Benchmarks;
public static partial class ia_loop
{
[SpacetimeDB.Table(Name = "velocity")]
public partial struct Velocity(uint entity_id, float x, float y, float z)
{
[PrimaryKey]
public uint entity_id = entity_id;
public float x = x;
public float y = y;
public float z = z;
}
[SpacetimeDB.Table(Name = "position")]
public partial struct Position(uint entity_id, float x, float y, float z)
{
[PrimaryKey]
public uint entity_id = entity_id;
public float x = x;
public float y = y;
public float z = z;
public float vx = x + 10.0f;
public float vy = y + 20.0f;
public float vz = z + 30.0f;
}
public static ulong MomentMilliseconds()
{
// Idk why Rust uses complicated math, but it seems like it should always return 1.
return 1;
}
[SpacetimeDB.Type]
public enum AgentAction
{
Inactive,
Idle,
Evading,
Investigating,
Retreating,
Fighting,
}
[SpacetimeDB.Table(Name = "game_enemy_ai_agent_state")]
public partial struct GameEnemyAiAgentState(
ulong entity_id,
List<ulong> last_move_timestamps,
ulong next_action_timestamp,
AgentAction action
)
{
[PrimaryKey]
public ulong entity_id = entity_id;
public List<ulong> last_move_timestamps = last_move_timestamps;
public ulong next_action_timestamp = next_action_timestamp;
public AgentAction action = action;
}
[SpacetimeDB.Table(Name = "game_targetable_state")]
public partial struct GameTargetableState(ulong entity_id, long quad)
{
[PrimaryKey]
public ulong entity_id = entity_id;
public long quad = quad;
}
[SpacetimeDB.Table(Name = "game_live_targetable_state")]
public partial struct GameLiveTargetableState(ulong entity_id, long quad)
{
[Unique]
public ulong entity_id = entity_id;
[SpacetimeDB.Index.BTree]
public long quad = quad;
}
[SpacetimeDB.Table(Name = "game_mobile_entity_state")]
public partial struct GameMobileEntityState(
ulong entity_id,
int location_x,
int location_y,
ulong timestamp
)
{
[PrimaryKey]
public ulong entity_id = entity_id;
[SpacetimeDB.Index.BTree]
public int location_x = location_x;
public int location_y = location_y;
public ulong timestamp = timestamp;
}
[SpacetimeDB.Table(Name = "game_enemy_state")]
public partial struct GameEnemyState(ulong entity_id, int herd_id)
{
[PrimaryKey]
public ulong entity_id = entity_id;
public int herd_id = herd_id;
}
[SpacetimeDB.Type]
public partial struct SmallHexTile(int x, int z, uint dimension)
{
public int x = x;
public int z = z;
public uint dimension = dimension;
}
[SpacetimeDB.Table(Name = "game_herd_cache")]
public partial struct GameHerdCache(
int id,
uint dimension_id,
int current_population,
SmallHexTile location,
int max_population,
float spawn_eagerness,
int roaming_distance
)
{
[PrimaryKey]
public int id = id;
public uint dimension_id = dimension_id;
public int current_population = current_population;
public SmallHexTile location = location;
public int max_population = max_population;
public float spawn_eagerness = spawn_eagerness;
public int roaming_distance = roaming_distance;
}
[SpacetimeDB.Reducer]
public static void insert_bulk_position(ReducerContext ctx, uint count)
{
for (uint id = 0; id < count; id++)
{
ctx.Db.position.Insert(new(id, id, id + 5, id * 5));
}
Log.Info($"INSERT POSITION: {count}");
}
[SpacetimeDB.Reducer]
public static void insert_bulk_velocity(ReducerContext ctx, uint count)
{
for (uint id = 0; id < count; id++)
{
ctx.Db.velocity.Insert(new(id, id, id + 5, id * 5));
}
Log.Info($"INSERT VELOCITY: {count}");
}
[SpacetimeDB.Reducer]
public static void update_position_all(ReducerContext ctx, uint expected)
{
uint count = 0;
foreach (Position position in ctx.Db.position.Iter())
{
Position newPosition = position;
newPosition.x += position.vx;
newPosition.y += position.vy;
newPosition.z += position.vz;
ctx.Db.position.entity_id.Update(newPosition);
count++;
}
Log.Info($"UPDATE POSITION ALL: {expected}, processed: {count}");
}
[SpacetimeDB.Reducer]
public static void update_position_with_velocity(ReducerContext ctx, uint expected)
{
uint count = 0;
foreach (Velocity velocity in ctx.Db.velocity.Iter())
{
if (ctx.Db.position.entity_id.Find(velocity.entity_id) is not { } position)
{
continue;
}
position.x += velocity.x;
position.y += velocity.y;
position.z += velocity.z;
ctx.Db.position.entity_id.Update(position);
count++;
}
Log.Info($"UPDATE POSITION BY VELOCITY: {expected}, processed: {count}");
}
[SpacetimeDB.Reducer]
public static void insert_world(ReducerContext ctx, ulong players)
{
for (ulong i = 0; i < players; i++)
{
ulong next_action_timestamp =
(i & 2) == 2 ? MomentMilliseconds() + 2000 : MomentMilliseconds();
ctx.Db.game_enemy_ai_agent_state.Insert(
new(i, [i, 0, i * 2], next_action_timestamp, AgentAction.Idle)
);
ctx.Db.game_live_targetable_state.Insert(new(i, (long)i));
ctx.Db.game_targetable_state.Insert(new(i, (long)i));
ctx.Db.game_mobile_entity_state.Insert(new(i, (int)i, (int)i, next_action_timestamp));
ctx.Db.game_enemy_state.Insert(new(i, (int)i));
ctx.Db.game_herd_cache.Insert(
new(
(int)i,
(uint)i,
(int)(i * 2),
new SmallHexTile((int)i, (int)i, (uint)(i * 2)),
(int)(i * 4),
i,
(int)i
)
);
}
Log.Info($"INSERT WORLD PLAYERS: {players}");
}
public static List<GameTargetableState> GetTargetablesNearQuad(
ReducerContext ctx,
ulong entity_id,
ulong num_players
)
{
List<GameTargetableState> result = new(4);
for (ulong id = entity_id; id < num_players; id++)
{
foreach (
GameLiveTargetableState t in ctx.Db.game_live_targetable_state.quad.Filter((long)id)
)
{
result.Add(
ctx.Db.game_targetable_state.entity_id.Find(t.entity_id)
?? throw new Exception("Identity not found")
);
}
}
return result;
}
private const int MAX_MOVE_TIMESTAMPS = 20;
public static void MoveAgent(
ReducerContext ctx,
ref GameEnemyAiAgentState agent,
SmallHexTile agent_coord,
ulong current_time_ms
)
{
ulong entity_id = agent.entity_id;
GameEnemyState enemy =
ctx.Db.game_enemy_state.entity_id.Find(entity_id)
?? throw new Exception("GameEnemyState Entity ID not found");
ctx.Db.game_enemy_state.entity_id.Update(enemy);
agent.next_action_timestamp = current_time_ms + 2000;
agent.last_move_timestamps.Add(current_time_ms);
if (agent.last_move_timestamps.Count > MAX_MOVE_TIMESTAMPS)
{
agent.last_move_timestamps.RemoveAt(0);
}
GameTargetableState targetable =
ctx.Db.game_targetable_state.entity_id.Find(entity_id)
?? throw new Exception("GameTargetableState Entity ID not found");
int new_hash = targetable.quad.GetHashCode();
targetable.quad = new_hash;
ctx.Db.game_targetable_state.entity_id.Update(targetable);
if (ctx.Db.game_live_targetable_state.entity_id.Find(entity_id) is not null)
{
ctx.Db.game_live_targetable_state.entity_id.Update(new(entity_id, new_hash));
}
GameMobileEntityState mobile_entity =
ctx.Db.game_mobile_entity_state.entity_id.Find(entity_id)
?? throw new Exception("GameMobileEntityState Entity ID not found");
mobile_entity.location_x += 1;
mobile_entity.location_y += 1;
mobile_entity.timestamp = agent.next_action_timestamp;
ctx.Db.game_enemy_ai_agent_state.entity_id.Update(agent);
ctx.Db.game_mobile_entity_state.entity_id.Update(mobile_entity);
}
public static void AgentLoop(
ReducerContext ctx,
GameEnemyAiAgentState agent,
GameTargetableState agent_targetable,
List<GameTargetableState> surrounding_agents,
ulong current_time_ms
)
{
ulong entity_id = agent.entity_id;
GameMobileEntityState? coordinates =
ctx.Db.game_mobile_entity_state.entity_id.Find(entity_id)
?? throw new Exception("GameMobileEntityState Entity ID not found");
GameEnemyState agent_entity =
ctx.Db.game_enemy_state.entity_id.Find(entity_id)
?? throw new Exception("GameEnemyState Entity ID not found");
GameHerdCache agent_herd =
ctx.Db.game_herd_cache.id.Find(agent_entity.herd_id)
?? throw new Exception("GameHerdCache Entity ID not found");
SmallHexTile agent_herd_coordinates = agent_herd.location;
MoveAgent(ctx, ref agent, agent_herd_coordinates, current_time_ms);
}
[SpacetimeDB.Reducer]
public static void game_loop_enemy_ia(ReducerContext ctx, ulong players)
{
uint count = 0;
ulong current_time_ms = MomentMilliseconds();
foreach (GameEnemyAiAgentState agent in ctx.Db.game_enemy_ai_agent_state.Iter())
{
GameTargetableState agent_targetable =
ctx.Db.game_targetable_state.entity_id.Find(agent.entity_id)
?? throw new Exception("No TargetableState for AgentState entity");
List<GameTargetableState> surrounding_agents = GetTargetablesNearQuad(
ctx,
agent_targetable.entity_id,
players
);
GameEnemyAiAgentState newAgent = agent with { action = AgentAction.Fighting };
AgentLoop(ctx, newAgent, agent_targetable, surrounding_agents, current_time_ms);
count++;
}
Log.Info($"ENEMY IA LOOP PLAYERS: {players}, processed: {count}");
}
[SpacetimeDB.Reducer]
public static void init_game_ia_loop(ReducerContext ctx, uint initial_load)
{
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);
}
[SpacetimeDB.Reducer]
public static void run_game_ia_loop(ReducerContext ctx, uint initial_load)
{
Load load = new(initial_load);
game_loop_enemy_ia(ctx, load.num_players);
}
}