-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbot.c
More file actions
145 lines (122 loc) · 3.13 KB
/
bot.c
File metadata and controls
145 lines (122 loc) · 3.13 KB
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
struct Bot {
struct Vector position;
struct Vector velocity;
unsigned char direction;
unsigned char command;
unsigned char animation;
unsigned char delay;
} bot;
unsigned char func_1_return;
unsigned char func_2_return;
const unsigned char bot_sprit_up = 24;
const unsigned char bot_sprit_down = 4;
const unsigned char bot_sprit_side = 44;
void turn_bot() {
unsigned char ani_offset = 0;
if(bot.animation > 0) {
ani_offset = 1+(bot.animation/4)*4;
}
if(bot.direction == 3) {
// right sprites
set_sprite_tile(0, ani_offset+bot_sprit_side+2);
set_sprite_tile(1, ani_offset+bot_sprit_side);
} else if(bot.direction == 1) {
// left sprites
set_sprite_tile(0, ani_offset+bot_sprit_side);
set_sprite_tile(1, ani_offset+bot_sprit_side+2);
} else if(bot.direction == 2) {
// down sprites
set_sprite_tile(0, ani_offset+bot_sprit_down);
set_sprite_tile(1, ani_offset+bot_sprit_down+2);
} else {
// up sprites
set_sprite_tile(0, ani_offset+bot_sprit_up);
set_sprite_tile(1, ani_offset+bot_sprit_up+2);
}
if(bot.direction == 3) {
set_sprite_prop(0, S_FLIPX);
set_sprite_prop(1, S_FLIPX);
} else {
set_sprite_prop(0, 0);
set_sprite_prop(1, 0);
}
}
void move_bot() {
move_sprite(0, bot.position.x, bot.position.y);
move_sprite(1, bot.position.x+8, bot.position.y);
}
void init_bot() {
bot.command = 0;
bot.animation = 0;
bot.delay = 0;
turn_bot();
move_bot();
}
void command_move() {
set_velocity(bot.direction, &bot.velocity);
walk_sound();
if(tile_at_next(&bot.position, &bot.velocity, 16) < tile_objective_inactive)
bot.animation = 16;
else
bot.delay = 10;
}
void command_turn(char dir) {
if(dir < 0 && bot.direction == 0)
bot.direction = 3;
else if(dir > 0 && bot.direction == 3)
bot.direction = 0;
else
bot.direction = bot.direction + dir;
turn_bot();
bot.delay = 5;
}
unsigned char command_next(unsigned char current) {
current = current + 1;
switch(current) {
case 10: // end of main program
return 42;
case 15: // end of function 1
return func_1_return;
case 20: // end of function 2
return func_2_return;
default:
return current;
}
}
void update_bot() {
if(bot.delay > 0) {
bot.delay--;
} else if(bot.animation > 0) {
bot.animation--;
bot.position.x = bot.position.x + bot.velocity.x;
bot.position.y = bot.position.y + bot.velocity.y;
move_bot();
turn_bot();
if(bot.animation < 1)
bot.delay = 5;
} else if(bot.command < 20) {
switch(program[bot.command]) {
case 1: // Move forward
command_move();
break;
case 2: // Turn Left
command_turn(-1);
break;
case 3: // Turn Right
command_turn(1);
break;
case 4: // Transmit
init_transmission(bot.direction, &bot.position);
break;
case 5: // Function 1
func_1_return = command_next(bot.command);
bot.command = 10;
return;
case 6: // Function 2
func_2_return = command_next(bot.command);
bot.command = 15;
return;
}
bot.command = command_next(bot.command);
}
}