summaryrefslogtreecommitdiffstats
path: root/src/char/enemy.c
blob: ff00915b727dcf7dfd6f0b4bc4dfd5dae8338a9f (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright (C) 2021  P. J. McDermott
 *
 * This file is part of Maze Fight
 *
 * Maze Fight 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.
 *
 * Maze Fight 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 Maze Fight.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <SDL.h>
#include <stdlib.h>
#include "../char.h"
#include "../defs.h"
#include "../maze.h"
#include "char.h"

struct mf_enemy {
	struct mf_char   parent;
	int              num_allies;
	struct mf_char **allies;
	int              waiting;
};

static int
_mf_enemy_step(struct mf_char *c)
{
	struct mf_enemy   *e       = (struct mf_enemy *) c;
	enum _mf_char_dir  dirs[4] = {MF_CHAR_DIR_U_, MF_CHAR_DIR_D_,
	                              MF_CHAR_DIR_L_, MF_CHAR_DIR_R_};
	int                i;
	int                j;
	int                t;
	enum _mf_char_dir  back;
	int                dx;
	int                dy;

	/* Shuffle directions */
	for (i = 0; i < 4; ++i) {
		j = rand() / (RAND_MAX / (i+1));
		t       = dirs[i];
		dirs[i] = dirs[j];
		dirs[j] = t;
	}

	/* Don't go backwards unless necessary */
	switch (c->cur_dir) {
		case MF_CHAR_DIR_U_: back = MF_CHAR_DIR_D_; break;
		case MF_CHAR_DIR_D_: back = MF_CHAR_DIR_U_; break;
		case MF_CHAR_DIR_L_: back = MF_CHAR_DIR_R_; break;
		case MF_CHAR_DIR_R_: back = MF_CHAR_DIR_L_; break;
		default:             back = MF_CHAR_DIR_N_; break;
	}
	for (i = 0; i < 3; ++i) {
		if (dirs[i] == back) {
			t       = dirs[i];
			dirs[i] = dirs[3];
			dirs[3] = t;
		}
	}

	for (i = 0; i < 4; ++i) {
		switch (dirs[i]) {
			case MF_CHAR_DIR_U_: dx =  0; dy = -1; break;
			case MF_CHAR_DIR_D_: dx =  0; dy =  1; break;
			case MF_CHAR_DIR_L_: dx = -1; dy =  0; break;
			case MF_CHAR_DIR_R_: dx =  1; dy =  0; break;
			default:             dx =  0; dy =  0; break;
		}
		if (mf_maze_is_wall(c->maze, c->cur_x, c->cur_y, dx, dy)) {
			/* Wall ahead; don't go this direction. */
			continue;
		}
		for (j = 0; j < e->num_allies; ++j) {
			if (e->allies[j] == c) {
				/* Found self */
				continue;
			}
			if (e->allies[j]->cur_x == c->cur_x + dx &&
					e->allies[j]->cur_y == c->cur_y + dy) {
				/* Another enemy currently in this direction */
				goto next_dir;
			}
			if (e->allies[j]->new_x == c->cur_x + dx &&
					e->allies[j]->new_y == c->cur_y + dy) {
				/* Another enemy moving in this direction */
				goto next_dir;
			}
		}
		/* Move */
		c->new_dir = dirs[i];
		e->waiting = SDL_FALSE;
		return 0;
 next_dir:
		continue;
	}

	c->new_dir = MF_CHAR_DIR_N_;
	e->waiting = SDL_TRUE;
	return 0;
}

static int
_mf_enemy_turn(struct mf_char *c)
{
	struct mf_enemy *e = (struct mf_enemy *) c;
	int              dx;
	int              dy;
	int              i;

	switch (c->cur_dir) {
		case MF_CHAR_DIR_U_: dx =  0; dy = -1; break;
		case MF_CHAR_DIR_D_: dx =  0; dy =  1; break;
		case MF_CHAR_DIR_L_: dx = -1; dy =  0; break;
		case MF_CHAR_DIR_R_: dx =  1; dy =  0; break;
		default:             dx =  0; dy =  0; break;
	}

	for (i = 0; i < e->num_allies; ++i) {
		if (e->allies[i] == c) {
			/* Found self */
			continue;
		}
		if (e->allies[i]->cur_x == c->cur_x + dx &&
				e->allies[i]->cur_y == c->cur_y + dy) {
			/* Another enemy currently in this direction */
			return 0;
		}
		if (e->allies[i]->new_x == c->cur_x + dx &&
				e->allies[i]->new_y == c->cur_y + dy) {
			/* Another enemy moving in this direction */
			return 0;
		}
	}

	/* Go straight */
	c->new_dir = c->cur_dir;

	return 0;
}

static int
_mf_enemy_update(struct mf_char *c)
{
	struct mf_enemy *e = (struct mf_enemy *) c;

	if (e->waiting == SDL_TRUE) {
		return _mf_enemy_step(c);
	} else {
		return 0;
	}
}

static int
_mf_enemy_render(struct mf_char *c __attribute__((__unused__)),
		SDL_Renderer *renderer __attribute__((__unused__)))
{
	return 0;
}

static void
_mf_enemy_destroy(struct mf_char *c __attribute__((__unused__)))
{
}

struct mf_char *
mf_enemy_new(struct mf_maze *maze, int cell_width, int maze_size,
		int num_allies, struct mf_char **allies)
{
	struct mf_char  *c;
	struct mf_enemy *e;

	mf_char_init(c, e, enemy);

	c->maze       = maze;
	c->cell_width = cell_width;
	c->speed      = MF_ENEMY_SPEED;
	c->new_dir    = MF_CHAR_DIR_N_;
	c->turning    = 0;
	c->turn_time  = MF_ENEMY_TURN_TIME;
	c->cur_x      = 0;
	c->cur_y      = 0;
	c->travel     = 0;
	c->padding    = MF_ENEMY_P;
	c->smile_y    = MF_ENEMY_SMILE_Y;
	c->smile_r    = MF_ENEMY_SMILE_R;
	c->eye_x      = MF_ENEMY_EYE_X;
	c->eye_y      = MF_ENEMY_EYE_Y;
	c->eye_r      = MF_ENEMY_EYE_R;

	c->head_color.r = MF_COLOR_ELYR_R, c->head_color.g = MF_COLOR_ELYR_G;
	c->head_color.b = MF_COLOR_ELYR_B, c->head_color.a = MF_COLOR_ELYR_A;
	c->smil_color.r = MF_COLOR_ESML_R, c->smil_color.g = MF_COLOR_ESML_G;
	c->smil_color.b = MF_COLOR_ESML_B, c->smil_color.a = MF_COLOR_ESML_A;
	c->eyes_color.r = MF_COLOR_EEYE_R, c->eyes_color.g = MF_COLOR_EEYE_G;
	c->eyes_color.b = MF_COLOR_EEYE_B, c->eyes_color.a = MF_COLOR_EEYE_A;

	while (c->cur_x < maze_size * MF_ENEMY_MIN_DIST &&
			c->cur_y < maze_size * MF_ENEMY_MIN_DIST) {
		mf_enemy_random_jump(c, maze_size);
	}

	c->cur_dir = MF_CHAR_DIR_N_;
	_mf_enemy_step(c);
	c->cur_dir = c->new_dir;

	e->num_allies = num_allies;
	e->allies     = allies;
	e->waiting    = SDL_FALSE;

	return c;
}

void
mf_enemy_random_jump(struct mf_char *c, int maze_size)
{
	c->cur_x = rand() / (RAND_MAX / maze_size);
	c->cur_y = rand() / (RAND_MAX / maze_size);
}