#include "game.h" static const u16 grass = RGB(15, 26, 12); /* One scenery-only cache; the frame buffer still holds actors and UI. */ TEST_INTERNAL u16 background_cache[WIDTH * HEIGHT]; TEST_INTERNAL enum Room background_room; TEST_INTERNAL int background_valid, background_door_open; #ifdef GAME_TEST TEST_INTERNAL unsigned scenery_samples; #endif int in_tall_grass(int x, int y) { return current_room == ROUTE && ((x >= 32 && x < 81 && y >= 53 && y < 87) || (x >= 151 && x < 208 && y >= 112 && y < 133)); } /* Little shaded trees, made from pixels rather than downloaded artwork. */ static u16 tree_pixel(int x, int y, u16 ground) { int dx = x - 9, dy = y - 8; int distance = dx * dx + dy * dy; if (y >= 15 && y < 21 && x >= 7 && x <= 11) ground = x < 9 ? RGB(11, 8, 5) : RGB(17, 12, 6); if (distance < 76) { if (distance > 54) return RGB(3, 10, 7); if (y > 10 || x > 12) return RGB(5, 15, 8); if ((x + y * 2) % 7 < 2) return RGB(13, 24, 10); return RGB(8, 20, 9); } return ground; } /* A cozy room: wooden floor, a bed, bookcase, dresser, plant, and table. */ TEST_INTERNAL u16 cottage_scenery(int x, int y) { u16 color = RGB(3, 7, 10); if (y < 18 || y >= 142) return RGB(29, 30, 25); if (x < 18 || x >= 222 || y < 24) return color; color = RGB(13, 9, 7); if (x >= 24 && x < 216 && y >= 28 && y < 54) { color = y < 43 ? RGB(29, 27, 21) : RGB(16, 21, 18); if (y == 42 || y == 51) color = RGB(10, 13, 12); if (y > 43 && x % 16 == 0) color = RGB(12, 17, 15); } if (x >= 26 && x < 214 && y >= 54 && y < 135) { color = ((y / 8) & 1) ? RGB(24, 18, 12) : RGB(26, 20, 13); if (y % 8 == 0 || (x + (y / 8) * 23) % 47 == 0) color = RGB(17, 12, 9); if (y % 8 == 1) color = RGB(29, 23, 16); if (y % 8 == 5 && (x * 3 + y) % 41 < 8) color = RGB(22, 16, 11); } /* Window and sill above the bed. */ if (x >= 43 && x < 70 && y >= 30 && y < 49) { color = RGB(11, 10, 9); if (x >= 45 && x < 68 && y >= 32 && y < 46) color = RGB(12, 25, 29); if (x >= 46 && x < 54 && y >= 33 && y < 38) color = RGB(25, 31, 30); if (x == 56 || y == 39) color = RGB(27, 25, 19); if (y >= 47) color = RGB(25, 20, 14); } /* A bookcase fixed to the back wall. */ if (x >= 150 && x < 184 && y >= 29 && y < 54) { color = RGB(11, 8, 6); if (x >= 153 && x < 181 && y >= 32 && y < 51) { int book = (x - 153) / 4; color = book % 3 == 0 ? RGB(24, 10, 10) : book % 3 == 1 ? RGB(9, 18, 21) : RGB(25, 21, 12); if (x % 4 == 0) color = RGB(30, 27, 18); } if (y == 41 || y == 52) color = RGB(24, 16, 10); } /* Bed: headboard, pillow, blue blanket, and wooden feet. */ if (x >= 34 && x < 69 && y >= 59 && y < 98) { color = RGB(10, 8, 7); if (x >= 37 && x < 66 && y >= 62 && y < 94) color = RGB(29, 27, 21); if (x >= 40 && x < 63 && y >= 64 && y < 72) color = y == 71 ? RGB(23, 24, 23) : RGB(31, 31, 28); if (x >= 37 && x < 66 && y >= 75 && y < 94) { color = RGB(7, 15, 24); if (x < 41 || x > 62) color = RGB(5, 11, 20); if (y == 76 || y == 89) color = RGB(15, 25, 29); if (y > 78 && y < 87 && (x + y) % 10 == 0) color = RGB(22, 29, 30); } if (y < 62 || y == 95) color = RGB(20, 13, 8); } /* Dresser with two little brass handles. */ if (x >= 96 && x < 132 && y >= 54 && y < 69) { color = RGB(13, 9, 6); if (x > 98 && x < 129 && y > 56 && y < 67) color = RGB(23, 16, 10); if (y == 61) color = RGB(15, 10, 7); if (x >= 111 && x < 116 && (y == 59 || y == 64)) color = RGB(31, 25, 10); if (y == 54) color = RGB(29, 22, 14); } /* Red woven rug, clear of the furniture. */ if (x >= 86 && x < 140 && y >= 83 && y < 113) { color = RGB(22, 9, 12); if (x < 89 || x >= 137 || y < 86 || y >= 110) color = RGB(29, 23, 15); else if ((x + y) % 12 == 0 || (x - y + 240) % 12 == 0) color = RGB(28, 15, 16); } /* Dining table, shaded edge, legs, and a little cup. */ if (x >= 150 && x < 188 && y >= 76 && y < 102) { color = RGB(12, 8, 6); if (y < 97 && x > 151 && x < 186) color = RGB(25, 18, 11); if (y == 78) color = RGB(30, 24, 16); if (y >= 97 && x >= 156 && x < 182) return RGB(18, 13, 9); if (x >= 168 && x < 174 && y >= 83 && y < 89) color = RGB(30, 29, 24); if (x >= 169 && x < 173 && y == 83) color = RGB(12, 9, 7); } /* Potted plant tucked into the top-right corner. */ if (x >= 197 && x < 207 && y >= 65 && y < 77) color = y < 68 ? RGB(28, 17, 10) : RGB(21, 11, 8); int px = x - 201, py = y - 60; if (px * px + py * py < 51) color = (x + y) % 4 == 0 ? RGB(15, 25, 10) : RGB(5, 17, 9); /* Entry mat and a bright opening through the bottom wall. */ if (x >= 108 && x < 132 && y >= 120 && y < 134) color = y % 3 == 0 ? RGB(21, 17, 11) : RGB(29, 25, 17); if (x >= 108 && x < 132 && y >= 134) { color = x < 111 || x >= 129 ? RGB(25, 18, 11) : RGB(12, 23, 11); if (x >= 113 && x < 127) color = RGB(27, 25, 16); } return color; } /* Sunpetal Trail, the first area outside Greenleaf Village. */ TEST_INTERNAL u16 route_scenery(int x, int y) { if (y < 18 || y >= 142) return RGB(29, 30, 25); u16 color = ((x / 16 + y / 16) & 1) ? RGB(16, 27, 13) : grass; if ((x & 15) == 4 && (y & 7) >= 5) color = RGB(11, 22, 10); if ((x & 15) == 11 && (y & 15) == 3) color = RGB(21, 29, 15); if (x < 24 || x >= 216 || y >= 136) color = RGB(7, 18, 9); if (y < 40 && (x < 88 || x >= 128)) color = tree_pixel((x - 8) % 20, y - 19, color); if (x < 24) color = tree_pixel(x - 2, (y - 20) % 22, color); if (x >= 216) color = tree_pixel(x - 217, (y - 20) % 22, color); if (y >= 128) color = tree_pixel((x - 8) % 20, y - 127, color); /* A central trail and a short branch to the pond lookout. */ if ((x >= 97 && x < 120 && y < 136) || (x >= 97 && x < 210 && y >= 89 && y < 104)) { color = RGB(29, 27, 18); if ((x == 97 || x == 119) && (y < 89 || y >= 104)) color = RGB(20, 23, 12); if ((y == 89 || y == 103) && x >= 120) color = RGB(20, 23, 12); if ((x & 7) == 3 && (y & 7) == 5) color = RGB(25, 23, 15); if ((x & 15) == 10 && (y & 7) == 2) color = RGB(31, 29, 21); } /* The encounter check uses these exact same grass boundaries. */ if (in_tall_grass(x, y)) { int tx = x & 7, ty = y & 7; if ((tx == 1 && ty >= 2 && ty <= 5) || (tx == 3 && ty >= 1 && ty <= 5) || (tx == 5 && ty >= 3 && ty <= 6)) color = RGB(4, 15, 8); if ((tx == 2 && ty >= 3 && ty <= 6) || (tx == 4 && ty >= 2 && ty <= 6)) color = RGB(9, 22, 9); if ((tx == 1 && ty == 1) || (tx == 3 && ty == 0)) color = RGB(22, 29, 12); } if (x >= 152 && x < 208 && y >= 46 && y < 79) { color = RGB(27, 25, 16); if (x >= 155 && x < 205 && y >= 49 && y < 76) { color = y < 54 ? RGB(6, 15, 25) : RGB(8, 20, 29); if (y % 9 == 0 && (x + y * 3) % 19 < 7) color = RGB(17, 28, 30); if (y == 75) color = RGB(13, 25, 28); } if (x == 154 || y == 48) color = RGB(31, 29, 20); } /* A readable sign pointing north, on two wooden posts. */ if ((x == 128 || x == 142) && y >= 64 && y < 74) color = RGB(16, 11, 7); if (x >= 124 && x < 147 && y >= 55 && y < 68) { color = RGB(13, 9, 7); if (x > 125 && x < 145 && y > 56 && y < 66) color = RGB(29, 24, 15); if ((x == 135 && y >= 59 && y <= 64) || (y == 60 && x >= 134 && x <= 136) || (y == 61 && x >= 133 && x <= 137)) color = RGB(10, 15, 11); } /* A small boulder beside the lower trail. */ int rx = x - 49, ry = y - 114; if (rx * rx + ry * ry * 2 < 190) { color = RGB(10, 13, 14); if (rx * rx + ry * ry * 2 < 145) color = RGB(19, 22, 22); if (y < 112 && x < 53 && rx * rx + ry * ry * 2 < 130) color = RGB(26, 28, 26); if (y > 118) color = RGB(13, 17, 17); } return color; } /* Calculate scenery at one pixel so moving never leaves sprite trails. */ TEST_INTERNAL u16 village_scenery(int x, int y) { if (y < 18 || y >= 142) return RGB(29, 30, 25); u16 color = grass; if (((x >> 4) + (y >> 4)) & 1) color = RGB(16, 27, 13); int gx = x & 15, gy = y & 15; if ((gx == 4 && gy >= 9 && gy <= 11) || (gx == 3 && gy == 8) || (gx == 6 && gy == 9)) color = RGB(11, 22, 10); if (gx == 11 && gy == 4) color = RGB(20, 29, 15); if (x < 8 || x >= 232 || y >= 136) { /* Wooden boundary fences, with a gap where the path leaves town. */ int post = (x < 8 || x >= 232) ? y % 16 : x % 16; color = RGB(9, 20, 10); if (post < 4) color = post == 0 ? RGB(13, 10, 7) : RGB(27, 22, 14); else if (y >= 138 || (x < 8 && x >= 3) || (x >= 232 && x < 237)) color = RGB(22, 17, 11); if (y >= 136 && x > 96 && x < 120) color = RGB(28, 26, 18); if (x >= 232 && y > 106 && y < 120) color = RGB(29, 27, 18); return color; } if (y < 40 && (x < 88 || x >= 128)) color = tree_pixel((x - 8) % 20, y - 19, color); /* Warm sand, darker edges, and little scattered pebbles. */ if ((x > 96 && x < 120) || (y > 106 && y < 120)) { color = RGB(29, 27, 18); if (((x == 97 || x == 119) && (y <= 106 || y >= 120)) || ((y == 107 || y == 119) && (x <= 96 || x >= 120))) color = RGB(20, 23, 12); if ((x & 7) == 3 && (y & 7) == 5) color = RGB(25, 23, 15); if ((x & 15) == 10 && (y & 7) == 2) color = RGB(31, 29, 21); } /* Rounded pond, shoreline, wavelets, reeds, and a flowering lily. */ if (x >= 22 && x < 76 && y >= 44 && y < 86) { int corner = (x < 26 || x > 71) && (y < 48 || y > 81); if (!corner) { color = RGB(22, 22, 13); if (x >= 24 && x < 74 && y >= 46 && y < 84) color = RGB(29, 27, 18); if (x >= 26 && x < 72 && y >= 48 && y < 82) { color = y < 53 ? RGB(6, 15, 25) : RGB(8, 20, 29); if (y >= 78) color = RGB(10, 23, 26); if (y % 9 == 0 && (x + y * 3) % 19 < 7) color = RGB(17, 28, 29); if (y % 9 == 1 && (x + y * 3) % 19 < 5) color = RGB(8, 21, 27); int lx = x - 61, ly = y - 59; if (lx * lx + ly * ly < 19 && !(lx > 0 && ly < 0)) color = ly < 0 ? RGB(16, 26, 10) : RGB(8, 20, 10); if ((x == 59 && y >= 55 && y <= 57) || (y == 56 && x >= 58 && x <= 60)) color = RGB(31, 19, 24); } } if ((x == 25 || x == 29 || x == 31) && y >= 76 && y <= 81) color = RGB(7, 17, 7); } /* Cottage: chimney, individual roof tiles, beams, glass, and steps. */ if (x >= 154 && x < 204 && y >= 70 && y < 76) color = RGB(8, 17, 9); if (x >= 170 && x < 185 && y >= 71 && y < 78) color = y % 3 == 0 ? RGB(15, 16, 14) : RGB(24, 24, 19); if (x >= 191 && x < 198 && y >= 27 && y < 39) { color = ((y % 4 == 0) || (x == 194 && y % 8 < 4)) ? RGB(14, 10, 9) : RGB(22, 16, 13); if (y == 27) color = RGB(10, 8, 8); } if (x >= 152 && x < 201 && y >= 49 && y < 72) { color = RGB(29, 27, 20); if (y < 52) color = RGB(21, 19, 14); if (x < 155 || x >= 198 || y == 69 || y == 71) color = RGB(13, 10, 8); if (x >= 171 && x < 184 && y >= 54) { color = RGB(12, 8, 6); if (door_open) { color = RGB(3, 5, 7); if (x == 171) color = RGB(23, 16, 10); } else { if (x >= 173 && x < 182 && y > 55) color = RGB(20, 13, 8); if (x == 177) color = RGB(16, 10, 7); if (x == 180 && y == 63) color = RGB(31, 25, 10); } } if (((x >= 157 && x < 167) || (x >= 186 && x < 196)) && y >= 54 && y < 65) { int wx = x >= 186 ? x - 186 : x - 157; color = RGB(12, 11, 10); if (wx > 0 && wx < 9 && y > 54 && y < 63) color = RGB(10, 21, 27); if (wx > 1 && wx < 4 && y > 55 && y < 59) color = RGB(23, 30, 30); if (wx == 4 || y == 59) color = RGB(21, 19, 14); if (y == 64) color = RGB(26, 24, 18); } } if (y >= 32 && y < 49 && x >= 147 + (48 - y) / 2 && x < 206 - (48 - y) / 2) { color = RGB(25, 10, 10); if ((y - 32) % 5 == 0 || (x + ((y - 32) / 5) * 4) % 9 == 0) color = RGB(16, 7, 8); else if ((y - 32) % 5 == 1) color = RGB(30, 15, 12); if (y == 48) color = RGB(10, 8, 8); } /* Flowers have stems, petals, and golden centers. */ if ((x > 126 && x < 146 && y > 77 && y < 98) || (x > 28 && x < 72 && y > 95 && y < 104)) { int fx = x % 7, fy = y % 8; if (fx == 3 && fy >= 4 && fy <= 6) color = RGB(5, 16, 7); if ((fx == 2 && fy == 5) || (fx == 4 && fy == 6)) color = RGB(7, 20, 8); if ((fx == 3 && (fy == 1 || fy == 3)) || (fy == 2 && (fx == 2 || fx == 4))) color = (x / 7) & 1 ? RGB(31, 20, 24) : RGB(30, 30, 25); if (fx == 3 && fy == 2) color = RGB(31, 24, 6); } /* Decorative tall-grass tiles: walk through them, no encounters yet. */ if (x >= 24 && x < 80 && y >= 123 && y < 134) { int tx = x & 7, ty = (y - 123) & 7; if ((tx == 1 && ty >= 2 && ty <= 5) || (tx == 3 && ty >= 1 && ty <= 5) || (tx == 5 && ty >= 3 && ty <= 6)) color = RGB(4, 15, 8); if ((tx == 2 && ty >= 3 && ty <= 6) || (tx == 4 && ty >= 2 && ty <= 6)) color = RGB(9, 22, 9); if ((tx == 1 && ty == 1) || (tx == 3 && ty == 0)) color = RGB(22, 29, 12); } return color; } TEST_INTERNAL u16 mellon_garden_scenery(int x, int y) { u16 color = village_scenery(x, y); if (y < 18 || y >= 142) return color; /* Connect west to town and south to the beach; the east boundary stays closed. */ if (x < 8 && y > 106 && y < 120) color = RGB(29, 27, 18); if (x >= 232 && y > 106 && y < 120) color = RGB(9, 20, 10); if (x >= 97 && x < 120 && y >= 128) color = RGB(31, 28, 19); /* Melon beds take the place of the village pond. */ if (x >= 22 && x < 76 && y >= 44 && y < 86) { color = RGB(20, 15, 9); if (x == 22 || x == 75 || y == 44 || y == 85) color = RGB(29, 23, 14); if (y % 12 == 0) color = RGB(13, 10, 7); int mx = (x - 24) % 16 - 7, my = (y - 45) % 18 - 8; if (mx * mx + my * my < 30) { color = x % 4 < 2 ? RGB(9, 19, 8) : RGB(19, 28, 12); if (my > 3) color = RGB(6, 14, 7); } if (my == -6 && mx == 0) color = RGB(6, 15, 7); } /* A blue roof makes the professor's house easy to recognize. */ if (x >= 147 && x < 206 && y >= 32 && y < 49) { if (color == RGB(25, 10, 10)) color = RGB(8, 16, 24); else if (color == RGB(16, 7, 8)) color = RGB(5, 10, 17); else if (color == RGB(30, 15, 12)) color = RGB(14, 23, 29); } return color; } static u16 mellon_house_scenery(int x, int y) { u16 color = cottage_scenery(x, y); if (color == RGB(22, 9, 12)) color = RGB(7, 17, 15); else if (color == RGB(28, 15, 16)) color = RGB(12, 23, 20); /* A little research computer on the desk at the back of the room. */ if (x >= 104 && x < 124 && y >= 36 && y < 54) { color = RGB(10, 13, 16); if (x > 106 && x < 121 && y > 38 && y < 49) color = RGB(9, 23, 26); if (x > 108 && x < 118 && (y == 41 || y == 45)) color = RGB(23, 30, 26); if (y >= 50 && (x < 112 || x > 116)) color = cottage_scenery(x, y); } /* An open notebook on the table. */ if (x >= 157 && x < 166 && y >= 83 && y < 91) { color = x == 161 ? RGB(18, 19, 16) : RGB(31, 30, 25); if ((y == 85 || y == 88) && x != 161) color = RGB(18, 23, 23); } return color; } TEST_INTERNAL u16 scenery(int x, int y) { #ifdef GAME_TEST ++scenery_samples; #endif if (current_room == ROUTE) return route_scenery(x, y); if (current_room == COTTAGE) return cottage_scenery(x, y); if (current_room == MELLON_GARDEN) return mellon_garden_scenery(x, y); if (current_room == MELLON_HOUSE) return mellon_house_scenery(x, y); if (current_room == BEACH) return beach_scenery(x, y); return village_scenery(x, y); } static void ensure_background(void) { if (!background_valid || background_room != current_room) { for (int row = 0; row < HEIGHT; ++row) for (int col = 0; col < WIDTH; ++col) background_cache[row * WIDTH + col] = scenery(col, row); background_room = current_room; background_door_open = door_open; background_valid = 1; } else if (background_door_open != door_open) { /* Only these door pixels change; keep the rest of the map cached. */ if (current_room == OUTSIDE || current_room == MELLON_GARDEN) for (int row = 54; row < 72; ++row) for (int col = 171; col < 184; ++col) background_cache[row * WIDTH + col] = scenery(col, row); background_door_open = door_open; } } void restore(int x, int y, int w, int h) { int left = x < 0 ? 0 : x; int right = x + w > WIDTH ? WIDTH : x + w; int top = y < 0 ? 0 : y; int bottom = y + h > HEIGHT ? HEIGHT : y + h; if (left >= right || top >= bottom) return; ensure_background(); mark_dirty(left, top, right - left, bottom - top); for (int row = top; row < bottom; ++row) { const u16 *source = &background_cache[row * WIDTH + left]; u16 *destination = &SCREEN[row * WIDTH + left]; for (int col = 0; col < right - left; ++col) destination[col] = source[col]; } }