Work on AI

This commit is contained in:
Miloslav Číž 2019-10-22 16:48:27 +02:00
parent 4452396ad2
commit a2a528bf6f
4 changed files with 65 additions and 6 deletions

View File

@ -493,6 +493,27 @@ SFG_PROGRAM_MEMORY uint8_t SFG_monsterSprites[][SFG_TEXTURE_STORE_SIZE] =
5,185,172,16,0,0,0,2,38,136,134,34,80,80,0,0,0,88,167,65,0,0,0,0,182,136,134,98,
80,0,0,0,0,5,185,145,17,17,16,0,34,102,102,102,32,0,0,0,0,0,87,153,52,51,49,0,2,
98,34,102,107,0,0,0,0,0,1,116,71,65,16,0,2,37,82,43,32,0,0,0,0,0,0,17,17,16,0
},
{ // 2, spider walking
175,0,4,5,6,3,223,1,10,7,2,18,62,95,120,63,0,0,17,17,0,0,0,0,0,0,0,1,17,17,0,0,
0,1,50,211,16,0,0,0,0,0,0,21,51,50,16,0,0,19,26,161,49,0,0,0,0,0,17,146,35,33,0,
0,0,29,173,90,33,0,0,0,0,1,148,68,81,16,0,0,0,18,165,218,209,0,16,0,0,25,67,49,
16,0,0,0,0,19,26,161,49,16,17,0,1,148,51,16,0,0,0,0,0,1,61,35,119,113,84,16,25,
68,56,0,0,0,0,0,0,23,81,23,87,83,51,16,17,21,129,0,0,0,0,0,1,50,55,114,130,67,
68,65,0,1,50,16,0,0,0,0,1,37,39,81,68,67,51,50,0,1,85,81,17,17,17,0,1,50,215,17,
204,67,35,35,16,25,153,148,68,83,50,16,0,17,18,68,111,108,50,34,16,1,68,68,68,
82,37,81,0,1,68,67,105,246,98,34,33,0,133,88,136,136,136,129,0,1,23,117,198,150,
102,34,34,238,227,37,181,17,0,0,0,21,135,87,82,102,252,98,46,123,91,50,81,0,0,0,
1,37,24,136,114,38,159,101,235,181,35,181,112,0,0,0,1,37,24,136,114,38,159,101,
235,181,35,181,112,0,0,0,0,21,135,87,82,102,252,98,46,123,83,123,33,0,0,0,0,1,
23,117,198,150,102,34,34,238,235,50,133,17,17,0,0,1,68,67,105,246,98,34,33,0,
131,40,85,85,34,16,0,17,18,68,111,108,50,34,16,1,148,33,17,17,17,0,1,50,215,17,
204,67,35,35,16,25,50,16,0,0,0,0,1,37,39,81,68,67,51,50,17,148,72,136,128,0,0,0,
1,50,55,114,130,67,68,65,20,68,73,148,136,128,0,0,0,23,81,23,87,83,51,16,1,17,
84,68,83,56,129,0,0,1,61,35,119,113,84,16,0,5,21,68,82,34,81,0,0,19,26,161,49,
16,17,0,0,20,66,87,18,37,16,0,0,18,165,218,209,0,16,0,0,1,84,52,65,17,81,17,0,
29,173,90,33,0,0,0,0,0,17,19,52,82,37,16,0,19,26,161,49,0,0,0,0,0,0,1,19,82,81,
0,0,1,50,211,16,0,0,0,0,0,0,0,1,17,16,0,0,0,17,17,0,0,0,0,0,0,0,0,0,0,0,0
}
};

View File

