-
Notifications
You must be signed in to change notification settings - Fork 7
/
level_gen.cpp
635 lines (538 loc) · 18.8 KB
/
level_gen.cpp
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include "level_gen.hpp"
namespace madEscape {
using namespace madrona;
using namespace madrona::math;
using namespace madrona::phys;
namespace consts {
inline constexpr float doorWidth = consts::worldWidth / 3.f;
}
enum class RoomType : uint32_t {
SingleButton,
DoubleButton,
CubeBlocking,
CubeButtons,
NumTypes,
};
static inline float randInRangeCentered(Engine &ctx, float range)
{
return ctx.data().rng.sampleUniform() * range - range / 2.f;
}
static inline float randBetween(Engine &ctx, float min, float max)
{
return ctx.data().rng.sampleUniform() * (max - min) + min;
}
// Initialize the basic components needed for physics rigid body entities
static inline void setupRigidBodyEntity(
Engine &ctx,
Entity e,
Vector3 pos,
Quat rot,
SimObject sim_obj,
EntityType entity_type,
ResponseType response_type = ResponseType::Dynamic,
Diag3x3 scale = {1, 1, 1})
{
ObjectID obj_id { (int32_t)sim_obj };
ctx.get<Position>(e) = pos;
ctx.get<Rotation>(e) = rot;
ctx.get<Scale>(e) = scale;
ctx.get<ObjectID>(e) = obj_id;
ctx.get<Velocity>(e) = {
Vector3::zero(),
Vector3::zero(),
};
ctx.get<ResponseType>(e) = response_type;
ctx.get<ExternalForce>(e) = Vector3::zero();
ctx.get<ExternalTorque>(e) = Vector3::zero();
ctx.get<EntityType>(e) = entity_type;
}
// Register the entity with the broadphase system
// This is needed for every entity with all the physics components.
// Not registering an entity will cause a crash because the broadphase
// systems will still execute over entities with the physics components.
static void registerRigidBodyEntity(
Engine &ctx,
Entity e,
SimObject sim_obj)
{
ObjectID obj_id { (int32_t)sim_obj };
ctx.get<broadphase::LeafID>(e) =
RigidBodyPhysicsSystem::registerEntity(ctx, e, obj_id);
}
// Creates floor, outer walls, and agent entities.
// All these entities persist across all episodes.
void createPersistentEntities(Engine &ctx)
{
// Create the floor entity, just a simple static plane.
ctx.data().floorPlane = ctx.makeRenderableEntity<PhysicsEntity>();
setupRigidBodyEntity(
ctx,
ctx.data().floorPlane,
Vector3 { 0, 0, 0 },
Quat { 1, 0, 0, 0 },
SimObject::Plane,
EntityType::None, // Floor plane type should never be queried
ResponseType::Static);
// Create the outer wall entities
// Behind
ctx.data().borders[0] = ctx.makeRenderableEntity<PhysicsEntity>();
setupRigidBodyEntity(
ctx,
ctx.data().borders[0],
Vector3 {
0,
-consts::wallWidth / 2.f,
0,
},
Quat { 1, 0, 0, 0 },
SimObject::Wall,
EntityType::Wall,
ResponseType::Static,
Diag3x3 {
consts::worldWidth + consts::wallWidth * 2,
consts::wallWidth,
2.f,
});
// Right
ctx.data().borders[1] = ctx.makeRenderableEntity<PhysicsEntity>();
setupRigidBodyEntity(
ctx,
ctx.data().borders[1],
Vector3 {
consts::worldWidth / 2.f + consts::wallWidth / 2.f,
consts::worldLength / 2.f,
0,
},
Quat { 1, 0, 0, 0 },
SimObject::Wall,
EntityType::Wall,
ResponseType::Static,
Diag3x3 {
consts::wallWidth,
consts::worldLength,
2.f,
});
// Left
ctx.data().borders[2] = ctx.makeRenderableEntity<PhysicsEntity>();
setupRigidBodyEntity(
ctx,
ctx.data().borders[2],
Vector3 {
-consts::worldWidth / 2.f - consts::wallWidth / 2.f,
consts::worldLength / 2.f,
0,
},
Quat { 1, 0, 0, 0 },
SimObject::Wall,
EntityType::Wall,
ResponseType::Static,
Diag3x3 {
consts::wallWidth,
consts::worldLength,
2.f,
});
// Create agent entities. Note that this leaves a lot of components
// uninitialized, these will be set during world generation, which is
// called for every episode.
for (CountT i = 0; i < consts::numAgents; ++i) {
Entity agent = ctx.data().agents[i] =
ctx.makeRenderableEntity<Agent>();
// Create a render view for the agent
if (ctx.data().enableRender) {
render::RenderingSystem::attachEntityToView(ctx,
agent,
100.f, 0.001f,
1.5f * math::up);
}
ctx.get<Scale>(agent) = Diag3x3 { 1, 1, 1 };
ctx.get<ObjectID>(agent) = ObjectID { (int32_t)SimObject::Agent };
ctx.get<ResponseType>(agent) = ResponseType::Dynamic;
ctx.get<GrabState>(agent).constraintEntity = Entity::none();
ctx.get<EntityType>(agent) = EntityType::Agent;
}
// Populate OtherAgents component, which maintains a reference to the
// other agents in the world for each agent.
for (CountT i = 0; i < consts::numAgents; i++) {
Entity cur_agent = ctx.data().agents[i];
OtherAgents &other_agents = ctx.get<OtherAgents>(cur_agent);
CountT out_idx = 0;
for (CountT j = 0; j < consts::numAgents; j++) {
if (i == j) {
continue;
}
Entity other_agent = ctx.data().agents[j];
other_agents.e[out_idx++] = other_agent;
}
}
}
// Although agents and walls persist between episodes, we still need to
// re-register them with the broadphase system and, in the case of the agents,
// reset their positions.
static void resetPersistentEntities(Engine &ctx)
{
registerRigidBodyEntity(ctx, ctx.data().floorPlane, SimObject::Plane);
for (CountT i = 0; i < 3; i++) {
Entity wall_entity = ctx.data().borders[i];
registerRigidBodyEntity(ctx, wall_entity, SimObject::Wall);
}
for (CountT i = 0; i < consts::numAgents; i++) {
Entity agent_entity = ctx.data().agents[i];
registerRigidBodyEntity(ctx, agent_entity, SimObject::Agent);
// Place the agents near the starting wall
Vector3 pos {
randInRangeCentered(ctx,
consts::worldWidth / 2.f - 2.5f * consts::agentRadius),
randBetween(ctx, consts::agentRadius * 1.1f, 2.f),
0.f,
};
if (i % 2 == 0) {
pos.x += consts::worldWidth / 4.f;
} else {
pos.x -= consts::worldWidth / 4.f;
}
ctx.get<Position>(agent_entity) = pos;
ctx.get<Rotation>(agent_entity) = Quat::angleAxis(
randInRangeCentered(ctx, math::pi / 4.f),
math::up);
auto &grab_state = ctx.get<GrabState>(agent_entity);
if (grab_state.constraintEntity != Entity::none()) {
ctx.destroyEntity(grab_state.constraintEntity);
grab_state.constraintEntity = Entity::none();
}
ctx.get<Progress>(agent_entity).maxY = pos.y;
ctx.get<Velocity>(agent_entity) = {
Vector3::zero(),
Vector3::zero(),
};
ctx.get<ExternalForce>(agent_entity) = Vector3::zero();
ctx.get<ExternalTorque>(agent_entity) = Vector3::zero();
ctx.get<Action>(agent_entity) = Action {
.moveAmount = 0,
.moveAngle = 0,
.rotate = consts::numTurnBuckets / 2,
.grab = 0,
};
ctx.get<StepsRemaining>(agent_entity).t = consts::episodeLen;
}
}
// Builds the two walls & door that block the end of the challenge room
static void makeEndWall(Engine &ctx,
Room &room,
CountT room_idx)
{
float y_pos = consts::roomLength * (room_idx + 1) -
consts::wallWidth / 2.f;
// Quarter door of buffer on both sides, place door and then build walls
// up to the door gap on both sides
float door_center = randBetween(ctx, 0.75f * consts::doorWidth,
consts::worldWidth - 0.75f * consts::doorWidth);
float left_len = door_center - 0.5f * consts::doorWidth;
Entity left_wall = ctx.makeRenderableEntity<PhysicsEntity>();
setupRigidBodyEntity(
ctx,
left_wall,
Vector3 {
(-consts::worldWidth + left_len) / 2.f,
y_pos,
0,
},
Quat { 1, 0, 0, 0 },
SimObject::Wall,
EntityType::Wall,
ResponseType::Static,
Diag3x3 {
left_len,
consts::wallWidth,
1.75f,
});
registerRigidBodyEntity(ctx, left_wall, SimObject::Wall);
float right_len =
consts::worldWidth - door_center - 0.5f * consts::doorWidth;
Entity right_wall = ctx.makeRenderableEntity<PhysicsEntity>();
setupRigidBodyEntity(
ctx,
right_wall,
Vector3 {
(consts::worldWidth - right_len) / 2.f,
y_pos,
0,
},
Quat { 1, 0, 0, 0 },
SimObject::Wall,
EntityType::Wall,
ResponseType::Static,
Diag3x3 {
right_len,
consts::wallWidth,
1.75f,
});
registerRigidBodyEntity(ctx, right_wall, SimObject::Wall);
Entity door = ctx.makeRenderableEntity<DoorEntity>();
setupRigidBodyEntity(
ctx,
door,
Vector3 {
door_center - consts::worldWidth / 2.f,
y_pos,
0,
},
Quat { 1, 0, 0, 0 },
SimObject::Door,
EntityType::Door,
ResponseType::Static,
Diag3x3 {
consts::doorWidth * 0.8f,
consts::wallWidth,
1.75f,
});
registerRigidBodyEntity(ctx, door, SimObject::Door);
ctx.get<OpenState>(door).isOpen = false;
room.walls[0] = left_wall;
room.walls[1] = right_wall;
room.door = door;
}
static Entity makeButton(Engine &ctx,
float button_x,
float button_y)
{
Entity button = ctx.makeRenderableEntity<ButtonEntity>();
ctx.get<Position>(button) = Vector3 {
button_x,
button_y,
0.f,
};
ctx.get<Rotation>(button) = Quat { 1, 0, 0, 0 };
ctx.get<Scale>(button) = Diag3x3 {
consts::buttonWidth,
consts::buttonWidth,
0.2f,
};
ctx.get<ObjectID>(button) = ObjectID { (int32_t)SimObject::Button };
ctx.get<ButtonState>(button).isPressed = false;
ctx.get<EntityType>(button) = EntityType::Button;
return button;
}
static Entity makeCube(Engine &ctx,
float cube_x,
float cube_y,
float scale = 1.f)
{
Entity cube = ctx.makeRenderableEntity<PhysicsEntity>();
setupRigidBodyEntity(
ctx,
cube,
Vector3 {
cube_x,
cube_y,
1.f * scale,
},
Quat { 1, 0, 0, 0 },
SimObject::Cube,
EntityType::Cube,
ResponseType::Dynamic,
Diag3x3 {
scale,
scale,
scale,
});
registerRigidBodyEntity(ctx, cube, SimObject::Cube);
return cube;
}
static void setupDoor(Engine &ctx,
Entity door,
Span<const Entity> buttons,
bool is_persistent)
{
DoorProperties &props = ctx.get<DoorProperties>(door);
for (CountT i = 0; i < buttons.size(); i++) {
props.buttons[i] = buttons[i];
}
props.numButtons = (int32_t)buttons.size();
props.isPersistent = is_persistent;
}
// A room with a single button that needs to be pressed, the door stays open.
static CountT makeSingleButtonRoom(Engine &ctx,
Room &room,
float y_min,
float y_max)
{
float button_x = randInRangeCentered(ctx,
consts::worldWidth / 2.f - consts::buttonWidth);
float button_y = randBetween(ctx, y_min + consts::roomLength / 4.f,
y_max - consts::wallWidth - consts::buttonWidth / 2.f);
Entity button = makeButton(ctx, button_x, button_y);
setupDoor(ctx, room.door, { button }, true);
room.entities[0] = button;
return 1;
}
// A room with two buttons that need to be pressed simultaneously,
// the door stays open.
static CountT makeDoubleButtonRoom(Engine &ctx,
Room &room,
float y_min,
float y_max)
{
float a_x = randBetween(ctx,
-consts::worldWidth / 2.f + consts::buttonWidth,
-consts::buttonWidth);
float a_y = randBetween(ctx,
y_min + consts::roomLength / 4.f,
y_max - consts::wallWidth - consts::buttonWidth / 2.f);
Entity a = makeButton(ctx, a_x, a_y);
float b_x = randBetween(ctx,
consts::buttonWidth,
consts::worldWidth / 2.f - consts::buttonWidth);
float b_y = randBetween(ctx,
y_min + consts::roomLength / 4.f,
y_max - consts::wallWidth - consts::buttonWidth / 2.f);
Entity b = makeButton(ctx, b_x, b_y);
setupDoor(ctx, room.door, { a, b }, true);
room.entities[0] = a;
room.entities[1] = b;
return 2;
}
// This room has 3 cubes blocking the exit door as well as two buttons.
// The agents either need to pull the middle cube out of the way and
// open the door or open the door with the buttons and push the cubes
// into the next room.
static CountT makeCubeBlockingRoom(Engine &ctx,
Room &room,
float y_min,
float y_max)
{
float button_a_x = randBetween(ctx,
-consts::worldWidth / 2.f + consts::buttonWidth,
-consts::buttonWidth - consts::worldWidth / 4.f);
float button_a_y = randBetween(ctx,
y_min + consts::buttonWidth,
y_max - consts::roomLength / 4.f);
Entity button_a = makeButton(ctx, button_a_x, button_a_y);
float button_b_x = randBetween(ctx,
consts::buttonWidth + consts::worldWidth / 4.f,
consts::worldWidth / 2.f - consts::buttonWidth);
float button_b_y = randBetween(ctx,
y_min + consts::buttonWidth,
y_max - consts::roomLength / 4.f);
Entity button_b = makeButton(ctx, button_b_x, button_b_y);
setupDoor(ctx, room.door, { button_a, button_b }, true);
Vector3 door_pos = ctx.get<Position>(room.door);
float cube_a_x = door_pos.x - 3.f;
float cube_a_y = door_pos.y - 2.f;
Entity cube_a = makeCube(ctx, cube_a_x, cube_a_y, 1.5f);
float cube_b_x = door_pos.x;
float cube_b_y = door_pos.y - 2.f;
Entity cube_b = makeCube(ctx, cube_b_x, cube_b_y, 1.5f);
float cube_c_x = door_pos.x + 3.f;
float cube_c_y = door_pos.y - 2.f;
Entity cube_c = makeCube(ctx, cube_c_x, cube_c_y, 1.5f);
room.entities[0] = button_a;
room.entities[1] = button_b;
room.entities[2] = cube_a;
room.entities[3] = cube_b;
room.entities[4] = cube_c;
return 5;
}
// This room has 2 buttons and 2 cubes. The buttons need to remain pressed
// for the door to stay open. To progress, the agents must push at least one
// cube onto one of the buttons, or more optimally, both.
static CountT makeCubeButtonsRoom(Engine &ctx,
Room &room,
float y_min,
float y_max)
{
float button_a_x = randBetween(ctx,
-consts::worldWidth / 2.f + consts::buttonWidth,
-consts::buttonWidth - consts::worldWidth / 4.f);
float button_a_y = randBetween(ctx,
y_min + consts::buttonWidth,
y_max - consts::roomLength / 4.f);
Entity button_a = makeButton(ctx, button_a_x, button_a_y);
float button_b_x = randBetween(ctx,
consts::buttonWidth + consts::worldWidth / 4.f,
consts::worldWidth / 2.f - consts::buttonWidth);
float button_b_y = randBetween(ctx,
y_min + consts::buttonWidth,
y_max - consts::roomLength / 4.f);
Entity button_b = makeButton(ctx, button_b_x, button_b_y);
setupDoor(ctx, room.door, { button_a, button_b }, false);
float cube_a_x = randBetween(ctx,
-consts::worldWidth / 4.f,
-1.5f);
float cube_a_y = randBetween(ctx,
y_min + 2.f,
y_max - consts::wallWidth - 2.f);
Entity cube_a = makeCube(ctx, cube_a_x, cube_a_y, 1.5f);
float cube_b_x = randBetween(ctx,
1.5f,
consts::worldWidth / 4.f);
float cube_b_y = randBetween(ctx,
y_min + 2.f,
y_max - consts::wallWidth - 2.f);
Entity cube_b = makeCube(ctx, cube_b_x, cube_b_y, 1.5f);
room.entities[0] = button_a;
room.entities[1] = button_b;
room.entities[2] = cube_a;
room.entities[3] = cube_b;
return 4;
}
// Make the doors and separator walls at the end of the room
// before delegating to specific code based on room_type.
static void makeRoom(Engine &ctx,
LevelState &level,
CountT room_idx,
RoomType room_type)
{
Room &room = level.rooms[room_idx];
makeEndWall(ctx, room, room_idx);
float room_y_min = room_idx * consts::roomLength;
float room_y_max = (room_idx + 1) * consts::roomLength;
CountT num_room_entities;
switch (room_type) {
case RoomType::SingleButton: {
num_room_entities =
makeSingleButtonRoom(ctx, room, room_y_min, room_y_max);
} break;
case RoomType::DoubleButton: {
num_room_entities =
makeDoubleButtonRoom(ctx, room, room_y_min, room_y_max);
} break;
case RoomType::CubeBlocking: {
num_room_entities =
makeCubeBlockingRoom(ctx, room, room_y_min, room_y_max);
} break;
case RoomType::CubeButtons: {
num_room_entities =
makeCubeButtonsRoom(ctx, room, room_y_min, room_y_max);
} break;
default: MADRONA_UNREACHABLE();
}
// Need to set any extra entities to type none so random uninitialized data
// from prior episodes isn't exported to pytorch as agent observations.
for (CountT i = num_room_entities; i < consts::maxEntitiesPerRoom; i++) {
room.entities[i] = Entity::none();
}
}
static void generateLevel(Engine &ctx)
{
LevelState &level = ctx.singleton<LevelState>();
// For training simplicity, define a fixed sequence of levels.
makeRoom(ctx, level, 0, RoomType::DoubleButton);
makeRoom(ctx, level, 1, RoomType::CubeBlocking);
makeRoom(ctx, level, 2, RoomType::CubeButtons);
#if 0
// An alternative implementation could randomly select the type for each
// room rather than a fixed progression of challenge difficulty
for (CountT i = 0; i < consts::numRooms; i++) {
RoomType room_type = (RoomType)(
ctx.data().rng.sampleI32(0, (uint32_t)RoomType::NumTypes));
makeRoom(ctx, level, i, room_type);
}
#endif
}
// Randomly generate a new world for a training episode
void generateWorld(Engine &ctx)
{
resetPersistentEntities(ctx);
generateLevel(ctx);
}
}