-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
211 lines (176 loc) · 4.79 KB
/
main.c
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
/*
============================================================
Depth-first Search Algorithm - NES Proof-of-concept
Copyright 2018 - 2021 Ninja Dynamics - See license below
============================================================
Creative Commons - Attribution 3.0 Unported
https://creativecommons.org/licenses/by/3.0/legalcode
You are free to:
------------------------------------------------------------
Share - copy and redistribute the material in any
medium or format.
Adapt - remix, transform, and build upon the material
for any purpose, even commercially.
Under the following terms:
------------------------------------------------------------
Attribution - You must give appropriate credit,
provide a link to the license, and indicate if
changes were made. You may do so in any reasonable
manner, but not in any way that suggests the licensor
endorses you or your use.
No additional restrictions — You may not apply legal
terms or technological measures that legally restrict
others from doing anything the license permits.
============================================================
*/
#include "neslib.h"
#include "vrambuf.h"
//#link "vrambuf.c"
#include "dfs.h"
//#link "dfs.c"
#include "cursor.h"
//#link "cursor.c"
//#link "chr_generic.s"
/*{pal:"nes",layout:"nes"}*/
const char PALETTE[32] = {
0x21, // screen color
0x17,0x28,0x30,0x0, // background palette 0
0x1c,0x20,0x2c,0x0, // background palette 1
0x00,0x10,0x20,0x0, // background palette 2
0x06,0x16,0x26,0x0, // background palette 3
0x16,0x35,0x24,0x0, // sprite palette 0
0x00,0x37,0x25,0x0, // sprite palette 1
0x0d,0x2d,0x3a,0x0, // sprite palette 2
0x0d,0x27,0x2a // sprite palette 3
};
static uint8_t sprid;
static uint16_t wp_i, wp;
static uint8_t x, y, sprite;
static uint8_t px, py;
static int8_t vx, vy;
static uint8_t sx, sy, dx, dy;
static byte pad;
static uint8_t framecount;
void put_msg(char *msg, int8_t size) {
vrambuf_put(NTADR_A(3, 2), msg, size);
ppu_wait_nmi();
}
void draw_map(void) {
for (y = 0; y < 30; ++y) {
for (x = 0; x < 32; ++x) {
vram_adr(NTADR_A(x, y));
vram_write(&area[y][x], 1);
}
}
}
void draw_path(void) {
if (!wp) {
put_msg("No solution", 11);
}
else {
vrambuf_clear();
}
for (wp_i = 0; wp_i < wp; ++wp_i) {
x = waypointX[wp_i];
y = waypointY[wp_i];
vram_adr(NTADR_A(x, y));
vram_write("+", 1);
}
}
void main(void) {
sx = 0;
sy = 0;
dx = 0;
dy = 0;
// clear vram buffer
vrambuf_clear();
// set NMI handler
set_vram_update(updbuf);
// Set the colors
pal_all(PALETTE);
// Draw area
draw_map();
// Cursor
cursor_init(TILE_MODE, 0x10);
cursor.state = ON;
// Enable PPU rendering (turn on screen)
ppu_on_all();
// infinite loop
while (1) {
//oam_clear();
sprid = 0;
if (cursor.state == ON) {
pad = pad_trigger(0);
if (pad & PAD_A) {
if ((!sx && !sy) || (dx && dy)) {
wp = 0;
sx = cursor.mx;
sy = cursor.my;
dx = NULL;
dy = NULL;
ppu_off();
draw_map();
ppu_on_all();
}
else if ((sx && sy) && (!dx && !dy)) {
put_msg("Calculating", 11);
// - - - - -
px = sx * 8;
py = sy * 8;
// - - - - -
dx = cursor.mx;
dy = cursor.my;
wp = solve(sx, sy, dx, dy);
ppu_off();
vrambuf_clear();
draw_map();
draw_path();
ppu_on_all();
sprite = 0x18;
wp_i = 0;
}
}
if (pad & PAD_B) {
wp = 0;
sx = NULL;
sy = NULL;
dx = NULL;
dy = NULL;
ppu_off();
draw_map();
ppu_on_all();
}
cursor_move();
sprid = oam_spr(cursor.x, cursor.y - 1, cursor.sprite, 0, sprid);
}
if (wp) {
x = waypointX[wp_i];
y = waypointY[wp_i];
vx = 0;
if (px != x * 8) vx = (px > x * 8) ? -1 : 1;
vy = 0;
if (py != y * 8) vy = (py > y * 8) ? -1 : 1;
px += vx;
py += vy;
//sprid = oam_spr(x*8, y*8-1, 0x18, 0, sprid);
sprid = oam_spr(px, py-1, sprite, 0, sprid);
if (++framecount == 16) {
framecount = 0;
if (++sprite > 0x18 + 2) {
sprite = 0x18;
}
}
if (px == x * 8 && py == y * 8) {
if (++wp_i >= wp) {wp_i = 0; px = sx*8; py = sy*8; }
}
}
if (sx && sy) {
sprid = oam_spr(sx*8, sy*8 - 1, 'S', 3, sprid);
}
if (dx && dy) {
sprid = oam_spr(dx*8, dy*8 - 1, 'F', 3, sprid);
}
ppu_wait_nmi();
oam_clear();
}
}