Skip to content

Commit cae06e2

Browse files
committed
damage and player really is JUST the player and not a reference to all entities. :/
1 parent e652f2f commit cae06e2

8 files changed

Lines changed: 155 additions & 102 deletions

File tree

.idea/workspace.xml

Lines changed: 98 additions & 86 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ include_directories(lib/libtcod-1.9.0-x86_64-msvc/include)
99
link_directories(lib/libtcod-1.9.0-x86_64-msvc/)
1010
find_library( LIBTCOD_LIB libtcod )
1111

12-
set(SOURCE_FILES src/main.c
12+
set(SOURCE_FILES src/engine.c
1313
src/input_handlers.c
1414
src/input_handlers.h
1515
src/entity.c

cmake-build-debug/libtcod-tutorial.cbp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
<Unit filename="/cygdrive/e/code/c/libtcod-tutorial/src/data/list.h">
100100
<Option target="libtcod-tutorial"/>
101101
</Unit>
102+
<Unit filename="/cygdrive/e/code/c/libtcod-tutorial/src/engine.c">
103+
<Option target="libtcod-tutorial"/>
104+
</Unit>
102105
<Unit filename="/cygdrive/e/code/c/libtcod-tutorial/src/entity.c">
103106
<Option target="libtcod-tutorial"/>
104107
</Unit>
@@ -111,9 +114,6 @@
111114
<Unit filename="/cygdrive/e/code/c/libtcod-tutorial/src/input_handlers.h">
112115
<Option target="libtcod-tutorial"/>
113116
</Unit>
114-
<Unit filename="/cygdrive/e/code/c/libtcod-tutorial/src/main.c">
115-
<Option target="libtcod-tutorial"/>
116-
</Unit>
117117
<Unit filename="/cygdrive/e/code/c/libtcod-tutorial/src/map/fov.c">
118118
<Option target="libtcod-tutorial"/>
119119
</Unit>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Exception: STATUS_ACCESS_VIOLATION at rip=0010040204D
2+
rax=0000000000000000 rbx=00000000FFFFCA70 rcx=00002BA9E8AE55A1
3+
rdx=0000000000000002 rsi=000000000000001E rdi=0000000000000000
4+
r8 =0000000004CA09F0 r9 =0000000004CA0A24 r10=0000000100000000
5+
r11=00000001004024A7 r12=0000000000000000 r13=0000000000000000
6+
r14=0000000000000000 r15=0000000000000000
7+
rbp=00000000FFFFC840 rsp=00000000FFFFC7C0
8+
program=E:\code\c\libtcod-tutorial\cmake-build-debug\libtcod-tutorial.exe, pid 10652, thread main
9+
cs=0033 ds=002B es=002B fs=0053 gs=002B ss=002B
10+
Stack trace:
11+
Frame Function Args
12+
000FFFFC840 0010040204D (00600003270, 00600000340, 00000000003, 02300000013)
13+
000FFFFCAF0 001004024C4 (0010000001E, 00000000006, 0010000000A, 00100000050)
14+
000FFFFCB90 001004012F9 (00000000020, FF0700010302FF00, 00180049DAA, 00000310000)
15+
000FFFFCCD0 00180049E16 (00000000000, 00000000000, 00000000000, 00000000000)
16+
00000000000 00180047973 (00000000000, 00000000000, 00000000000, 00000000000)
17+
000FFFFFFF0 00180047A24 (00000000000, 00000000000, 00000000000, 00000000000)
18+
End of stack trace

src/components/fighter.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ struct fighter *create_fighter(int hp, int defense, int power) {
1212
f->power = power;
1313
return f;
1414
}
15+
16+
void take_damage(struct fighter *fighter, int amount) {
17+
fighter->hp -= amount;
18+
}
19+
20+
void attack(struct entity *attacker, struct entity* target) {
21+
int damage = attacker->fighter->power - target->fighter->defense;
22+
23+
if (damage > 0) {
24+
take_damage(target->fighter, damage);
25+
printf("%s attack %s for %d hit points.\n", attacker->name, target->name, damage);
26+
}
27+
}
28+
29+

src/components/fighter.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55
#ifndef LIBTCOD_TUTORIAL_FIGHTER_H
66
#define LIBTCOD_TUTORIAL_FIGHTER_H
77

8+
#include <stdio.h>
9+
#include "../entity.h"
10+
811
struct fighter{
912
int max_hp;
1013
int hp;
1114
int defense;
1215
int power;
1316
};
1417

15-
1618
struct fighter* create_fighter(int hp, int defense, int power);
19+
20+
void take_damage(struct fighter *fighter, int amount);
21+
22+
void attack(struct entity *attacker, struct entity* target);
23+
1724
#endif //LIBTCOD_TUTORIAL_FIGHTER_H

src/main.c renamed to src/engine.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ int main() {
2929
colors[LIGHT_GROUND] = TCOD_color_RGB(200, 180, 50);
3030

3131
struct entity_list *entities_list_head;
32-
struct entity_list *player;
32+
struct entity *player;
3333
entities_list_head = (struct entity_list *) malloc(sizeof(struct entity_list));
3434
entities_list_head->next = NULL;
35-
player = entities_list_head;
3635
struct fighter *fighter = create_fighter(30, 2, 5);
37-
player->data = create_entity(screen_width / 2, screen_height / 2, '@', TCOD_white, "Player", true, fighter, NULL);
36+
entities_list_head->data = create_entity(screen_width / 2, screen_height / 2, '@', TCOD_white, "Player", true, fighter, NULL);;
37+
player = entities_list_head->data;
3838
TCOD_console_set_custom_font("terminal16x16_gs_ro.png",
3939
TCOD_FONT_TYPE_GRAYSCALE | TCOD_FONT_LAYOUT_ASCII_INROW,
4040
16,
@@ -43,7 +43,7 @@ int main() {
4343
TCOD_console_t con = TCOD_console_new(screen_width, screen_height);
4444

4545
struct game_map *map = create_game_map(map_width, map_height);
46-
make_map(map, max_rooms, room_min_size, room_max_size, map_width, map_height, player->data, entities_list_head,
46+
make_map(map, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities_list_head,
4747
max_monsters_per_room);
4848

4949
TCOD_key_t *key;
@@ -62,7 +62,7 @@ int main() {
6262
TCOD_sys_check_for_event(TCOD_EVENT_KEY_PRESS, key, mouse);
6363

6464
if (fov_recompute) {
65-
recompute_fov(fov_map, player->data->x, player->data->y, fov_radius, fov_light_walls, fov_algorithm);
65+
recompute_fov(fov_map, player->x, player->y, fov_radius, fov_light_walls, fov_algorithm);
6666
}
6767
render_all(con, entities_list_head, map, fov_map, fov_recompute, screen_width, screen_height, colors);
6868
fov_recompute = false;
@@ -73,20 +73,21 @@ int main() {
7373

7474
handle_keys(key, action);
7575
if (action->type != NO_ACTION) {
76+
7677
#pragma clang diagnostic push
7778
#pragma ide diagnostic ignored "OCDFAInspection"
7879
if (action->type == MOVE && game_state == PLAYERS_TURN) {
7980
int dx = action->data.move.dx;
8081
int dy = action->data.move.dy;
81-
int destination_x = player->data->x + dx;
82-
int destination_y = player->data->y + dy;
82+
int destination_x = player->x + dx;
83+
int destination_y = player->y + dy;
8384
if (!is_blocked(map, destination_x, destination_y)) {
8485
struct entity *target = get_blocking_entities_at_location(entities_list_head, destination_x,
8586
destination_y);
8687
if (target) {
87-
printf("You kick the %s in the shins, much to its annoyance!\n", target->name);
88+
attack(player, target);
8889
} else {
89-
move(player->data, dx, dy);
90+
move(player, dx, dy);
9091
fov_recompute = true;
9192
}
9293
}
@@ -102,7 +103,7 @@ int main() {
102103
struct entity_list *curr = entities_list_head;
103104
while (curr != NULL) {
104105
if (curr->data->ai_action) {
105-
(*curr->data->ai_action)(curr->data, player->data, fov_map, map, entities_list_head);
106+
(*curr->data->ai_action)(curr->data, player, fov_map, map, entities_list_head);
106107
}
107108
curr = curr->next;
108109
}

src/map/game_map.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void basic_ai_monster_turn(struct entity *e, struct entity *target, TCOD_Map *fo
169169
if (distance_to(e, target) >= 2) {
170170
move_astar(e, target, entity_list, map);
171171
} else if (target->fighter && target->fighter->hp > 0) {
172-
printf("The %s insults you! Your ego is damaged!\n", e->name);
172+
attack(e, target);
173173
}
174174
}
175175
}

0 commit comments

Comments
 (0)