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/>.
|
|
|
|
*/
|
|
|
|
|
2018-03-19 10:25:32 +01:00
|
|
|
#include <stdlib.h>
|
2017-12-05 15:03:20 +01:00
|
|
|
#include "position.h"
|
|
|
|
#include "defines.h"
|
|
|
|
|
2018-03-15 16:30:41 +01:00
|
|
|
Position
|
2018-09-10 22:27:26 +02:00
|
|
|
position_to_matrix_coords(const Position *src)
|
2017-12-05 15:03:20 +01:00
|
|
|
{
|
|
|
|
unsigned int room_px_width, room_px_height;
|
|
|
|
Position pos;
|
|
|
|
|
|
|
|
room_px_width = TILE_DIMENSION * MAP_ROOM_WIDTH;
|
|
|
|
room_px_height = TILE_DIMENSION * MAP_ROOM_HEIGHT;
|
|
|
|
|
|
|
|
pos.x = (src->x % room_px_width) / TILE_DIMENSION;
|
|
|
|
pos.y = (src->y % room_px_height) / TILE_DIMENSION;
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
2017-12-06 11:44:17 +01:00
|
|
|
|
2018-03-15 16:30:41 +01:00
|
|
|
Position
|
|
|
|
position_to_room_coords(Position *src)
|
2017-12-06 11:44:17 +01:00
|
|
|
{
|
|
|
|
unsigned int room_px_width, room_px_height;
|
|
|
|
Position pos;
|
|
|
|
|
|
|
|
room_px_width = TILE_DIMENSION * MAP_ROOM_WIDTH;
|
|
|
|
room_px_height = TILE_DIMENSION * MAP_ROOM_HEIGHT;
|
|
|
|
|
|
|
|
pos.x = (src->x - (src->x % room_px_width)) / room_px_width;
|
|
|
|
pos.y = (src->y - (src->y % room_px_height)) / room_px_height;
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
2017-12-13 23:20:54 +01:00
|
|
|
|
2017-12-17 13:43:41 +01:00
|
|
|
bool
|
|
|
|
position_equals(const Position *p1, const Position *p2)
|
|
|
|
{
|
|
|
|
return p1->x == p2->x && p1->y == p2->y;
|
|
|
|
}
|
|
|
|
|
2018-03-19 10:25:32 +01:00
|
|
|
bool
|
|
|
|
position_proximity(unsigned int distance,
|
|
|
|
const Position *p1,
|
|
|
|
const Position *p2)
|
|
|
|
{
|
|
|
|
unsigned int dx, dy;
|
|
|
|
|
|
|
|
dx = abs(p1->x - p2->x);
|
|
|
|
dy = abs(p1->y - p2->y);
|
|
|
|
|
|
|
|
return dx <= distance && dy <= distance;
|
|
|
|
}
|
|
|
|
|
2018-03-15 16:30:41 +01:00
|
|
|
bool
|
|
|
|
position_in_room(Position *pos, Position *roomPos)
|
2017-12-13 23:20:54 +01:00
|
|
|
{
|
2017-12-19 21:00:02 +01:00
|
|
|
int room_px_width, room_px_height, room_x_px, room_y_px;
|
2017-12-13 23:20:54 +01:00
|
|
|
|
|
|
|
room_px_width = TILE_DIMENSION * MAP_ROOM_WIDTH;
|
|
|
|
room_px_height = TILE_DIMENSION * MAP_ROOM_HEIGHT;
|
|
|
|
|
|
|
|
room_x_px = roomPos->x * room_px_width;
|
|
|
|
room_y_px = roomPos->y * room_px_height;
|
|
|
|
|
|
|
|
if (pos->x < room_x_px)
|
|
|
|
return false;
|
2018-08-11 20:16:23 +02:00
|
|
|
else if (pos->x >= room_x_px + room_px_width)
|
2017-12-13 23:20:54 +01:00
|
|
|
return false;
|
|
|
|
else if (pos->y < room_y_px)
|
|
|
|
return false;
|
2018-08-11 20:16:23 +02:00
|
|
|
else if (pos->y >= room_y_px + room_px_height)
|
2017-12-13 23:20:54 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-01 19:37:57 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
position_in_roommatrix(const Position *pos)
|
|
|
|
{
|
2018-08-09 13:58:16 +02:00
|
|
|
return pos->x >= 0
|
|
|
|
&& pos->x < MAP_ROOM_WIDTH
|
|
|
|
&& pos->y >= 0
|
|
|
|
&& pos->y < MAP_ROOM_HEIGHT;
|
2018-03-01 19:37:57 +01:00
|
|
|
}
|
2018-08-11 23:35:16 +02:00
|
|
|
|
|
|
|
Position
|
|
|
|
position_to_tile_pos(const Position *p)
|
|
|
|
{
|
|
|
|
return (Position) {
|
|
|
|
p->x - (p->x % TILE_DIMENSION),
|
|
|
|
p->y - (p->y % TILE_DIMENSION)
|
|
|
|
};
|
|
|
|
}
|
2019-03-04 15:00:35 +01:00
|
|
|
|
|
|
|
Position
|
|
|
|
position_add(const Position *a, const Position *b)
|
|
|
|
{
|
|
|
|
return POS(a->x + b->x, a->y + b->y);
|
|
|
|
}
|