@ -58,13 +58,13 @@
Rate at which AI will be updated, which also affects how fast enemies will
appear.
*/
#define SFG_AI_FPS 2
#define SFG_AI_FPS 4
/**
Says a probability (0 - 255) of the AI changing its state during one update
step.
*/
#define SFG_AI_RANDOM_CHANGE_PROBABILITY 20
#define SFG_AI_RANDOM_CHANGE_PROBABILITY 40
/**
Speed of rocket projectile, in squares per second.

41
main.c
View File

@ -329,6 +329,13 @@ typedef struct
#define SFG_EXPLOSION_DURATION_FRAMES 1
#endif
#define SFG_SPRITE_ANIMATION_FRAME_DURATION \
(SFG_FPS / SFG_SPRITE_ANIMATION_SPEED)
#if SFG_SPRITE_ANIMATION_FRAME_DURATION == 0
#define SFG_SPRITE_ANIMATION_FRAME_DURATION 1
#endif
/*
GLOBAL VARIABLES
===============================================================================
@ -336,6 +343,8 @@ typedef struct
uint8_t SFG_currentRandom;
uint8_t SFG_spriteAnimationFrame;
struct
{
RCL_Camera camera;
@ -1019,6 +1028,8 @@ void SFG_setAndInitLevel(const SFG_Level *level)
SFG_currentLevel.timeStart = SFG_getTimeMs();
SFG_currentLevel.frameStart = SFG_gameFrame;
SFG_spriteAnimationFrame = 0;
SFG_initPlayer();
}
@ -1068,9 +1079,10 @@ void SFG_monsterPerformAI(SFG_MonsterRecord *monster)
if (SFG_random() < SFG_AI_RANDOM_CHANGE_PROBABILITY)
{
// sometimes randomly change state
state = SFG_MONSTER_STATE_IDLE;
state = (SFG_random() % 4 == 0) ?
SFG_MONSTER_STATE_IDLE : SFG_MONSTER_STATE_ATTACKING;
}
if (state == SFG_MONSTER_STATE_IDLE)
else if (state == SFG_MONSTER_STATE_IDLE)
{
switch (SFG_random() % 8)
{
@ -1085,6 +1097,10 @@ void SFG_monsterPerformAI(SFG_MonsterRecord *monster)
default: break;
}
}
else if (state == SFG_MONSTER_STATE_ATTACKING)
{
state = SFG_MONSTER_STATE_IDLE;
}
else
{
if (state == SFG_MONSTER_STATE_GOING_E ||
@ -1323,6 +1339,10 @@ void SFG_gameStep()
for (uint8_t i = 0; i < SFG_KEY_COUNT; ++i)
SFG_keyStates[i] = (SFG_keyStates[i] << 1) | SFG_keyPressed(i);
if ((SFG_currentLevel.frameStart - SFG_gameFrame) %
SFG_SPRITE_ANIMATION_FRAME_DURATION == 0)
SFG_spriteAnimationFrame++;
int8_t recomputeDirection = 0;
RCL_Vector2D moveOffset;
@ -2052,7 +2072,9 @@ void SFG_draw()
{
SFG_MonsterRecord m = SFG_currentLevel.monsterRecords[i];
if (m.stateType != SFG_MONSTER_STATE_INACTIVE)
uint8_t state = m.stateType & SFG_MONSTER_MASK_STATE;
if (state != SFG_MONSTER_STATE_INACTIVE)
{
RCL_Vector2D worldPosition;
@ -2069,12 +2091,23 @@ void SFG_draw()
SFG_player.camera);
if (p.depth > 0)
SFG_drawScaledSprite(SFG_monsterSprites[ (SFG_gameFrame >> 5) & 0x01 ],
{
const uint8_t *s;
if (state == SFG_MONSTER_STATE_IDLE)
s = SFG_monsterSprites[0];
else if (state == SFG_MONSTER_STATE_ATTACKING)
s = SFG_monsterSprites[1];
else
s = SFG_monsterSprites[SFG_spriteAnimationFrame & 0x01 ? 0 : 2];
SFG_drawScaledSprite(s,
p.position.x * SFG_RAYCASTING_SUBSAMPLE,p.position.y,
RCL_perspectiveScale(SFG_GAME_RESOLUTION_Y,p.depth),
p.depth / (RCL_UNITS_PER_SQUARE * 2),p.depth);
}
}
}
// item sprites:
for (uint8_t i = 0; i < SFG_currentLevel.itemRecordCount; ++i)

View File

@ -130,4 +130,9 @@
*/
#define SFG_EXPLOSION_DURATION 70
/**
Specifies how quick some sprite animations are, in frames per second.
*/
#define SFG_SPRITE_ANIMATION_SPEED 4
#endif // guard