2018-02-16 18:11:26 +01:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-12-21 12:35:52 +01:00
|
|
|
#include <stdlib.h>
|
2017-12-17 13:43:41 +01:00
|
|
|
#include <assert.h>
|
2018-01-25 10:45:05 +01:00
|
|
|
|
2017-12-13 14:26:30 +01:00
|
|
|
#include "monster.h"
|
|
|
|
#include "util.h"
|
2017-12-15 08:08:45 +01:00
|
|
|
#include "player.h"
|
|
|
|
#include "monster.h"
|
2017-12-17 13:43:41 +01:00
|
|
|
#include "random.h"
|
2018-01-23 14:11:03 +01:00
|
|
|
#include "gui.h"
|
2018-01-25 10:45:05 +01:00
|
|
|
#include "item.h"
|
|
|
|
#include "item_builder.h"
|
2018-01-29 13:48:44 +01:00
|
|
|
#include "map.h"
|
2018-02-03 23:39:49 +01:00
|
|
|
#include "particle_engine.h"
|
2018-03-03 12:56:53 +01:00
|
|
|
#include "defines.h"
|
2018-05-15 11:16:56 +02:00
|
|
|
#include "update_data.h"
|
|
|
|
#include "actiontextbuilder.h"
|
2018-05-17 21:41:23 +02:00
|
|
|
#include "texturecache.h"
|
2017-12-15 15:03:29 +01:00
|
|
|
|
2018-05-19 17:55:10 +02:00
|
|
|
static void
|
|
|
|
monster_set_sprite_clip_for_current_state(Monster *m)
|
|
|
|
{
|
|
|
|
switch (m->state.current) {
|
|
|
|
case AGRESSIVE:
|
|
|
|
m->stateIndicator.sprite->clip = CLIP16(16 * 11, 16 * 3);
|
|
|
|
break;
|
|
|
|
case SCARED:
|
|
|
|
m->stateIndicator.sprite->clip = CLIP16(16 * 12, 16 * 3);
|
|
|
|
break;
|
2018-05-21 21:03:59 +02:00
|
|
|
case SLEEPING:
|
|
|
|
m->stateIndicator.sprite->clip = CLIP16(16 * 10, 16 * 4);
|
|
|
|
break;
|
|
|
|
case SCANNING:
|
|
|
|
m->stateIndicator.sprite->clip = CLIP16(16 * 13, 16 * 4);
|
2018-05-19 17:55:10 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 13:34:27 +02:00
|
|
|
static void
|
|
|
|
monster_set_state_display_time(Monster *m)
|
|
|
|
{
|
|
|
|
switch (m->state.current) {
|
|
|
|
case SLEEPING:
|
|
|
|
m->stateIndicator.displayCount = -1;
|
|
|
|
break;
|
|
|
|
case PASSIVE:
|
|
|
|
case STATIONARY:
|
|
|
|
case STUNNED:
|
|
|
|
m->stateIndicator.displayCount = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
m->stateIndicator.displayCount = 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-19 17:55:10 +02:00
|
|
|
static void
|
|
|
|
monster_state_change(Monster *m, StateType newState)
|
|
|
|
{
|
|
|
|
if (m->state.current == newState)
|
|
|
|
return;
|
|
|
|
|
2018-08-04 23:52:52 +02:00
|
|
|
m->state.last = m->state.current;
|
2018-05-19 17:55:10 +02:00
|
|
|
m->state.current = newState;
|
|
|
|
m->state.stepsSinceChange = 0;
|
2018-08-09 15:57:05 +02:00
|
|
|
m->state.forceCount = 0;
|
2018-06-28 12:50:32 +02:00
|
|
|
|
2018-08-06 13:34:27 +02:00
|
|
|
monster_set_state_display_time(m);
|
2018-05-19 17:55:10 +02:00
|
|
|
|
2018-08-06 13:34:27 +02:00
|
|
|
if (newState == STUNNED) {
|
2018-08-04 23:52:52 +02:00
|
|
|
m->stats.disadvantage = true;
|
2018-08-06 13:34:27 +02:00
|
|
|
} else if (m->state.last == STUNNED) {
|
2018-08-04 23:52:52 +02:00
|
|
|
m->stats.disadvantage = false;
|
2018-08-06 13:34:27 +02:00
|
|
|
m->sprite->angle = 0;
|
|
|
|
}
|
2018-08-04 23:52:52 +02:00
|
|
|
|
2018-05-19 17:55:10 +02:00
|
|
|
monster_set_sprite_clip_for_current_state(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
monster_behaviour_check_post_hit(Monster *m)
|
|
|
|
{
|
2018-08-09 15:57:05 +02:00
|
|
|
if (m->state.stepsSinceChange < m->state.forceCount)
|
2018-08-04 23:52:52 +02:00
|
|
|
return;
|
2018-08-06 19:38:27 +02:00
|
|
|
|
2018-05-19 17:55:10 +02:00
|
|
|
switch (m->behaviour) {
|
|
|
|
case PACIFIST:
|
|
|
|
case COWARD:
|
|
|
|
monster_state_change(m, SCARED);
|
|
|
|
break;
|
2018-08-06 19:38:27 +02:00
|
|
|
case GUERILLA:
|
2018-08-23 09:11:13 +02:00
|
|
|
case ASSASSIN:
|
2018-08-22 13:13:54 +02:00
|
|
|
case SORCERER:
|
2018-08-14 13:25:18 +02:00
|
|
|
case FIRE_DEMON:
|
2018-08-06 19:38:27 +02:00
|
|
|
break;
|
2018-05-19 17:55:10 +02:00
|
|
|
default:
|
2018-05-21 21:03:59 +02:00
|
|
|
monster_state_change(m, AGRESSIVE);
|
2018-05-19 17:55:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 09:11:13 +02:00
|
|
|
static void
|
|
|
|
damage_surroundings(Monster *m, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
Position roomPos = position_to_matrix_coords(&m->sprite->pos);
|
|
|
|
for (Sint32 i = -1; i <= 1; ++i) {
|
|
|
|
for (Sint32 j = -1; j <= 1; ++j) {
|
|
|
|
if (i == 0 && j == 0)
|
|
|
|
continue;
|
|
|
|
RoomSpace *r = &rm->spaces[roomPos.x + i][roomPos.y + j];
|
|
|
|
if (r->monster) {
|
|
|
|
int dmg = stats_fight(&m->stats, &r->monster->stats);
|
|
|
|
monster_hit(r->monster, dmg);
|
|
|
|
gui_log("%s takes %d damage from the explosion", r->monster->label, dmg);
|
|
|
|
} else if (r->player) {
|
|
|
|
int dmg = stats_fight(&m->stats, &r->player->stats);
|
|
|
|
player_hit(r->player, dmg);
|
|
|
|
gui_log("You take %d damage from the explosion", dmg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sorcerer_blast(Monster *m, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
gui_log("%s creates a magical explosion", m->label);
|
|
|
|
particle_engine_eldritch_explosion(m->sprite->pos, DIM(TILE_DIMENSION, TILE_DIMENSION));
|
|
|
|
|
|
|
|
damage_surroundings(m, rm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
assassin_cloak_effect(Monster *m, bool cloak)
|
|
|
|
{
|
|
|
|
if (cloak)
|
|
|
|
gui_log("%s dissappears from sight", m->label);
|
|
|
|
else
|
|
|
|
gui_log("%s reappears, filled with rage", m->label);
|
|
|
|
particle_engine_fire_explosion(m->sprite->pos, DIM(TILE_DIMENSION, TILE_DIMENSION));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-19 17:55:10 +02:00
|
|
|
static void
|
|
|
|
monster_behaviour_check_post_attack(Monster *m)
|
|
|
|
{
|
|
|
|
switch (m->behaviour) {
|
|
|
|
case GUERILLA:
|
2018-08-22 13:13:54 +02:00
|
|
|
case SORCERER:
|
2018-08-14 13:25:18 +02:00
|
|
|
case FIRE_DEMON:
|
2018-05-19 17:55:10 +02:00
|
|
|
monster_state_change(m, SCARED);
|
|
|
|
break;
|
2018-08-23 09:11:13 +02:00
|
|
|
case ASSASSIN:
|
|
|
|
assassin_cloak_effect(m, true);
|
|
|
|
monster_state_change(m, SCARED);
|
|
|
|
break;
|
2018-05-19 17:55:10 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:03:59 +02:00
|
|
|
static void
|
|
|
|
handle_sentinel_behaviour(Monster *m, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
if (m->state.current == AGRESSIVE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Position monsterPos = m->sprite->pos;
|
|
|
|
if (!position_in_room(&monsterPos, &rm->roomPos))
|
|
|
|
return;
|
|
|
|
|
|
|
|
monsterPos = position_to_matrix_coords(&monsterPos);
|
|
|
|
Position playerPos = rm->playerRoomPos;
|
|
|
|
unsigned int dx = abs(playerPos.x - monsterPos.x);
|
|
|
|
unsigned int dy = abs(playerPos.y - monsterPos.y);
|
|
|
|
if (dx < 3 && dy < 3)
|
|
|
|
monster_state_change(m, AGRESSIVE);
|
|
|
|
else if (dx <= 3 && dy <= 3)
|
|
|
|
monster_state_change(m, SCANNING);
|
|
|
|
else
|
|
|
|
monster_state_change(m, SLEEPING);
|
|
|
|
}
|
|
|
|
|
2018-05-19 17:55:10 +02:00
|
|
|
static void
|
|
|
|
monster_behaviour_check(Monster *m, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
switch (m->behaviour) {
|
|
|
|
case GUERILLA:
|
2018-08-22 13:13:54 +02:00
|
|
|
case SORCERER:
|
2018-08-14 13:25:18 +02:00
|
|
|
case FIRE_DEMON:
|
2018-08-22 14:02:55 +02:00
|
|
|
if (m->state.stepsSinceChange > 5
|
2018-05-19 17:55:10 +02:00
|
|
|
&& m->state.current == SCARED) {
|
|
|
|
monster_state_change(m, AGRESSIVE);
|
|
|
|
}
|
|
|
|
break;
|
2018-08-23 09:11:13 +02:00
|
|
|
case ASSASSIN:
|
|
|
|
if (m->state.stepsSinceChange > 5
|
|
|
|
&& m->state.current == SCARED) {
|
|
|
|
assassin_cloak_effect(m, false);
|
|
|
|
monster_state_change(m, AGRESSIVE);
|
|
|
|
}
|
|
|
|
break;
|
2018-05-21 21:03:59 +02:00
|
|
|
case SENTINEL:
|
|
|
|
handle_sentinel_behaviour(m, rm);
|
|
|
|
break;
|
2018-05-19 17:55:10 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 14:26:30 +01:00
|
|
|
Monster*
|
2018-05-15 11:16:56 +02:00
|
|
|
monster_create(void)
|
2017-12-13 14:26:30 +01:00
|
|
|
{
|
|
|
|
Monster *m = ec_malloc(sizeof(Monster));
|
|
|
|
m->sprite = sprite_create();
|
2018-02-23 23:53:52 +01:00
|
|
|
m->sprite->dim = GAME_DIMENSION;
|
2017-12-14 12:01:05 +01:00
|
|
|
m->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 };
|
2018-08-06 13:34:27 +02:00
|
|
|
m->sprite->rotationPoint = (SDL_Point) { 16, 16 };
|
2018-02-20 14:22:26 +01:00
|
|
|
|
|
|
|
m->stats = (Stats) {
|
|
|
|
12, // Max HP
|
|
|
|
12, // hp
|
|
|
|
2, // dmg
|
|
|
|
0, // atk
|
|
|
|
0, // def
|
|
|
|
1, // speed
|
2018-08-04 23:52:52 +02:00
|
|
|
1, // lvl
|
|
|
|
false, // advantage
|
|
|
|
false // disadvantage
|
2018-02-20 14:22:26 +01:00
|
|
|
};
|
|
|
|
|
2018-01-17 09:32:49 +01:00
|
|
|
m->label = NULL;
|
2018-01-25 10:45:05 +01:00
|
|
|
m->lclabel = NULL;
|
2018-02-19 15:09:04 +01:00
|
|
|
m->steps = 0;
|
2018-05-17 21:41:23 +02:00
|
|
|
m->stateIndicator.sprite = sprite_create();
|
|
|
|
sprite_set_texture(m->stateIndicator.sprite, texturecache_add("GUI/GUI0.png"), 0);
|
|
|
|
sprite_set_texture(m->stateIndicator.sprite, texturecache_add("GUI/GUI1.png"), 1);
|
|
|
|
m->stateIndicator.sprite->dim = GAME_DIMENSION;
|
|
|
|
m->stateIndicator.displayCount = 0;
|
|
|
|
m->stateIndicator.shownOnPlayerRoomEnter = false;
|
2018-08-09 15:57:05 +02:00
|
|
|
m->state.forceCount = 0;
|
2018-08-15 07:19:44 +02:00
|
|
|
m->boss = false;
|
2018-05-19 17:55:10 +02:00
|
|
|
monster_set_behaviour(m, NORMAL);
|
2017-12-15 15:03:29 +01:00
|
|
|
|
2017-12-13 14:26:30 +01:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2017-12-15 15:03:29 +01:00
|
|
|
void
|
|
|
|
monster_update_pos(Monster *m, Position pos)
|
|
|
|
{
|
|
|
|
m->sprite->pos = pos;
|
|
|
|
|
2018-05-17 21:41:23 +02:00
|
|
|
Position indicatorPos = pos;
|
|
|
|
indicatorPos.y -= 32;
|
|
|
|
m->stateIndicator.sprite->pos = indicatorPos;
|
2017-12-15 15:03:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-17 13:43:41 +01:00
|
|
|
static bool
|
2018-05-13 23:32:44 +02:00
|
|
|
has_collided(Monster *monster, RoomMatrix *matrix, Vector2d direction)
|
2017-12-17 13:43:41 +01:00
|
|
|
{
|
|
|
|
if (!position_in_room(&monster->sprite->pos, &matrix->roomPos))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Position roomPos = position_to_matrix_coords(&monster->sprite->pos);
|
|
|
|
RoomSpace *space = &matrix->spaces[roomPos.x][roomPos.y];
|
2017-12-18 09:59:01 +01:00
|
|
|
|
2018-02-05 16:15:45 +01:00
|
|
|
if (space->player && monster->state.current == AGRESSIVE) {
|
2017-12-18 15:26:56 +01:00
|
|
|
unsigned int dmg = stats_fight(&monster->stats,
|
|
|
|
&space->player->stats);
|
2018-01-23 22:54:02 +01:00
|
|
|
|
2017-12-18 15:26:56 +01:00
|
|
|
player_hit(space->player, dmg);
|
2018-01-23 14:11:03 +01:00
|
|
|
|
2018-05-13 23:32:44 +02:00
|
|
|
if (dmg > 0) {
|
|
|
|
gui_log("%s hit you for %u damage",
|
|
|
|
monster->label, dmg);
|
|
|
|
camera_shake(direction, 300);
|
|
|
|
} else {
|
2018-01-25 10:45:05 +01:00
|
|
|
gui_log("%s missed you", monster->label);
|
2018-05-13 23:32:44 +02:00
|
|
|
}
|
2018-05-19 17:55:10 +02:00
|
|
|
monster_behaviour_check_post_attack(monster);
|
2017-12-18 09:59:01 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:11:32 +02:00
|
|
|
return space->occupied || space->lethal || space->trap || space->damaging;
|
2017-12-17 13:43:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2018-05-13 23:32:44 +02:00
|
|
|
move(Monster *m, RoomMatrix *rm, Vector2d direction)
|
2017-12-17 13:43:41 +01:00
|
|
|
{
|
2018-05-13 23:32:44 +02:00
|
|
|
m->sprite->pos.x += TILE_DIMENSION * (int) direction.x;
|
|
|
|
m->sprite->pos.y += TILE_DIMENSION * (int) direction.y;
|
|
|
|
if (has_collided(m, rm, direction)) {
|
|
|
|
m->sprite->pos.x -= TILE_DIMENSION * (int) direction.x;
|
|
|
|
m->sprite->pos.y -= TILE_DIMENSION * (int) direction.y;
|
|
|
|
return false;
|
2017-12-17 13:43:41 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-18 09:59:01 +01:00
|
|
|
static void
|
|
|
|
monster_drunk_walk(Monster *m, RoomMatrix *rm)
|
2017-12-17 13:43:41 +01:00
|
|
|
{
|
|
|
|
unsigned int i, maxMoveAttempts = 6;
|
|
|
|
|
2017-12-18 12:12:24 +01:00
|
|
|
if (get_random(5) >= 2)
|
2017-12-17 13:43:41 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < maxMoveAttempts; ++i) {
|
2018-05-13 23:32:44 +02:00
|
|
|
int moveDir = get_random(3);
|
|
|
|
if (moveDir == 0 && move(m, rm, VECTOR2D_LEFT))
|
2017-12-17 13:43:41 +01:00
|
|
|
break;
|
2018-05-13 23:32:44 +02:00
|
|
|
else if (moveDir == 1 && move(m, rm, VECTOR2D_RIGHT))
|
2017-12-17 13:43:41 +01:00
|
|
|
break;
|
2018-05-13 23:32:44 +02:00
|
|
|
else if (moveDir == 2 && move(m, rm, VECTOR2D_UP))
|
2017-12-17 13:43:41 +01:00
|
|
|
break;
|
2018-05-13 23:32:44 +02:00
|
|
|
else if (moveDir == 3 && move(m, rm, VECTOR2D_DOWN))
|
2017-12-17 13:43:41 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-12-18 09:59:01 +01:00
|
|
|
}
|
|
|
|
|
2018-07-31 21:08:36 +02:00
|
|
|
static Direction
|
|
|
|
get_optimal_move_towards(Monster *m, RoomMatrix *rm, const Position *dest)
|
2017-12-18 09:59:01 +01:00
|
|
|
{
|
|
|
|
int x_dist, y_dist;
|
|
|
|
Position mPos;
|
|
|
|
|
|
|
|
mPos = position_to_matrix_coords(&m->sprite->pos);
|
|
|
|
|
2018-03-03 12:56:53 +01:00
|
|
|
unsigned int currentScore = 100;
|
|
|
|
unsigned int chosenDirection = UP;
|
2017-12-18 09:59:01 +01:00
|
|
|
|
2018-03-03 12:56:53 +01:00
|
|
|
for (unsigned int i = 0; i < 4; ++i) {
|
|
|
|
Position next = mPos;
|
|
|
|
unsigned int nextScore = 0;
|
2017-12-18 09:59:01 +01:00
|
|
|
|
2018-03-03 12:56:53 +01:00
|
|
|
switch (i) {
|
2018-07-31 21:08:36 +02:00
|
|
|
case UP:
|
|
|
|
next.y -= 1;
|
|
|
|
break;
|
|
|
|
case DOWN:
|
|
|
|
next.y += 1;
|
|
|
|
break;
|
|
|
|
case LEFT:
|
|
|
|
next.x -= 1;
|
|
|
|
break;
|
|
|
|
case RIGHT:
|
|
|
|
next.x += 1;
|
|
|
|
break;
|
2018-03-03 12:56:53 +01:00
|
|
|
}
|
|
|
|
|
2018-07-31 21:08:36 +02:00
|
|
|
if (position_equals(&next, dest)) {
|
2018-03-03 12:56:53 +01:00
|
|
|
chosenDirection = (Direction) i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!position_in_roommatrix(&next))
|
|
|
|
continue;
|
|
|
|
|
2018-07-31 21:08:36 +02:00
|
|
|
x_dist = abs(next.x - dest->x);
|
|
|
|
y_dist = abs(next.y - dest->y);
|
2018-03-03 12:56:53 +01:00
|
|
|
|
2018-08-06 00:28:23 +02:00
|
|
|
if (rm->spaces[next.x][next.y].occupied
|
|
|
|
|| rm->spaces[next.x][next.y].lethal
|
|
|
|
|| rm->spaces[next.x][next.y].trap) {
|
2018-03-03 12:56:53 +01:00
|
|
|
nextScore += 50;
|
|
|
|
}
|
|
|
|
|
2018-03-03 13:33:14 +01:00
|
|
|
nextScore += x_dist > y_dist ? x_dist : y_dist;
|
2018-03-03 12:56:53 +01:00
|
|
|
if (nextScore < currentScore) {
|
|
|
|
currentScore = nextScore;
|
2018-07-31 21:08:36 +02:00
|
|
|
chosenDirection = (Direction) i;
|
2018-03-03 12:56:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 21:08:36 +02:00
|
|
|
return chosenDirection;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
monster_agressive_walk(Monster *m, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
unsigned int chosenDirection = get_optimal_move_towards(m, rm, &rm->playerRoomPos);
|
|
|
|
|
2018-03-03 12:56:53 +01:00
|
|
|
switch (chosenDirection) {
|
2018-07-31 21:08:36 +02:00
|
|
|
case UP:
|
|
|
|
move(m, rm, VECTOR2D_UP);
|
|
|
|
break;
|
|
|
|
case DOWN:
|
|
|
|
move(m, rm, VECTOR2D_DOWN);
|
|
|
|
break;
|
|
|
|
case LEFT:
|
|
|
|
move(m, rm, VECTOR2D_LEFT);
|
|
|
|
break;
|
|
|
|
case RIGHT:
|
|
|
|
move(m, rm, VECTOR2D_RIGHT);
|
|
|
|
break;
|
2017-12-18 09:59:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
monster_coward_walk(Monster *m, RoomMatrix *rm)
|
|
|
|
{
|
|
|
|
int x_dist, y_dist;
|
|
|
|
Position mPos;
|
|
|
|
|
|
|
|
mPos = position_to_matrix_coords(&m->sprite->pos);
|
|
|
|
|
|
|
|
x_dist = mPos.x - rm->playerRoomPos.x;
|
|
|
|
y_dist = mPos.y - rm->playerRoomPos.y;
|
|
|
|
|
|
|
|
if (abs(x_dist) > abs(y_dist)) {
|
|
|
|
if (x_dist > 0)
|
2018-05-13 23:32:44 +02:00
|
|
|
move(m, rm, VECTOR2D_RIGHT);
|
2017-12-18 09:59:01 +01:00
|
|
|
else
|
2018-05-13 23:32:44 +02:00
|
|
|
move(m, rm, VECTOR2D_LEFT);
|
2017-12-18 09:59:01 +01:00
|
|
|
} else {
|
|
|
|
if (y_dist > 0)
|
2018-05-13 23:32:44 +02:00
|
|
|
move(m, rm, VECTOR2D_DOWN);
|
2017-12-18 09:59:01 +01:00
|
|
|
else
|
2018-05-13 23:32:44 +02:00
|
|
|
move(m, rm, VECTOR2D_UP);
|
2017-12-18 09:59:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-19 15:09:04 +01:00
|
|
|
bool
|
2018-08-13 13:11:32 +02:00
|
|
|
monster_move(Monster *m, RoomMatrix *rm, Map *map)
|
2017-12-18 09:59:01 +01:00
|
|
|
{
|
2018-08-09 15:57:05 +02:00
|
|
|
if (m->state.forceCount) {
|
|
|
|
if (m->state.stepsSinceChange >= m->state.forceCount) {
|
2018-08-04 23:52:52 +02:00
|
|
|
monster_state_change(m, m->state.last);
|
|
|
|
monster_behaviour_check_post_hit(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 15:57:05 +02:00
|
|
|
if (m->state.current == STUNNED) {
|
|
|
|
m->state.stepsSinceChange += 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:03:59 +02:00
|
|
|
monster_behaviour_check(m, rm);
|
2018-08-13 13:11:32 +02:00
|
|
|
Position origPos = m->sprite->pos;
|
|
|
|
Position originalMPos =
|
2018-08-08 14:46:59 +02:00
|
|
|
position_to_matrix_coords(&m->sprite->pos);
|
2018-08-22 13:13:54 +02:00
|
|
|
|
|
|
|
if (m->state.current == AGRESSIVE && m->behaviour == SORCERER) {
|
|
|
|
if (position_proximity(1, &originalMPos, &rm->playerRoomPos)) {
|
|
|
|
sorcerer_blast(m, rm);
|
|
|
|
monster_behaviour_check_post_attack(m);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 13:11:32 +02:00
|
|
|
rm->spaces[originalMPos.x][originalMPos.y].occupied = false;
|
|
|
|
rm->spaces[originalMPos.x][originalMPos.y].monster = NULL;
|
2017-12-18 09:59:01 +01:00
|
|
|
|
2017-12-18 12:12:24 +01:00
|
|
|
switch (m->state.current) {
|
2017-12-18 09:59:01 +01:00
|
|
|
case PASSIVE:
|
|
|
|
monster_drunk_walk(m, rm);
|
|
|
|
break;
|
|
|
|
case AGRESSIVE:
|
|
|
|
monster_agressive_walk(m, rm);
|
|
|
|
break;
|
2017-12-18 12:12:24 +01:00
|
|
|
case SCARED:
|
2017-12-18 09:59:01 +01:00
|
|
|
monster_coward_walk(m, rm);
|
|
|
|
break;
|
2018-05-19 17:55:10 +02:00
|
|
|
case STATIONARY:
|
2018-05-21 21:03:59 +02:00
|
|
|
case SLEEPING:
|
|
|
|
case SCANNING:
|
2018-05-19 17:55:10 +02:00
|
|
|
default:
|
|
|
|
break;
|
2017-12-18 09:59:01 +01:00
|
|
|
};
|
2017-12-17 13:43:41 +01:00
|
|
|
|
|
|
|
monster_update_pos(m, m->sprite->pos);
|
|
|
|
|
2018-08-08 14:46:59 +02:00
|
|
|
Position newPos = position_to_matrix_coords(&m->sprite->pos);
|
|
|
|
rm->spaces[newPos.x][newPos.y].occupied = true;
|
|
|
|
rm->spaces[newPos.x][newPos.y].monster = m;
|
|
|
|
|
2018-08-13 13:11:32 +02:00
|
|
|
if (!position_equals(&originalMPos, &newPos)) {
|
2018-08-08 14:46:59 +02:00
|
|
|
Player *p = rm->spaces[rm->playerRoomPos.x][rm->playerRoomPos.y].player;
|
|
|
|
if (p) {
|
|
|
|
Uint32 range = 3 + player_has_artifact(p, IMPROVED_HEARING) * 2;
|
|
|
|
bool withinHearingDist =
|
|
|
|
range > 3 && position_proximity(range,
|
|
|
|
&newPos,
|
|
|
|
&rm->playerRoomPos);
|
|
|
|
|
|
|
|
RoomSpace *space = &rm->spaces[newPos.x][newPos.y];
|
|
|
|
if (space->light < 100 && withinHearingDist) {
|
2018-08-22 20:54:39 +02:00
|
|
|
Position alertPos = m->sprite->pos;
|
|
|
|
alertPos.x += TILE_DIMENSION >> 1;
|
|
|
|
alertPos.y += TILE_DIMENSION >> 1;
|
|
|
|
actiontextbuilder_create_text("!", C_WHITE, &alertPos);
|
2018-08-08 14:46:59 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-13 13:11:32 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-22 13:13:54 +02:00
|
|
|
if (!position_equals(&origPos, &m->sprite->pos)) {
|
|
|
|
if (rm->modifier->type == RMOD_TYPE_FIRE || m->behaviour == FIRE_DEMON) {
|
|
|
|
Object *o = object_create_fire();
|
|
|
|
o->sprite->pos = origPos;
|
|
|
|
o->damage *= m->stats.lvl;
|
|
|
|
linkedlist_push(&map->objects, o);
|
|
|
|
}
|
|
|
|
if (m->behaviour == SORCERER) {
|
|
|
|
Object *o = object_create_green_gas();
|
|
|
|
o->sprite->pos = origPos;
|
|
|
|
o->damage *= m->stats.lvl;
|
|
|
|
linkedlist_push(&map->objects, o);
|
|
|
|
}
|
2018-08-08 14:46:59 +02:00
|
|
|
}
|
2018-02-19 15:09:04 +01:00
|
|
|
|
|
|
|
m->steps++;
|
|
|
|
if (m->steps >= m->stats.speed) {
|
2018-05-17 21:41:23 +02:00
|
|
|
if (m->stateIndicator.displayCount > 0)
|
|
|
|
m->stateIndicator.displayCount -= 1;
|
2018-05-19 17:55:10 +02:00
|
|
|
m->state.stepsSinceChange += 1;
|
2018-02-19 15:09:04 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-15 11:16:56 +02:00
|
|
|
|
2018-02-19 15:09:04 +01:00
|
|
|
return false;
|
2017-12-17 13:43:41 +01:00
|
|
|
}
|
|
|
|
|
2018-08-22 20:54:39 +02:00
|
|
|
void
|
|
|
|
monster_reset_steps(Monster *m)
|
|
|
|
{
|
|
|
|
m->steps = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-15 11:16:56 +02:00
|
|
|
void
|
|
|
|
monster_update(Monster *m, UpdateData *data)
|
|
|
|
{
|
2018-05-17 21:41:23 +02:00
|
|
|
Position monsterRoomPos = position_to_room_coords(&m->sprite->pos);
|
2018-08-06 13:34:27 +02:00
|
|
|
if (position_equals(&data->matrix->roomPos, &monsterRoomPos)
|
|
|
|
&& !m->stateIndicator.shownOnPlayerRoomEnter)
|
|
|
|
{
|
2018-05-17 21:41:23 +02:00
|
|
|
m->stateIndicator.shownOnPlayerRoomEnter = true;
|
2018-08-06 13:34:27 +02:00
|
|
|
monster_set_state_display_time(m);
|
2018-05-17 21:41:23 +02:00
|
|
|
} else {
|
|
|
|
m->stateIndicator.shownOnPlayerRoomEnter = false;
|
|
|
|
}
|
2018-08-06 13:34:27 +02:00
|
|
|
|
|
|
|
if (m->state.current == STUNNED) {
|
|
|
|
Sprite *s = m->sprite;
|
|
|
|
if (s->angle > 30)
|
|
|
|
s->angle = -1;
|
|
|
|
else if (s->angle < -30)
|
|
|
|
s->angle = 0;
|
|
|
|
|
|
|
|
if (s->angle >= 0)
|
|
|
|
s->angle += 2;
|
|
|
|
else
|
|
|
|
s->angle -= 2;
|
|
|
|
}
|
2018-05-17 21:41:23 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 12:12:24 +01:00
|
|
|
void
|
|
|
|
monster_hit(Monster *monster, unsigned int dmg)
|
|
|
|
{
|
|
|
|
if (dmg > 0) {
|
2018-02-03 23:39:49 +01:00
|
|
|
Position p = monster->sprite->pos;
|
|
|
|
p.x += 8;
|
|
|
|
p.y += 8;
|
|
|
|
Dimension d = { 8, 8 };
|
2018-02-09 13:55:57 +01:00
|
|
|
particle_engine_bloodspray(p, d, dmg);
|
2018-05-15 14:12:38 +02:00
|
|
|
char msg[10];
|
|
|
|
m_sprintf(msg, 10, "-%d", dmg);
|
2018-05-15 11:16:56 +02:00
|
|
|
actiontextbuilder_create_text(msg,
|
2018-05-15 23:21:28 +02:00
|
|
|
C_RED,
|
2018-05-15 11:16:56 +02:00
|
|
|
&monster->sprite->pos);
|
2017-12-18 12:12:24 +01:00
|
|
|
} else {
|
2018-05-15 11:16:56 +02:00
|
|
|
actiontextbuilder_create_text("Dodged",
|
2018-05-15 23:21:28 +02:00
|
|
|
C_YELLOW,
|
2018-05-15 11:16:56 +02:00
|
|
|
&monster->sprite->pos);
|
2017-12-18 12:12:24 +01:00
|
|
|
}
|
2018-05-19 17:55:10 +02:00
|
|
|
monster_behaviour_check_post_hit(monster);
|
2017-12-18 12:12:24 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 15:16:14 +01:00
|
|
|
void
|
|
|
|
monster_update_stats_for_level(Monster *m, unsigned int level)
|
|
|
|
{
|
|
|
|
m->stats.lvl = level;
|
|
|
|
m->stats.maxhp *= level;
|
|
|
|
m->stats.hp = m->stats.maxhp;
|
|
|
|
m->stats.dmg *= level;
|
|
|
|
m->stats.atk *= level;
|
|
|
|
m->stats.def *= level;
|
|
|
|
}
|
|
|
|
|
2018-01-29 13:48:44 +01:00
|
|
|
void
|
2018-02-24 21:15:13 +01:00
|
|
|
monster_drop_loot(Monster *monster, Map *map, Player *player)
|
2018-01-25 10:45:05 +01:00
|
|
|
{
|
2018-02-15 14:45:20 +01:00
|
|
|
static unsigned int treasure_drop_chance = 1;
|
2018-02-24 21:15:13 +01:00
|
|
|
unsigned int item_drop_chance = 1;
|
2018-08-08 00:14:24 +02:00
|
|
|
|
2018-01-29 13:48:44 +01:00
|
|
|
Item *item;
|
2018-03-13 16:13:54 +01:00
|
|
|
Item *items[3];
|
2018-01-30 13:44:21 +01:00
|
|
|
unsigned int item_count = 0;
|
2018-03-02 17:07:15 +01:00
|
|
|
bool player_full_health = player->stats.hp >= player->stats.maxhp;
|
2018-02-24 21:15:13 +01:00
|
|
|
|
2018-08-15 07:19:44 +02:00
|
|
|
if (monster->boss) {
|
|
|
|
Artifact *a = artifact_create_random(player, 2);
|
|
|
|
a->sprite->pos = monster->sprite->pos;
|
|
|
|
linkedlist_append(&map->artifacts, a);
|
|
|
|
Item *treasure = item_builder_build_item(TREASURE, map->level*2);
|
|
|
|
treasure->sprite->pos = monster->sprite->pos;
|
|
|
|
linkedlist_append(&map->items, treasure);
|
|
|
|
}
|
|
|
|
|
2018-08-22 13:13:54 +02:00
|
|
|
if (monster->stats.lvl > 2 && get_random(29) == 0) {
|
2018-08-10 13:00:23 +02:00
|
|
|
Artifact *a = artifact_create_random(player, 1);
|
|
|
|
a->sprite->pos = monster->sprite->pos;
|
|
|
|
linkedlist_append(&map->artifacts, a);
|
|
|
|
}
|
|
|
|
|
2018-02-24 21:15:13 +01:00
|
|
|
if (player->stats.hp < player->stats.maxhp / 2)
|
|
|
|
item_drop_chance = 0;
|
2018-01-29 13:48:44 +01:00
|
|
|
|
2018-02-15 14:45:20 +01:00
|
|
|
if (get_random(treasure_drop_chance) == 0) {
|
2018-01-31 09:15:33 +01:00
|
|
|
item = item_builder_build_item(TREASURE, map->level);
|
2018-01-29 13:48:44 +01:00
|
|
|
item->sprite->pos = monster->sprite->pos;
|
2018-01-30 13:44:21 +01:00
|
|
|
items[item_count++] = item;
|
2018-01-29 13:48:44 +01:00
|
|
|
}
|
2018-02-15 14:45:20 +01:00
|
|
|
if (get_random(item_drop_chance) == 0) {
|
2018-02-24 21:15:13 +01:00
|
|
|
ItemKey key;
|
2018-03-13 16:13:54 +01:00
|
|
|
key = get_random(DAGGER);
|
2018-02-24 21:15:13 +01:00
|
|
|
item = item_builder_build_item(key, map->level);
|
2018-01-29 13:48:44 +01:00
|
|
|
item->sprite->pos = monster->sprite->pos;
|
2018-01-30 13:44:21 +01:00
|
|
|
items[item_count++] = item;
|
2018-01-29 13:48:44 +01:00
|
|
|
}
|
2018-03-13 16:13:54 +01:00
|
|
|
if (!player_full_health && get_random(2) == 0) {
|
|
|
|
item = item_builder_build_item(FLESH, map->level);
|
|
|
|
item->sprite->pos = monster->sprite->pos;
|
|
|
|
items[item_count++] = item;
|
|
|
|
}
|
2018-01-25 10:45:05 +01:00
|
|
|
|
2018-01-30 13:44:21 +01:00
|
|
|
if (item_count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gui_log("%s dropped something", monster->label);
|
|
|
|
|
|
|
|
if (item_count == 1) {
|
|
|
|
linkedlist_append(&map->items, items[0]);
|
|
|
|
} else {
|
|
|
|
Item *container = item_builder_build_sack();
|
|
|
|
container->sprite->pos = monster->sprite->pos;
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < item_count; ++i) {
|
|
|
|
linkedlist_append(&container->items, items[i]);
|
|
|
|
}
|
|
|
|
linkedlist_append(&map->items, container);
|
|
|
|
}
|
2018-01-25 10:45:05 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
void
|
|
|
|
monster_render(Monster *m, Camera *cam)
|
|
|
|
{
|
2018-05-17 23:14:42 +02:00
|
|
|
if (m->stats.hp <= 0)
|
|
|
|
return;
|
|
|
|
|
2018-08-23 09:11:13 +02:00
|
|
|
if (m->behaviour == ASSASSIN && m->state.current != AGRESSIVE)
|
|
|
|
return;
|
|
|
|
|
2017-12-13 20:31:04 +01:00
|
|
|
sprite_render(m->sprite, cam);
|
2018-08-11 15:15:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-08-23 09:41:10 +02:00
|
|
|
monster_render_top_layer(Monster *m, RoomMatrix *rm, Camera *cam)
|
2018-08-11 15:15:53 +02:00
|
|
|
{
|
2018-08-13 13:11:32 +02:00
|
|
|
if (m->stats.hp <= 0)
|
|
|
|
return;
|
|
|
|
|
2018-08-23 09:11:13 +02:00
|
|
|
if (m->behaviour == ASSASSIN && m->state.current != AGRESSIVE)
|
|
|
|
return;
|
|
|
|
|
2018-08-23 09:41:10 +02:00
|
|
|
Position mPos = position_to_matrix_coords(&m->sprite->pos);
|
|
|
|
mPos.y -= 1;
|
|
|
|
if (rm->spaces[mPos.x][mPos.y].player) {
|
|
|
|
sprite_set_alpha(m->stateIndicator.sprite, 110);
|
|
|
|
}
|
2018-06-28 12:50:32 +02:00
|
|
|
if (m->stateIndicator.displayCount != 0)
|
2018-05-17 21:41:23 +02:00
|
|
|
sprite_render(m->stateIndicator.sprite, cam);
|
2018-08-23 09:41:10 +02:00
|
|
|
if (rm->spaces[mPos.x][mPos.y].player) {
|
|
|
|
sprite_set_alpha(m->stateIndicator.sprite, 255);
|
|
|
|
}
|
2018-05-17 21:41:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-05-19 17:55:10 +02:00
|
|
|
monster_set_behaviour(Monster *m, MonsterBehaviour behaviour)
|
2018-05-17 21:41:23 +02:00
|
|
|
{
|
2018-05-19 17:55:10 +02:00
|
|
|
m->behaviour = behaviour;
|
|
|
|
switch (behaviour) {
|
|
|
|
case HOSTILE:
|
|
|
|
case GUERILLA:
|
2018-08-23 09:11:13 +02:00
|
|
|
case ASSASSIN:
|
2018-08-22 13:13:54 +02:00
|
|
|
case SORCERER:
|
2018-05-19 17:55:10 +02:00
|
|
|
case COWARD:
|
2018-08-14 13:25:18 +02:00
|
|
|
case FIRE_DEMON:
|
2018-05-19 17:55:10 +02:00
|
|
|
m->state.current = AGRESSIVE;
|
|
|
|
break;
|
2018-05-21 21:03:59 +02:00
|
|
|
case SENTINEL:
|
|
|
|
m->state.current = SLEEPING;
|
|
|
|
break;
|
2018-05-19 17:55:10 +02:00
|
|
|
case PACIFIST:
|
|
|
|
case NORMAL:
|
|
|
|
default:
|
|
|
|
m->state.current = PASSIVE;
|
|
|
|
break;
|
|
|
|
}
|
2018-05-17 21:41:23 +02:00
|
|
|
monster_set_sprite_clip_for_current_state(m);
|
2017-12-13 20:31:04 +01:00
|
|
|
}
|
|
|
|
|
2018-08-04 23:52:52 +02:00
|
|
|
void
|
2018-08-09 15:57:05 +02:00
|
|
|
monster_set_state(Monster *m, StateType state, Uint8 forceCount)
|
2018-08-04 23:52:52 +02:00
|
|
|
{
|
2018-08-09 15:31:27 +02:00
|
|
|
monster_state_change(m, state);
|
2018-08-09 15:57:05 +02:00
|
|
|
m->state.forceCount = forceCount;
|
2018-08-09 15:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
monster_push(Monster *m, RoomMatrix *rm, Vector2d dir)
|
|
|
|
{
|
|
|
|
move(m, rm, dir);
|
2018-08-04 23:52:52 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 14:26:30 +01:00
|
|
|
void
|
|
|
|
monster_destroy(Monster *m)
|
|
|
|
{
|
|
|
|
sprite_destroy(m->sprite);
|
2018-01-17 09:32:49 +01:00
|
|
|
if (m->label)
|
|
|
|
free(m->label);
|
2018-01-25 10:45:05 +01:00
|
|
|
if (m->lclabel)
|
|
|
|
free(m->lclabel);
|
2018-05-17 21:41:23 +02:00
|
|
|
sprite_destroy(m->stateIndicator.sprite);
|
2017-12-13 14:26:30 +01:00
|
|
|
free(m);
|
|
|
|
}
|