~ecs/tbrace

tbrace/update.c -rw-r--r-- 2.6 KiB
f007a19d — Eyal Sawady Makefile: rewrite 10 months ago
                                                                                
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
#include <stdbool.h>
#include <stdio.h>

#include "config.h"
#include "state.h"

static void
ai_update(struct state state, struct car *car)
{
	/* TODO: implement this */
	(void)state, (void)car; /* unused */
}

static bool
player_update(struct state state)
{
	while (1) {
		struct car *player = &state.cars[0];
		char action;
		printf("Laps: %d\n", player->laps);
		printf("Action (? for help): ");
		fflush(stdout);
		fflush(stdin);
		action = getchar();
		switch (action) {
		case 'h':
			player->vel_x -= SPEED;
			break;
		case 'j':
			player->vel_y += SPEED;
			break;
		case 'k':
			player->vel_y -= SPEED;
			break;
		case 'l':
			player->vel_x += SPEED;
			break;
		case 's':
			print_track(state);
			continue;
		case EOF:
			printf("\n");
			return false;
		case 'q':
			return false;
		case '\n':
			/* Wait */
			break;
		default:
			printf("Possible actions:\n"
				"\tTurn left  (h)\n"
				"\tTurn right (l)\n"
				"\tAccelerate (k)\n"
				"\tDecelerate (j)\n"
				"\tShow board (s)\n"
				"\tQuit (q or ^D)\n"
				"\tHelp (default)\n");
			continue;
		}
		return true;
	}
}

static bool
should_stop(struct state state, struct car *car)
{
	int tile = state.track[car->y][car->x];
	int delta_sign = 0;

	if (car->x < 0 || car->x >= state.track_x
			|| car->y < 0 || car->y >= state.track_y) {
		return true;
	}

	switch (tile) {
	case EMPTY:
		return false;
	case LAPLINE_UP:
		delta_sign = -car->vel_y;
		break;
	case LAPLINE_DOWN:
		delta_sign = car->vel_y;
		break;
	case LAPLINE_LEFT:
		delta_sign = -car->vel_x;
		break;
	case LAPLINE_RIGHT:
		delta_sign = car->vel_x;
		break;
	default:
		return true;
	}

	if (delta_sign > 0) {
		car->laps++;
	} else if (delta_sign < 0) {
		car->laps--;
	}

	return false;
}

static void
physics_update(struct state state)
{
	unsigned i;
	for (i = 0; i < state.num_cars; i++) {
		struct car *current = &state.cars[i];

		int spd_x = (current->vel_x > 0)
			? current->vel_x : -current->vel_x;
		int spd_y = (current->vel_y > 0)
			? current->vel_y : -current->vel_y;
		int dx = (current->vel_x > 0) ? 1 : -1;
		int dy = (current->vel_y > 0) ? 1 : -1;

		int i;
		for (i = 0; i < spd_x; i++) {
			current->x += dx;
			if (should_stop(state, current)) {
				current->x -= dx;
				break;
			}
		}

		for (i = 0; i < spd_y; i++) {
			current->y += dy;
			if (should_stop(state, current)) {
				current->y -= dy;
				break;
			}
		}
	}
}

bool
tick(struct state state)
{
	unsigned i;
	for (i = 1; i < state.num_cars; i++) {
		ai_update(state, &state.cars[i]);
	}

	print_track(state);
	if (!player_update(state)) {
		return false;
	}

	physics_update(state);

	return true;
}