Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "character.h"
#include "heap.h"
#include "util.h"
static int32_t character_turn_cmp(const void *key, const void *with) {
if (((character_t *) key)->next_turn == ((character_t *) with)->next_turn) {
return ((character_t *) key)->seq_num - ((character_t *) with)->seq_num;
} else {
return ((character_t *) key)->next_turn - ((character_t *) with)->next_turn;
}
}
char characteristics_symbols[] = "0123456789abcdef";
character_t *generate_monster(uint8_t x, uint8_t y) {
static int next_seq = 1;
uint32_t characteristics = rand() & 0x0000000f;
character_t *m = malloc(sizeof(character_t));
m->monster = malloc(sizeof(monster_t));
m->player = NULL;
m->monster->characteristics = characteristics;
// Initialize the starting "last seen" position to the position of the monster
// This will keep smart monsters from moving until they actually see the player
m->monster->last_seen.x = x;
m->monster->last_seen.y = y;
m->pos.x = x;
m->pos.y = y;
m->speed = randrange(5, 20);
m->seq_num = next_seq++;
m->next_turn = 0;
m->alive = 1;
m->symbol = characteristics_symbols[characteristics];
return m;
}
character_t *generate_pc(uint8_t x, uint8_t y) {
character_t *pc = malloc(sizeof(character_t));
pc->player = malloc(sizeof(pc_t));
pc->monster = NULL;
pc->player->target.x = x;
pc->player->target.y = y;
pc->pos.x = x;
pc->pos.y = y;
pc->speed = 10;
pc->seq_num = 0;
pc->next_turn = 0;
pc->alive = 1;
pc->symbol = '@';
return pc;
}
void init_character_turn_heap(heap_t *h) {
heap_init(h, character_turn_cmp, NULL);
}