Skip to content

Commit

Permalink
finish brain
Browse files Browse the repository at this point in the history
  • Loading branch information
r2dev committed Feb 18, 2021
1 parent 3505099 commit 6d7ca29
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 191 deletions.
5 changes: 0 additions & 5 deletions code/handmade.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,7 @@ Copy(memory_index Size, void* SourceInit, void* DestInit) {

struct controlled_hero {
brain_id BrainID;
v2 ddP;
v2 dSword;
r32 dZ;
r32 RecenterTimer;
b32 Exited;
b32 DebugSpawn;
};

enum game_mode {
Expand Down
32 changes: 31 additions & 1 deletion code/handmade_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,21 @@ enum brain_type {
Brain_Snake,
Brain_Count,
};

struct brain_slot {
u32 Index;
};

struct brain_id {
u32 Value;
};

enum reserved_brain_id {
ReservedBrainID_FirstControl = 1,
ReservedBrainID_LastControl = (ReservedBrainID_FirstControl + MAX_CONTROLLER_COUNT - 1),
ReservedBrainID_FirstFree,
};

struct entity {
entity_id ID;
b32 Updatable;
Expand All @@ -107,6 +115,7 @@ struct entity {

v3 P;
v3 dP;
v3 ddP;

r32 DistanceLimit;

Expand Down Expand Up @@ -141,6 +150,27 @@ struct entity {
entity_traversable_point Traversables[16];
};

struct brain_hero_parts {
entity *Head;
entity *Body;
};

#define BrainSlotFor(Type, Member) BrainSlotFor_((&(((Type *)0)->Member)) - (entity **)0)

inline brain_slot
BrainSlotFor_(u32 PackValue) {
brain_slot Result = {PackValue};
return(Result);
}

struct brain {
brain_type Type;
brain_id ID;
union {
brain_hero_parts Hero;
entity *Array[16];
};
};
inline b32
IsSet(entity* Entity, u32 Flag) {
b32 Result = Entity->Flags & Flag;
Expand Down Expand Up @@ -181,4 +211,4 @@ GetStairGround(entity* Entity, v3 AtGroundPoint) {
v2 Bary = Clamp01(GetBarycentric(RegionRect, AtGroundPoint.xy));
r32 Result = Entity->P.z + Bary.y * Entity->WalkableHeight;
return(Result);
}
}
4 changes: 2 additions & 2 deletions code/handmade_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ extern "C" {
PlatformMouseButton_Extended1,
PlatformMouseButton_Count,
};
#define MAX_CONTROLLER_COUNT 5
struct game_input {
game_controller_input Controllers[5];
game_controller_input Controllers[MAX_CONTROLLER_COUNT];

r32 dtForFrame;

Expand Down
90 changes: 61 additions & 29 deletions code/handmade_sim_region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ GetHashFromID(sim_region* SimRegion, entity_id ID) {
return(Result);
}

internal brain_hash *
GetHashFromID(sim_region* SimRegion, brain_id ID) {
Assert(ID.Value);
brain_hash* Result = 0;
u32 HashValue = ID.Value;
for (u32 Offset = 0; Offset < ArrayCount(SimRegion->BrainHash); ++Offset) {
u32 HashIndex = ((HashValue + Offset) & (ArrayCount(SimRegion->BrainHash) - 1));
brain_hash* Entry = SimRegion->BrainHash + HashIndex;
if (Entry->ID.Value == 0 || Entry->ID.Value == ID.Value) {
Result = Entry;
break;
}
}
return(Result);
}

inline entity *
GetEntityByID(sim_region* SimRegion, entity_id ID) {
entity_hash* Entry = GetHashFromID(SimRegion, ID);
Expand Down Expand Up @@ -94,29 +110,19 @@ EntityOverlapsRectangle(v3 P, entity_collision_volume Volume, rectangle3 Rect) {
return(Result);
}

internal void
AddEntity(sim_region* SimRegion, entity* Source, v3 ChunkDelta) {
entity_id ID = Source->ID;

entity_hash* Entry = GetHashFromID(SimRegion, ID);
Assert(Entry->Ptr == 0);
entity *Dest = 0;
if (SimRegion->EntityCount < SimRegion->MaxEntityCount) {
Dest = SimRegion->Entities + SimRegion->EntityCount++;
Entry->ID = ID;
Entry->Ptr = Dest;
if (Source) {
// decompression
*Dest = *Source;
}
Dest->ID = ID;
Dest->P += ChunkDelta;

Dest->Updatable = EntityOverlapsRectangle(Dest->P, Dest->Collision->TotalVolume, SimRegion->UpdatableBounds);
}
else {
InvalidCodePath
internal brain *
AddOrGetBrain(sim_region *SimRegion, brain_id ID, brain_type Type) {
brain *Result = 0;
brain_hash *BrainHash = GetHashFromID(SimRegion, ID);
Result = BrainHash->Ptr;
if (!Result) {
Assert(SimRegion->BrainCount < SimRegion->MaxBrainCount );
Result = SimRegion->Brains + SimRegion->BrainCount++;
Result->ID = ID;
Result->Type = Type;
BrainHash->Ptr = Result;
}
return(Result);
}

struct test_wall {
Expand All @@ -133,17 +139,15 @@ struct test_wall {
internal void
ConnectEntityPointers(sim_region *SimRegion) {
// TODO(not-set):
#if 0
for(u32 EntityIndex = 0; EntityIndex < SimRegion->EntityCount; ++EntityIndex) {
entity *Entity = SimRegion->Entities + EntityIndex;
LoadEntityReference(SimRegion, &Entity->Head);
//LoadEntityReference(SimRegion, &Entity->Head);
LoadTraversableReference(SimRegion, &Entity->Occupying);
if (Entity->Occupying.Entity.Ptr) {
Entity->Occupying.Entity.Ptr->Traversables[Entity->Occupying.Index].Occupier = Entity;
}
LoadTraversableReference(SimRegion, &Entity->CameFrom);
}
#endif
}

internal sim_region*
Expand All @@ -163,6 +167,10 @@ BeginSim(memory_arena* SimArena, game_mode_world* WorldMode, world* World, world
SimRegion->EntityCount = 0;
SimRegion->Entities = PushArray(SimArena, SimRegion->MaxEntityCount, entity);

SimRegion->MaxBrainCount = 256;
SimRegion->BrainCount = 0;
SimRegion->Brains = PushArray(SimArena, SimRegion->MaxBrainCount, brain);

world_position MinChunkP = MapIntoChunkSpace(World, SimRegion->Origin, GetMinCorner(SimRegion->Bounds));
world_position MaxChunkP = MapIntoChunkSpace(World, SimRegion->Origin, GetMaxCorner(SimRegion->Bounds));
for (s32 ChunkZ = MinChunkP.ChunkZ; ChunkZ <= MaxChunkP.ChunkZ; ++ChunkZ) {
Expand All @@ -176,10 +184,34 @@ BeginSim(memory_arena* SimArena, game_mode_world* WorldMode, world* World, world
world_entity_block* Block = Chunk->FirstBlock;
while (Block) {
for (u32 EntityIndex = 0; EntityIndex < Block->EntityCount; ++EntityIndex) {
entity *Entity = (entity *)Block->EntityData + EntityIndex;
v3 SimSpaceP = Entity->P + ChunkDelta;
if (EntityOverlapsRectangle(SimSpaceP, Entity->Collision->TotalVolume, SimRegion->Bounds)) {
AddEntity(SimRegion, Entity, ChunkDelta);
entity *Source = (entity *)Block->EntityData + EntityIndex;
v3 SimSpaceP = Source->P + ChunkDelta;
if (EntityOverlapsRectangle(SimSpaceP, Source->Collision->TotalVolume, SimRegion->Bounds)) {
entity_id ID = Source->ID;
entity_hash* Entry = GetHashFromID(SimRegion, ID);
Assert(Entry->Ptr == 0);
entity *Dest = 0;
if (SimRegion->EntityCount < SimRegion->MaxEntityCount) {
Dest = SimRegion->Entities + SimRegion->EntityCount++;
Entry->ID = ID;
Entry->Ptr = Dest;
if (Source) {
// decompression
*Dest = *Source;
}
Dest->ID = ID;
Dest->P += ChunkDelta;

Dest->Updatable = EntityOverlapsRectangle(Dest->P, Dest->Collision->TotalVolume, SimRegion->UpdatableBounds);
if (Dest->BrainID.Value != 0) {
brain *Brain = AddOrGetBrain(SimRegion, Dest->BrainID, Dest->BrainType);
Assert(Dest->BrainSlot.Index < ArrayCount(Brain->Array));
Brain->Array[Dest->BrainSlot.Index] = Dest;
}
}
else {
InvalidCodePath
}
}
}
world_entity_block* NextBlock = Block->Next;
Expand Down
5 changes: 0 additions & 5 deletions code/handmade_sim_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ struct entity_hash {
entity_id ID;
};

struct brain {
brain_type Type;
brain_id ID;
};

struct brain_hash {
brain *Ptr;
brain_id ID;
Expand Down
Loading

0 comments on commit 6d7ca29

Please sign in to comment.