#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
#define cls system("cls")
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1 : 0)
using namespace std;
// Original algorithm and random-call order intentionally preserved.
namespace craft {
// #define int long long
// #define double long double
// #define map unordered_map
// #pragma GCC optimize(2,3,"Ofast","inline")
// Change world_end to resize the world, then recompile.
// Valid generated columns are 1..world_end; column 0 is the original guard.
const int world_end = 200000;
static_assert(world_end >= 1000 && world_end <= 200000,
"world_end must be between 1000 and 200000");
const int spawn_x = world_end / 2;
// Original algorithms inspect a small halo past the final column.
const int world_width = world_end + 160;
const int mountain_count = world_end / 4;
const int progress_step = max(1, world_end / 20);
const int world_height = 1024;
const int prime_mod = 1e9 + 7;
const int ntt_mod = 998244353;
const double pi = 3.1415926;
const double eps = 0.00001;
const string version = "0.008-3";
const string game_name = "C++ Craft";
// Game settings (body remains enabled as in the supplied file).
bool flat_world;
bool sun_water;
bool game_started;
bool show_body;
bool fly_mode;
bool time_on;
bool small_sea;
bool work_mode;
int seed;
// True-color styles. The legacy palette is retained for menus and status.
// World materials use independent RGB colors in the block table below.
struct rgb {
int r, g, b;
};
struct text_style {
rgb fg, bg;
};
const rgb palette[16] = {
{12, 12, 12}, {0, 55, 218}, {19, 161, 14}, {58, 150, 221},
{197, 15, 31}, {136, 23, 152}, {243, 156, 17}, {204, 204, 204},
{118, 118, 118}, {59, 120, 255}, {22, 198, 12}, {97, 214, 214},
{231, 72, 86}, {180, 0, 158}, {249, 241, 165}, {242, 242, 242}};
text_style style_from_attr(int attr) {
return {palette[attr & 0x0f], palette[(attr >> 4) & 0x0f]};
}
text_style default_style = style_from_attr(0x07);
bool face_right = true;
bool enable_rgb() {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode;
if (!GetConsoleMode(handle, &mode) ||
!SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) ||
!SetConsoleOutputCP(CP_UTF8))
return false;
CONSOLE_SCREEN_BUFFER_INFO info;
if (GetConsoleScreenBufferInfo(handle, &info))
default_style = style_from_attr(info.wAttributes);
return true;
}
// Use the terminal's alternate screen so the loading animation never enters
// the user's normal scrollback. Restore it on normal exit and console signals.
void restore_screen() { cout << "\x1b[0m\x1b[?25h\x1b[?1049l" << flush; }
BOOL WINAPI handle_console_event(DWORD event) {
if (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT ||
event == CTRL_CLOSE_EVENT || event == CTRL_LOGOFF_EVENT ||
event == CTRL_SHUTDOWN_EVENT) {
const char restore[] = "\x1b[0m\x1b[?25h\x1b[?1049l";
DWORD written = 0;
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), restore,
static_cast<DWORD>(sizeof(restore) - 1), &written, nullptr);
}
return FALSE; // Let Windows perform its normal shutdown handling.
}
void enter_screen() {
cout << "\x1b[?1049h\x1b[2J\x1b[H" << flush;
atexit(restore_screen);
SetConsoleCtrlHandler(handle_console_event, TRUE);
}
// Two half-tile characters form hair and face; reversing them turns the head.
string head_pattern() {
return face_right ? "\xE2\x96\x88\xE2\x96\x80" // █▀
: "\xE2\x96\x80\xE2\x96\x88"; // ▀█
}
void set_style(const text_style &style) {
cout << "\x1b[38;2;" << style.fg.r << ';' << style.fg.g << ';' << style.fg.b
<< ";48;2;" << style.bg.r << ';' << style.bg.g << ';' << style.bg.b
<< 'm';
}
void print_text(const string &text, const text_style &style, int delay_ms = 0) {
if (!work_mode)
set_style(style);
if (delay_ms == 0) {
cout << text;
} else {
for (char ch : text) {
cout << ch;
Sleep(delay_ms);
}
}
set_style(default_style);
}
// Existing page, sky and character attributes map through the confirmed
// palette.
void print_text(const string &text, int attr = 7, int delay_ms = 0) {
print_text(text, style_from_attr(attr), delay_ms);
}
int get_seed(int x) {
if (x == -1)
return time(0);
return x;
}
void hide_cursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
// Coordinate conversions are kept separate from world generation and drawing.
namespace coord {
const int origin_x = spawn_x;
const int origin_y = 64;
const int tile_width = 2;
// The console uses character columns, while rendering uses two-column tiles.
void set_pos(int row, int tile_col) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {short(tile_col * tile_width), short(row)};
SetConsoleCursorPosition(handle, pos);
}
// World array coordinates <-> coordinates relative to the original origin.
pair<int, int> to_world(int x, int y) {
return make_pair(x + origin_x, y + origin_y);
}
pair<int, int> to_view(int x, int y) {
return make_pair(x - origin_x, y - origin_y);
}
// The original HUD displays relative X but the raw world Y.
int hud_x(int world_x) { return world_x - origin_x; }
} // namespace coord
pair<int, int> cam_pos;
pair<int, int> prev_cam;
// World storage and player/camera state.
int world[world_width][world_height];
int nether[world_width][world_height];
int height_map[world_width];
// 0: no forest, 1: oak, 2: cherry grove, 3: spruce forest.
vector<unsigned char> forest_plan;
// Shores already committed to water must not be repurposed by later lakes.
vector<unsigned char> shore_used;
vector<int> forest_prefix;
// Snow style is assigned to each spruce leaf by its generating tree.
unordered_map<long long, bool> spruce_snow_style;
int hour = 5;
int minute = 0;
int second = 0;
int unused_second;
// One complete game day lasts 5 real minutes, independent of frame rate.
const double game_seconds_per_real_second = 288.0;
double clock_fraction = 0.0;
std::chrono::steady_clock::time_point clock_last;
void reset_clock() {
hour = 5;
minute = second = 0;
clock_fraction = 0;
clock_last = std::chrono::steady_clock::now();
}
void advance_clock(double elapsed) {
if (!time_on || elapsed <= 0)
return;
clock_fraction += elapsed * game_seconds_per_real_second;
long long whole = static_cast<long long>(clock_fraction + 1e-9);
clock_fraction = max(0.0, clock_fraction - whole);
long long day_time = (hour * 3600LL + minute * 60LL + second + whole) % 86400;
hour = static_cast<int>(day_time / 3600);
minute = static_cast<int>((day_time / 60) % 60);
second = static_cast<int>(day_time % 60);
}
void tick_clock() {
const auto now = std::chrono::steady_clock::now();
double elapsed = std::chrono::duration<double>(now - clock_last).count();
clock_last = now;
advance_clock(elapsed);
}
int player_x;
int player_y;
int prev_x;
int prev_y;
int frame_wait = 1000 / 60;
double fall_sp;
// Velocity and gravity are measured in tiles per simulation frame.
double gravity = 0.18;
double fall_rem = 0.0;
bool dive_down = false;
const double jump_sp = 1.15;
const double swim_sp = 0.6;
const double max_fall_sp = 2.5;
// Block IDs are the original stored values. Keep this table in ID order.
// LittleSnow uses its foreground with the current sky background at draw time.
enum block_id {
block_air = -1,
block_grass = 0,
block_snow = 1,
block_dirt = 2,
block_stone = 3,
block_wood = 4,
block_high_leaf_0 = 5,
block_high_leaf_1 = 6,
block_leaf_0 = 7,
block_leaf_1 = 8,
block_water = 9,
block_cloud = 10,
block_bedrock = 11,
block_sand = 12,
block_ice = 13,
block_coal = 14,
block_iron = 15,
block_gold = 16,
block_diamond = 17,
block_ruby = 18,
block_gravel = 19,
block_sandstone = 20,
block_cactus = 21,
block_little_snow = 22,
block_bamboo = 23,
block_plant_snow = 24,
block_cherry_wood = 25,
block_cherry_leaf = 26,
block_spruce_wood = 27,
block_cactus_flower = 28,
block_kelp = 29,
block_count = 30
};
struct block_info {
block_id id;
const char *name;
const char *pattern;
text_style style;
};
const block_info blocks[] = {
{block_grass, "Grass", "==", {{140, 185, 91}, {112, 157, 67}}},
{block_snow, "Snow", "==", {{201, 216, 226}, {238, 244, 248}}},
{block_dirt, "Dirt", ":.", {{108, 77, 52}, {151, 112, 79}}},
{block_stone, "Stone", " ", {{103, 103, 103}, {126, 126, 126}}},
{block_wood, "Wood", "||", {{158, 124, 76}, {90, 68, 40}}},
{block_high_leaf_0, "HighLeaf_0", "##", {{54, 103, 67}, {37, 75, 49}}},
{block_high_leaf_1, "HighLeaf_1", "##", {{54, 103, 67}, {37, 75, 49}}},
{block_leaf_0, "NormalLeaf_0", "##", {{81, 139, 57}, {50, 105, 41}}},
{block_leaf_1, "NormalLeaf_1", "##", {{81, 139, 57}, {50, 105, 41}}},
{block_water, "NormalWater", " ", {{43, 100, 180}, {45, 104, 180}}},
{block_cloud, "Cloud", " ", {{12, 12, 12}, {204, 204, 204}}},
{block_bedrock, "BasicStone", "==", {{95, 95, 95}, {48, 48, 48}}},
{block_sand, "Sand", "##", {{216, 202, 143}, {231, 218, 163}}},
{block_ice, "Ice", ":.", {{220, 241, 252}, {143, 185, 219}}},
{block_coal, "Coal", ".:", {{42, 42, 42}, {126, 126, 126}}},
{block_iron, "Iron", "::", {{216, 181, 155}, {126, 126, 126}}},
{block_gold, "Gold", "..", {{246, 207, 67}, {126, 126, 126}}},
{block_diamond, "Diamond", ":.", {{80, 225, 218}, {126, 126, 126}}},
{block_ruby, "Ruby", "::", {{218, 65, 86}, {126, 126, 126}}},
{block_gravel, "Gravel", "##", {{96, 92, 88}, {138, 132, 126}}},
{block_sandstone, "SandStone", "==", {{201, 182, 127}, {223, 207, 154}}},
{block_cactus, "Cactus", "||", {{112, 163, 75}, {49, 113, 43}}},
// Fallback pattern for thin snow; rendering chooses one of two heights.
{block_little_snow,
"LittleSnow",
"\xE2\x96\x83\xE2\x96\x83",
{{247, 251, 255}, {0, 0, 0}}},
{block_bamboo, "Bamboo", "||", {{166, 200, 97}, {91, 153, 64}}},
{block_plant_snow,
"PlantSnow",
"\xE2\x96\x81\xE2\x96\x81",
{{247, 251, 255}, {0, 0, 0}}},
{block_cherry_wood, "CherryWood", "||", {{150, 105, 96}, {83, 57, 52}}},
{block_cherry_leaf, "CherryLeaf", "##", {{252, 197, 211}, {220, 145, 172}}},
{block_spruce_wood, "SpruceWood", "||", {{111, 80, 53}, {67, 48, 34}}},
{block_cactus_flower,
"CactusFlower",
"\xE2\x96\x84\xE2\x96\x84",
{{210, 121, 135}, {0, 0, 0}}},
{block_kelp, "Kelp", "||", {{79, 142, 74}, {45, 104, 180}}},
};
static_assert(sizeof(blocks) / sizeof(blocks[0]) == block_count,
"block table size mismatch");
const block_info *get_block(int id) {
return id >= 0 && id < block_count ? &blocks[id] : nullptr;
}
string block_name(int id) {
const block_info *block = get_block(id);
return block ? block->name : "";
}
double rand_slope() {
int roll = rand() % 100;
if (roll <= 42)
return 1.5;
if (roll <= 42 + 32)
return 1.2;
if (roll <= 42 + 32 + 22)
return 1;
return 0.7;
}
bool is_water_block(int block) {
return block == block_water || block == block_kelp;
}
bool is_solid(int x, int y) {
return (world[x][y] > block_air &&
(world[x][y] < block_wood || world[x][y] > block_cloud) &&
world[x][y] != block_cactus && world[x][y] != block_little_snow &&
world[x][y] != block_bamboo && world[x][y] != block_plant_snow &&
world[x][y] != block_cherry_wood &&
world[x][y] != block_cherry_leaf &&
world[x][y] != block_spruce_wood &&
world[x][y] != block_cactus_flower && world[x][y] != block_kelp);
}
void fill_stone(int i, int j, int value) {
if (i >= 0 && i <= world_end && j >= 0 && j < world_height &&
world[i][j] == block_stone)
world[i][j] = value;
}
void make_hole(int x, int y, int z) {
for (int i = max(0, x - z); i <= min(world_end, x + z); i++) {
for (int j = max(y - z, 0); j <= y + z; j++) {
if (min(abs(i - max(0, x - z)), abs(i - (x + z))) +
min(abs(j - max(0, y - z)), abs(j - (y + z))) <
3)
continue;
else if (min(abs(i - max(0, x - z)), abs(i - (x + z))) +
min(abs(j - max(0, y - z)), abs(j - (y + z))) ==
3) {
int roll = ((rand() % 100000) * rand()) % 1000000;
if (roll < 100000) { // up
if (j < 20)
continue;
roll = rand() % 2;
if (roll == 0) {
fill_stone(i - 1, j, block_coal);
fill_stone(i - 2, j, block_coal);
fill_stone(i, j - 1, block_coal);
fill_stone(i - 1, j - 1, block_coal);
fill_stone(i - 2, j - 1, block_coal);
fill_stone(i - 3, j - 1, block_coal);
fill_stone(i - 1, j - 2, block_coal);
fill_stone(i - 2, j - 2, block_coal);
} else {
fill_stone(i - 1, j, block_coal);
fill_stone(i - 2, j, block_coal);
fill_stone(i, j - 1, block_coal);
fill_stone(i - 2, j - 1, block_coal);
fill_stone(i - 1, j - 2, block_coal);
fill_stone(i - 2, j - 2, block_coal);
fill_stone(i - 2, j - 3, block_coal);
fill_stone(i - 1, j - 3, block_coal);
fill_stone(i, j - 3, block_coal);
fill_stone(i + 1, j - 3, block_coal);
}
} else if (roll < 100000 + 40000) {
roll = rand() % 2;
if (roll == 0) {
fill_stone(i - 1, j, block_iron);
fill_stone(i - 2, j, block_iron);
fill_stone(i, j - 1, block_iron);
fill_stone(i - 1, j - 1, block_iron);
fill_stone(i - 2, j - 1, block_iron);
fill_stone(i - 3, j - 1, block_iron);
} else {
fill_stone(i - 1, j, block_iron);
fill_stone(i - 2, j, block_iron);
fill_stone(i, j - 1, block_iron);
fill_stone(i - 2, j - 1, block_iron);
fill_stone(i - 1, j - 2, block_iron);
fill_stone(i - 2, j - 2, block_iron);
}
} else if (roll < 100000 + 40000 + 20000) {
fill_stone(i, j, block_gold);
fill_stone(i - 1, j, block_gold);
fill_stone(i, j - 1, block_gold);
roll = rand() % 2;
if (roll == 0) {
fill_stone(i + 1, j - 1, block_gold);
} else {
fill_stone(i - 1, j - 1, block_gold);
}
} else if (roll < 100000 + 40000 + 20000 + 10000) {
if (j > 30)
continue; // deep
fill_stone(i, j, block_diamond);
fill_stone(i - 1, j, block_diamond);
fill_stone(i, j - 1, block_diamond);
roll = rand() % 2;
if (roll == 0) {
fill_stone(i + 1, j - 1, block_diamond);
} else {
fill_stone(i - 1, j - 1, block_diamond);
}
} else
world[i][j] = block_stone;
} else if (world[i][j] != block_bedrock)
world[i][j] = block_air;
}
}
}
int rand_radius() {
int roll = rand() % 100;
if (roll < 50)
return rand() % 10; // 50%
if (roll < 75)
return rand() % 50; // 25%
if (roll < 90)
return rand() % 20; // 15%
if (roll < 97)
return rand() % 30; // 7%
return rand() % 40; // 3%
}
// Approximate Java-edition daylight; biome and weather tint are not simulated.
// Per-position hash keeps snow stable without consuming the world's rand()
// sequence.
uint32_t snow_hash(int x, int y) {
uint32_t v = static_cast<uint32_t>(seed) ^
(static_cast<uint32_t>(x) * 0x9e3779b9u) ^
(static_cast<uint32_t>(y) * 0x85ebca6bu);
v ^= v >> 16;
v *= 0x7feb352du;
v ^= v >> 15;
v *= 0x846ca68bu;
v ^= v >> 16;
return v;
}
const char *snow_pattern(int x, int y, bool plant_snow) {
uint32_t v = snow_hash(x, y);
if (plant_snow) {
auto it = spruce_snow_style.find(static_cast<long long>(x) * world_height +
y - 1);
bool thick = it != spruce_snow_style.end() && it->second;
return thick ? "\xE2\x96\x83\xE2\x96\x83" // ▃▃
: "\xE2\x96\x81\xE2\x96\x81"; // ▁▁
}
return (v & 1u) ? "\xE2\x96\x84\xE2\x96\x84" // ▄▄
: "\xE2\x96\x83\xE2\x96\x83"; // ▃▃
}
rgb sky_at(double t) {
// Smooth cyclic transitions between night, dawn, daylight and dusk.
const int times[] = {0, 4 * 60, 6 * 60, 8 * 60,
17 * 60, 19 * 60, 21 * 60, 24 * 60};
const rgb colors[] = {{11, 18, 42}, {11, 18, 42}, {201, 132, 99},
{120, 167, 255}, {120, 167, 255}, {201, 132, 99},
{11, 18, 42}, {11, 18, 42}};
for (int i = 0; i < 7; ++i)
if (t <= times[i + 1]) {
double f = (t - times[i]) / (times[i + 1] - times[i]);
auto blend = [f](int a, int b) {
return static_cast<int>(a + (b - a) * f + 0.5);
};
return {blend(colors[i].r, colors[i + 1].r),
blend(colors[i].g, colors[i + 1].g),
blend(colors[i].b, colors[i + 1].b)};
}
return colors[7];
}
rgb sky_color() {
return time_on ? sky_at(hour * 60.0 + minute + second / 60.0)
: sky_at(12 * 60);
}
text_style sky_style() { return {{0, 0, 0}, sky_color()}; }
// The face is unchanged from v0.006-20; the shirt is a solid color.
// The player uses the same appearance on land and in water.
const text_style player_body_style = {{38, 90, 108}, {60, 166, 163}};
const text_style player_head_style = {{67, 42, 31}, {190, 144, 110}};
// Two-character cells allow drawing only changed runs without reordering them.
const int view_cols = 31;
const int view_rows = 21;
struct frame_cell {
string pattern;
text_style style;
size_t split;
text_style second;
};
bool same_color(const rgb &a, const rgb &b) {
return a.r == b.r && a.g == b.g && a.b == b.b;
}
bool same_style(const text_style &a, const text_style &b) {
return same_color(a.fg, b.fg) && same_color(a.bg, b.bg);
}
bool same_cell(const frame_cell &a, const frame_cell &b) {
return a.pattern == b.pattern && same_style(a.style, b.style) &&
a.split == b.split && (a.split == 0 || same_style(a.second, b.second));
}
struct frame {
vector<frame_cell> cells;
frame() { cells.reserve(view_cols * view_rows); }
void put(const string &pattern, text_style style) {
if (work_mode)
style = default_style;
cells.push_back({pattern, style, 0, style});
}
void put_split(const string &first, text_style a, const string &second,
text_style b) {
if (work_mode)
a = b = default_style;
cells.push_back({first + second, a, first.size(), b});
}
void put(const string &pattern, int attr) {
put(pattern, style_from_attr(attr));
}
};
void append_style(string &out, const text_style &style) {
out += "\x1b[38;2;" + to_string(style.fg.r) + ";" + to_string(style.fg.g) +
";" + to_string(style.fg.b) + ";48;2;" + to_string(style.bg.r) + ";" +
to_string(style.bg.g) + ";" + to_string(style.bg.b) + "m";
}
vector<frame_cell> prev_frame;
void reset_frame() { prev_frame.clear(); }
void draw_frame(const frame &next) {
vector<frame_cell> &prev = prev_frame;
string out;
out.reserve(view_cols * view_rows * 8);
const bool first = prev.size() != next.cells.size();
bool have_style = false;
text_style active = default_style;
// Flush previous printf output before moving the native cursor. Setting it to
// buffer origin also brings that origin into the Windows console viewport.
fflush(stdout);
coord::set_pos(0, 0);
for (int row = 0; row < view_rows; ++row) {
int col = 0;
while (col < view_cols) {
int index = row * view_cols + col;
if (!first && same_cell(next.cells[index], prev[index])) {
++col;
continue;
}
// VT cursor coordinates are one-based; runs are top-to-bottom,
// left-to-right.
out += "\x1b[" + to_string(row + 1) + ";" + to_string(col * 2 + 1) + "H";
while (col < view_cols) {
index = row * view_cols + col;
const frame_cell &cell = next.cells[index];
if (!first && same_cell(cell, prev[index]))
break;
if (!have_style || !same_style(active, cell.style)) {
append_style(out, cell.style);
active = cell.style;
have_style = true;
}
if (cell.split) {
out.append(cell.pattern, 0, cell.split);
if (!same_style(active, cell.second)) {
append_style(out, cell.second);
active = cell.second;
}
out.append(cell.pattern, cell.split, string::npos);
} else {
out += cell.pattern;
}
++col;
}
}
}
if (!out.empty()) {
append_style(out, default_style);
cout.write(out.data(), static_cast<streamsize>(out.size()));
cout.flush();
}
prev = next.cells;
coord::set_pos(view_rows, 0);
}
void put_water(frame &out, int x, int y) {
const bool exposed = y + 1 >= world_height || world[x][y + 1] == block_air;
if (exposed) {
text_style style = blocks[block_water].style;
style.fg = {102, 159, 205};
out.put("\xE2\x89\x88\xE2\x89\x88", style); // ≈≈
} else {
out.put(blocks[block_water].pattern, blocks[block_water].style);
}
}
void draw_world() {
frame next;
for (int j = cam_pos.second + 10, y = 1; j >= cam_pos.second - 10; j--, y++) {
for (int i = cam_pos.first - 15, x = 1; i <= cam_pos.first + 15; i++, x++) {
if (i == player_x && j == player_y && show_body) {
next.put(" ", player_body_style);
} else if (i == player_x && j == player_y + 1 && show_body) {
next.put(head_pattern(), player_head_style);
} else if (get_block(world[i][j]) && world[i][j] != block_water) {
const block_info &block = *get_block(world[i][j]);
text_style style = block.style;
const bool is_snow =
block.id == block_little_snow || block.id == block_plant_snow;
if (is_snow || block.id == block_bamboo ||
block.id == block_cactus_flower)
style.bg = sky_color();
next.put(is_snow ? snow_pattern(i, j, block.id == block_plant_snow)
: block.pattern,
style);
} else if (!sun_water && world[i][j] == block_water)
put_water(next, i, j); // Water
else if (world[i][j] == block_water)
put_water(next, i, j); // Water
else
next.put(" ", sky_style());
}
}
draw_frame(next);
}
// Shared console pages. Keep the original text, colors, and cursor order.
namespace page {
const char *const border = "==============================\n";
const char *const stage_labels[] = {
"Update Ground : ", "Update Mountains : ", "Update Field : ",
"Update Dirt : ", "Update Tree : ", "Update Water : ",
"Update Cloud : ", "Update Mineral : ", "Update Minehole : "};
enum stage {
ground,
mountains,
field,
dirt,
tree,
water,
cloud,
mineral,
minehole
};
void header() {
print_text(border, 0xe);
print_text("C++ Craft v" + version + "\n\n");
}
// Build the loading screen from the same two-column cells and material
// styles as the game. Both loading and game use the same buffered renderer.
void loading_scene(stage current, int percent, bool finished = false) {
percent = max(0, min(100, percent));
const text_style panel = {{29, 69, 91}, {197, 229, 242}};
const text_style bar = {{80, 205, 62}, {48, 70, 48}};
const char *names[] = {"Ground", "Mountains", "Field", "Dirt", "Trees",
"Water", "Clouds", "Minerals", "Caves"};
frame scene;
const text_style loading_sky = {{0, 0, 0}, sky_at(12 * 60)};
string title = game_name + " v" + version;
string title_line(62, ' ');
title_line.replace(0, title.size(), title);
for (int row = 0; row < view_rows; ++row) {
string line(38, ' ');
const bool in_panel = row >= 5 && row <= 11;
if (in_panel) {
line[0] = '|';
line[37] = '|';
if (row == 5 || row == 11)
line = "+" + string(36, '-') + "+";
if (row == 7) {
string label =
finished ? "World ready" : string("Generating: ") + names[current];
line.replace(2, label.size(), label);
}
if (row == 9) {
int fill = percent * 24 / 100;
line.replace(2, 26,
"[" + string(fill, '#') + string(24 - fill, '.') + "]");
string number = to_string(percent) + "%";
line.replace(30, number.size(), number);
}
}
for (int col = 0; col < view_cols; ++col) {
if (row == 0) {
scene.put(title_line.substr(col * 2, 2),
{{245, 250, 255}, loading_sky.bg});
} else if (row == 15) {
scene.put(blocks[block_grass].pattern, blocks[block_grass].style);
} else if (row > 15) {
scene.put(blocks[block_dirt].pattern, blocks[block_dirt].style);
} else if (col == 3 && row == 13) {
scene.put(head_pattern(), player_head_style);
} else if (col == 3 && row == 14) {
scene.put(" ", player_body_style);
} else if (in_panel && col >= 9 && col < 28) {
int offset = (col - 9) * 2;
if (row == 5 || row == 11) {
scene.put(col == 9 ? (row == 5 ? "┏━" : "┗━")
: col == 27 ? (row == 5 ? "━┓" : "━┛")
: "━━",
panel);
continue;
}
if (col == 9) {
scene.put("┃ ", panel);
continue;
}
if (col == 27) {
scene.put(" ┃", panel);
continue;
}
string part = line.substr(offset, 2);
// The progress boundary may cut a two-character cell in half.
// Color each character separately so brackets never inherit bar color.
text_style first = row == 9 && part[0] == '#' ? bar : panel;
text_style second = row == 9 && part[1] == '#' ? bar : panel;
if (same_style(first, second))
scene.put(part, first);
else
scene.put_split(part.substr(0, 1), first, part.substr(1, 1), second);
} else {
scene.put(" ", loading_sky);
}
}
}
draw_frame(scene);
}
void progress(stage current, int percent) {
loading_scene(current, percent);
if (current == ground)
Sleep(50);
}
void complete() {
loading_scene(minehole, 100, true);
Sleep(1000);
// No native cursor moves while output remains buffered. Clear and force a
// full game redraw instead of reusing the loading screen's cached cells.
cout << "\x1b[0m\x1b[2J\x1b[H" << flush;
reset_frame();
coord::set_pos(0, 0);
}
void status() {
// Only overwrite changed lines, without erasing them first. This avoids
// flicker and preserves the final row in a 25-line console.
static string previous[4];
char x_text[40], y_text[40], time_text[40], seed_text[40];
snprintf(x_text, sizeof(x_text), " x: %6d", coord::hud_x(cam_pos.first));
snprintf(y_text, sizeof(y_text), " y: %6d", cam_pos.second);
snprintf(time_text, sizeof(time_text), "time: %02d:%02d", hour, minute);
snprintf(seed_text, sizeof(seed_text), "seed: %d", seed);
string lines[] = {x_text, y_text, time_on ? time_text : "", seed_text};
string out;
for (int i = 0; i < 4; ++i) {
if (lines[i] == previous[i])
continue;
out += "\x1b[" + to_string(view_rows + 1 + i) + ";1H" + lines[i];
if (previous[i].size() > lines[i].size())
out += string(previous[i].size() - lines[i].size(), ' ');
previous[i] = lines[i];
}
if (!out.empty()) {
cout << out << flush;
}
}
} // namespace page
// Build a connected sandy basin. At either bank it starts one block below
// the surface, then descends at most one block per two horizontal columns.
void make_lake_bed(int left, int right, int top) {
const int width = right - left + 1;
const int max_depth = min(10, max(3, width / 10));
for (int x = left; x <= right; ++x) {
int dist = min(x - left, right - x);
int depth = 1 + min(max_depth - 1, dist / 2);
int floor_y = max(2, top - depth);
int old = height_map[x];
for (int y = floor_y + 1; y <= old; ++y)
if (world[x][y] != block_bedrock && world[x][y] != block_cloud)
world[x][y] = block_air;
// Fill empty space under the designed floor before laying the sand.
for (int y = old + 1; y < floor_y; ++y)
world[x][y] = block_sandstone;
world[x][floor_y] = block_sand;
for (int d = 1; d <= 2 && floor_y - d > 0; ++d)
if (world[x][floor_y - d] != block_bedrock)
world[x][floor_y - d] = d == 1 ? block_sand : block_sandstone;
height_map[x] = floor_y;
}
}
// Lakes may overlap a former shore, where an earlier lake placed sugar cane.
// Clear the entire plant, including the part that would stand above the water.
void clear_lake_plants(int left, int right, int water_y) {
for (int x = max(left, 1); x <= min(right, world_end); ++x) {
int top = min(world_height - 1, max(height_map[x] + 20, water_y + 7));
for (int y = height_map[x]; y <= top; ++y) {
int block = world[x][y];
if ((block >= block_wood && block <= block_leaf_1) ||
block == block_cactus || block == block_bamboo ||
block == block_cherry_wood || block == block_cherry_leaf ||
block == block_spruce_wood || block == block_cactus_flower ||
block == block_plant_snow)
world[x][y] = block_air;
}
}
}
// Snowy desert keeps a solid sand surface with a snow layer above it.
void place_snowy_sand(int x, int surface_y) {
world[x][surface_y] = block_sand;
if (surface_y + 1 < world_height)
world[x][surface_y + 1] = block_little_snow;
}
bool is_tree_block(int id) {
return (id >= block_wood && id <= block_leaf_1) || id == block_plant_snow ||
id == block_cherry_wood || id == block_cherry_leaf ||
id == block_spruce_wood;
}
// Dense oak forest groups are selected as whole regions. Three out of each
// ten groups become cherry candidates, distributed rather than consecutive.
bool cherry_forest(int group) {
const uint32_t offset = snow_hash(0x4348, 0x4552) % 10u;
return (static_cast<uint32_t>(group) * 3u + offset) % 10u < 3u;
}
bool near_water(int x, int radius) {
for (int col = max(1, x - radius); col <= min(world_end, x + radius); ++col) {
int b = world[col][height_map[col]];
if (b == block_water || b == block_ice)
return true;
}
return false;
}
bool tree_site_clear(int x, int ground_y, int radius) {
if (x - radius < 1 || x + radius > world_end)
return false;
for (int dx = -radius; dx <= radius; ++dx)
if (height_map[x + dx] > ground_y + 1)
return false;
return true;
}
bool can_make_cherry(int x, int ground_y) {
if (x < 7 || x > world_end - 7 || ground_y + 17 >= world_height ||
world[x][ground_y] != block_grass || !tree_site_clear(x, ground_y, 7))
return false;
for (int y = ground_y + 1; y <= ground_y + 15; ++y) {
int b = world[x][y];
if (b != block_air && !is_tree_block(b))
return false;
}
return true;
}
// Crowns vary by seed and shift one tile left, right, or remain centered.
int grow_cherry(int x, int ground_y) {
int height = 10 + static_cast<int>(snow_hash(x, 0x4348) % 3u);
int shift = static_cast<int>(snow_hash(x, 0x5346) % 3u) - 1;
for (int y = 1; y <= height - 4; ++y)
world[x][ground_y + y] = block_cherry_wood;
const int widths[] = {3, 4, 5, 6, 6, 5, 4, 2};
for (int layer = 0; layer < 8; ++layer) {
int y = ground_y + height - 6 + layer;
int radius = widths[layer];
for (int dx = -radius; dx <= radius; ++dx) {
int col = x + shift + dx;
if (col < 1 || col > world_end)
continue;
if (abs(dx) == radius && (snow_hash(col, y + 0x4c45) & 1u) == 0)
continue;
int b = world[col][y];
if (b == block_air || is_tree_block(b))
world[col][y] = block_cherry_leaf;
}
}
return ground_y + height + 1;
}
bool can_grow_big_oak(int x, int ground_y) {
if (x < 7 || x + 7 > world_end || ground_y + 20 >= world_height ||
!is_solid(x + 1, ground_y) || !tree_site_clear(x, ground_y, 7))
return false;
for (int y = 1; y <= 15; ++y) {
for (int dx = -1; dx <= 2; ++dx) {
int b = world[x + dx][ground_y + y];
if (b != block_air && !is_tree_block(b))
return false;
}
}
return true;
}
void grow_big_oak(int x, int ground_y) {
int height = 12 + static_cast<int>(snow_hash(x, 0x4845) % 3u);
int lean = (snow_hash(x, 0x4c45) & 1u) ? 1 : -1;
for (int y = 1; y <= height; ++y) {
int shift = y >= 6 ? lean : 0;
for (int dx = 0; dx < 2; ++dx)
world[x + shift + dx][ground_y + y] = block_wood;
}
const int widths[] = {3, 4, 5, 5, 5, 4, 4, 3, 2};
for (int layer = 0; layer < 9; ++layer) {
int y = ground_y + height - 5 + layer;
int radius = widths[layer];
for (int dx = -radius; dx <= radius; ++dx) {
int col = x + lean + dx;
if (col < 1 || col > world_end)
continue;
if (abs(dx) == radius && (snow_hash(col, y + 0x4f41) & 1u) == 0)
continue;
int b = world[col][y];
bool own_trunk =
(col == x + lean || col == x + lean + 1) && b == block_wood;
if (b == block_air || b == block_leaf_0 || b == block_leaf_1 || own_trunk)
world[col][y] = block_leaf_0;
}
}
}
struct oak_site {
int x, ground_y, group;
};
// Convert only clusters with at least two plantable trees. Lone candidates
// remain oak, so cherry trees do not appear as isolated specimens.
void make_cherry_forests(const vector<oak_site> &sites) {
size_t begin = 0;
while (begin < sites.size()) {
size_t end = begin + 1;
while (end < sites.size() && sites[end].group == sites[end - 1].group &&
sites[end].x - sites[end - 1].x <= 16)
++end;
vector<oak_site> selected;
for (size_t n = begin; n < end; ++n) {
const oak_site &site = sites[n];
if ((selected.empty() || site.x - selected.back().x >= 4) &&
can_make_cherry(site.x, site.ground_y))
selected.push_back(site);
}
// Two or more nearby trees constitute a grove.
for (size_t n = 0; n < selected.size(); ++n) {
bool neighbor =
(n > 0 && selected[n].x - selected[n - 1].x <= 20) ||
(n + 1 < selected.size() && selected[n + 1].x - selected[n].x <= 20);
if (neighbor)
grow_cherry(selected[n].x, selected[n].ground_y);
}
begin = end;
}
}
bool can_place_cloud(int x, int y) {
return x >= 1 && x <= world_end && y >= 0 && y < world_height &&
world[x][y] == block_air;
}
// Plan contiguous forests before lakes. Runs shorter than 20 cells never
// become a forest. Each 128-column region is selected independently.
void plan_forests() {
forest_plan.assign(world_end + 1, 0);
for (int left = 1; left <= world_end;) {
int surface = world[left][height_map[left]];
int kind = surface == block_grass ? 1 : surface == block_snow ? 3 : 0;
if (!kind) {
++left;
continue;
}
int right = left;
while (right < world_end &&
world[right + 1][height_map[right + 1]] == surface)
++right;
if (right - left + 1 >= 20) {
for (int start = left; start <= right;) {
int bucket = start / 128;
int end = min(right, (bucket + 1) * 128 - 1);
if (end - start + 1 >= 20 && snow_hash(bucket, 0x464f) % 100u < 50u) {
int chosen = kind == 1 && cherry_forest(bucket) ? 2 : kind;
for (int x = start; x <= end; ++x)
forest_plan[x] = static_cast<unsigned char>(chosen);
}
start = end + 1;
}
}
left = right + 1;
}
forest_prefix.assign(world_end + 1, 0);
for (int x = 1; x <= world_end; ++x)
forest_prefix[x] = forest_prefix[x - 1] + (forest_plan[x] != 0);
}
bool near_cherry(int x, int radius) {
for (int pos = max(1, x - radius); pos <= min(world_end, x + radius); ++pos)
if (forest_plan[pos] == 2)
return true;
return false;
}
bool forest_near(int left, int right) {
left = max(1, left);
right = min(world_end, right);
return left <= right && forest_prefix[right] != forest_prefix[left - 1];
}
// Select once per body of water, not independently for each bank.
int shore_material(int left, bool sea) {
return sea || (snow_hash(left, 0x5348) & 1u) ? block_sand : block_gravel;
}
// Trace each shore from the water outwards and join natural terrain safely.
bool trace_shore(int edge, int direction, int material, int water_y,
bool commit) {
const int edge_y = water_y > 100 ? water_y - 1 : water_y;
int prev = max(2, min(world_height - 22, edge_y));
for (int d = 1; d <= 64; ++d) {
int x = edge + direction * d;
if (x < 1 || x > world_end || (!shore_used.empty() && shore_used[x]))
return false;
int old = height_map[x];
// A low bank would require an artificial dam with a vertical water wall.
if (d == 1 && old < edge_y - 3)
return false;
if (forest_plan[x])
return d > 5 && abs(old - prev) <= 1;
int surface = world[x][old];
if (surface == block_water || surface == block_ice ||
(old + 1 < world_height && world[x][old + 1] == block_water))
return false;
// Do not cut individual trees (including canopies crossing a biome edge).
for (int y = max(1, min(old, prev)); y <= min(world_height - 1, old + 20);
++y)
if (is_tree_block(world[x][y]) || world[x][y] == block_bamboo)
return false;
int target = prev;
// A short level beach followed by a gentler staircase, joined atomically.
if (d > 4 && d % 2 == 0)
target += old > prev ? 1 : old < prev ? -1 : 0;
// Reject a shore if it would require an enormous artificial ramp.
if (abs(target - old) > 3)
return false;
if (commit) {
int base = min(old, target);
for (int y = target + 1; y <= min(world_height - 1, old + 18); ++y)
if (world[x][y] != block_cloud && world[x][y] != block_bedrock)
world[x][y] = block_air;
for (int y = base + 1; y < target; ++y)
if (y > 0)
world[x][y] = block_stone;
world[x][target] = material;
for (int depth = 1; depth <= 2 && target - depth > 0; ++depth)
world[x][target - depth] =
material == block_sand ? block_sandstone : block_gravel;
height_map[x] = target;
if (!shore_used.empty())
shore_used[x] = 1;
}
prev = target;
if (d >= 5 && target == old)
return true;
}
return false;
}
bool can_make_shores(int left, int right, bool sea, int water_y) {
int material = shore_material(left, sea);
return trace_shore(left, -1, material, water_y, false) &&
trace_shore(right, 1, material, water_y, false);
}
void add_shore(int left, int right, bool sea, int water_y) {
int material = shore_material(left, sea);
trace_shore(left, -1, material, water_y, true);
trace_shore(right, 1, material, water_y, true);
}
// Infrequent patches on both banks; each side has one to three stalks.
// Validate both patches before placing anything to avoid partial plants.
void add_cane_pair(int left, int right) {
if (snow_hash(left, 0x4341) % 4u != 0)
return;
int counts[2] = {1 + static_cast<int>(snow_hash(left, 0x434e) % 3u),
1 + static_cast<int>(snow_hash(right, 0x4352) % 3u)};
for (int side = 0; side < 2; ++side) {
for (int d = 1; d <= counts[side]; ++d) {
int x = side == 0 ? left - d : right + d;
if (x < 1 || x > world_end || forest_plan[x])
return;
int y = height_map[x], b = world[x][y];
int height = 3 + static_cast<int>(snow_hash(x, 0x4847) % 3u);
if ((b != block_grass && b != block_dirt && b != block_sand) ||
y + height >= world_height)
return;
for (int h = 1; h <= height; ++h)
if (world[x][y + h] != block_air)
return;
}
}
for (int side = 0; side < 2; ++side)
for (int d = 1; d <= counts[side]; ++d) {
int x = side == 0 ? left - d : right + d;
int y = height_map[x];
int height = 3 + static_cast<int>(snow_hash(x, 0x4847) % 3u);
for (int h = 1; h <= height; ++h)
world[x][y + h] = block_bamboo;
}
}
void remove_single_clouds() {
vector<pair<int, int>> isolated;
for (int x = 1; x <= world_end; ++x)
for (int y = 109; y <= 131; ++y) {
if (world[x][y] != block_cloud)
continue;
bool neighbor = false;
for (int dx = -1; dx <= 1; ++dx)
for (int dy = -1; dy <= 1; ++dy)
if ((dx || dy) && x + dx >= 1 && x + dx <= world_end &&
world[x + dx][y + dy] == block_cloud)
neighbor = true;
if (!neighbor)
isolated.emplace_back(x, y);
}
for (const auto &p : isolated)
world[p.first][p.second] = block_air;
}
void rebuild_forest_prefix() {
for (int x = 1; x <= world_end; ++x)
forest_prefix[x] = forest_prefix[x - 1] + (forest_plan[x] != 0);
}
// Expand a large sea to include entire forest runs and the canopy margin.
// If this would become unreasonably wide, skip it rather than cut a forest.
bool absorb_forests(int &left, int &right) {
bool changed = true;
while (changed) {
changed = false;
int lo = max(1, left - 5), hi = min(world_end, right + 5);
for (int x = lo; x <= hi; ++x) {
if (!forest_plan[x])
continue;
int l = x, r = x;
while (l > 1 && forest_plan[l - 1])
--l;
while (r < world_end && forest_plan[r + 1])
++r;
int nl = max(1, min(left, l - 6)), nr = min(world_end, max(right, r + 6));
if (nr - nl > 600 || nl < 6 || nr > world_end - 6)
return false;
if (nl != left || nr != right) {
left = nl;
right = nr;
changed = true;
}
x = r;
}
}
return true;
}
void clear_absorbed_forests(int left, int right) {
for (int x = left; x <= right; ++x)
forest_plan[x] = 0;
rebuild_forest_prefix();
}
void add_spruce_snow() {
for (int x = 1; x <= world_end; ++x) {
int top = min(world_height - 2, height_map[x] + 24);
for (int y = height_map[x] + 1; y <= top; ++y)
if (world[x][y] == block_high_leaf_0 && world[x][y + 1] == block_air)
world[x][y + 1] = block_plant_snow;
}
}
// Keep non-cherry, non-giant foliage connected and mirror-symmetric. Do not
// erase terrain, water, or another tree's trunk when branches meet.
bool can_replace_branch(int block) {
return block == block_air || block == block_leaf_0 ||
block == block_high_leaf_0 || block == block_plant_snow;
}
void place_symmetric_branches(int x, int y, int radius, int block,
int snow_style = -1) {
for (int k = 1; k <= radius; ++k) {
if (x - k < 1 || x + k > world_end)
break;
if (can_replace_branch(world[x - k][y]) &&
can_replace_branch(world[x + k][y])) {
world[x - k][y] = block;
world[x + k][y] = block;
if (snow_style >= 0) {
spruce_snow_style[static_cast<long long>(x - k) * world_height + y] =
snow_style;
spruce_snow_style[static_cast<long long>(x + k) * world_height + y] =
snow_style;
}
} else {
break; // Avoid detached outer branches when the inner pair is blocked.
}
}
}
// Generate terrain and water first; surface biomes and vegetation follow.
void gen_world() { // Update The World
srand(seed);
spruce_snow_style.clear();
height_map[0] = 64;
for (int i = 1; i <= world_end; i++) {
height_map[i] = height_map[i - 1];
for (int j = 64; j >= 0; j--) {
if (j == 64)
world[i][j] = block_grass; // Grass
else if (j > 60)
world[i][j] = block_dirt; // Dirt
else if (j == 0)
world[i][j] = block_bedrock; // BasicStone
else
world[i][j] = block_stone; // Stone
}
if (i % progress_step == 0) {
page::progress(page::ground, static_cast<int>(100LL * i / world_end));
}
}
if (!flat_world) {
for (int i = 1; i <= mountain_count; i++) { // 50000 moutains
int peak_x = (rand() % (world_end)*rand()) % world_end;
int radius = rand_radius();
double slope = rand_slope();
for (int j = max(0, peak_x - radius);
j <= min(world_end, peak_x + radius); j++) {
for (int level = (radius - abs(peak_x - j)) / slope + 64; level >= 1;
level--) {
world[j][level] = block_stone; // Stone
height_map[j] = max(height_map[j], level);
}
}
if (i % max(1, mountain_count / 100) == 0) {
page::progress(page::mountains,
static_cast<int>(100LL * i / mountain_count));
}
}
for (int i = 1; i <= world_end; i += 150) {
int left = i, right = min(world_end - 1, i + rand() % 100 + 10),
max_h = 0;
for (int k = left; k <= right; k++)
max_h = max(max_h, height_map[k]);
while (left > 1 && height_map[left - 1] > max_h)
--left;
while (right < world_end - 1 && height_map[right + 1] > max_h)
++right;
for (int k = max(left, 1); k <= min(right, world_end); k++) {
// Do not update height_map until every cell under the new surface is
// filled; otherwise the loop stops after placing only its top block.
int old_h = height_map[k];
int new_h = min(max_h, min(height_map[left - 1] + abs(k - left),
height_map[right + 1] + abs(k - right)));
for (int j = old_h + 1; j <= new_h; ++j)
world[k][j] = block_stone;
height_map[k] = max(old_h, new_h);
}
if (i % progress_step == 0) {
page::progress(page::field, static_cast<int>(100LL * i / world_end));
}
}
// Water and basin corrections precede surface biomes and all trees.
// Forest planning later sees water as a natural biome boundary.
forest_plan.assign(world_end + 1, 0);
forest_prefix.assign(world_end + 1, 0);
shore_used.assign(world_end + 1, 0);
for (int i = 170; i < world_end - 100; i += 12) {
if (rand() % 100 >= 90)
continue;
int left = i;
bool planned_sea = rand() % 100 < 45;
int width = planned_sea ? 50 + rand() % 90 : 10 + rand() % 16;
int right = min(world_end - 6, i + width);
int max_h = 0, min_h = world_height;
for (int k = left; k <= right; ++k)
max_h = max(max_h, height_map[k]), min_h = min(min_h, height_map[k]);
while (planned_sea && left > 6 && right - left < 220 &&
height_map[left - 1] < max_h + 3)
--left, min_h = min(min_h, height_map[left]);
while (planned_sea && right < world_end - 6 && right - left < 220 &&
height_map[right + 1] < max_h + 3)
++right, min_h = min(min_h, height_map[right]);
bool sea = right - left + 1 >= 40;
if (small_sea && (max_h - min_h > 20 || (sea && max_h + 3 < 100)))
continue;
max_h = 0;
bool overlaps_water = false;
for (int x = left; x <= right; ++x) {
max_h = max(max_h, height_map[x]);
int surface = world[x][height_map[x]];
if (shore_used[x] || surface == block_water || surface == block_ice ||
(height_map[x] + 1 < world_height &&
world[x][height_map[x] + 1] == block_water))
overlaps_water = true;
for (int y = height_map[x] + 1;
y <= min(world_height - 1, height_map[x] + 5); ++y)
if (world[x][y] == block_bamboo)
overlaps_water = true;
}
if (overlaps_water)
continue;
// Avoid placing broad seas on the highest mountains.
if (sea && max_h > 120)
continue;
// Use the lower natural bank rather than piling water above it.
int water_y = min(min(world_height - 22, max_h + 3),
min(height_map[left - 1], height_map[right + 1]));
if (water_y < 5)
continue;
// Never partially terraform a candidate if either shore cannot join
// safely.
if (!can_make_shores(left, right, sea, water_y))
continue;
const bool frozen = water_y > 100;
const int top = frozen ? water_y - 1 : water_y;
make_lake_bed(left, right, top);
add_shore(left, right, sea, water_y);
add_cane_pair(left, right);
for (int x = left; x <= right; ++x) {
for (int y = top; y > height_map[x]; --y)
world[x][y] = block_water;
if (frozen)
world[x][top] = block_ice;
height_map[x] = top;
}
if (i % progress_step == 0)
page::progress(page::water, static_cast<int>(100LL * i / world_end));
i = right + 1;
}
// Kelp can grow in any submerged cell except the top liquid-water layer.
for (int x = 1; x <= world_end; ++x) {
int top = height_map[x];
if ((world[x][top] != block_water && world[x][top] != block_ice) ||
snow_hash(x, 0x4b454c50) % 3u != 0u)
continue;
int bed = top;
while (bed > 0 &&
(world[x][bed] == block_water || world[x][bed] == block_ice))
--bed;
int water_top = world[x][top] == block_ice ? top - 1 : top;
int max_height = water_top - bed - 1;
if (max_height < 1 || world[x][bed] != block_sand)
continue;
int height = 1 + static_cast<int>(snow_hash(x, 0x4b4854) %
static_cast<unsigned>(max_height));
for (int h = 1; h <= height; ++h)
world[x][bed + h] = block_kelp;
}
for (int i = 1; i <= world_end; i++) {
if (world[i][height_map[i]] != block_stone)
continue;
int soil_depth = rand() % 3 + 5, desert_roll = rand() % 200,
des_len = rand() % 50 + 10;
int des_end = i;
if (desert_roll == 0) {
const int limit = min(i + des_len, world_end);
while (des_end <= limit &&
world[des_end][height_map[des_end]] == block_stone &&
!shore_used[des_end])
++des_end;
}
if (desert_roll != 0 || des_end - i < 10) {
for (int j = height_map[i]; j >= 1; j--) {
if (j == height_map[i])
world[i][j] = (j > 100 ? 1 : 0); // Grass
else if (abs(j - height_map[i]) < soil_depth)
world[i][j] = block_dirt; // Dirt
else
world[i][j] = block_stone; // Stone
}
} else {
for (int k = i; k < des_end; k++) {
// Desert stretches must not overwrite existing lakes or shores.
if (world[k][height_map[k]] != block_stone)
continue;
for (int j = height_map[k]; j >= 1; j--) {
if (height_map[k] == j && j > 100)
place_snowy_sand(k, j);
else if (height_map[k] - j < 5 - rand() % 2)
world[k][j] = (height_map[k] - j > 3 ? 20 : 12); // Sand
else if (abs(j - height_map[k]) < soil_depth)
world[k][j] = block_gravel; // Gravel
else
world[k][j] = block_stone; // Stone
}
}
}
if (i % progress_step == 0) {
page::progress(page::dirt, static_cast<int>(100LL * i / world_end));
}
}
plan_forests();
int tree_gap = rand() % 15 + 5, tree_count = 0;
int tree_progress = -1;
int next_plain_tree = 10;
vector<oak_site> cherry_sites;
for (int i = 10; i <= world_end; i += tree_gap) {
const int ground_y = height_map[i];
const int kind = forest_plan[i];
const bool plain_tree = kind == 0 && world[i][ground_y] == block_grass &&
i >= next_plain_tree;
tree_gap = kind == 0 ? 1 : 4 + rand() % 2;
++tree_count;
if (kind == 2 && world[i][ground_y] == block_grass &&
world[i][ground_y + 1] != block_water) {
cherry_sites.push_back({i, ground_y, i / 128});
} else if (world[i][ground_y] == block_sand && !near_cherry(i, 2) &&
!near_water(i, 5) && snow_hash(i, 0x4343) % 7u == 0 &&
world[i][ground_y + 1] != block_water) { // cactus
int plant_h = rand() % 3 + 5;
for (int j = 1; j <= plant_h; j++) {
if (plant_h - j < 1)
;
else if (j > 2 && rand() % 2 && j % 2)
world[i - 1][ground_y + j] = block_cactus;
else if (j > 2 && rand() % 2 && j % 2)
world[i + 1][ground_y + j] = block_cactus;
world[i][ground_y + j] = block_cactus;
} // Cactus
int top = ground_y + plant_h;
if (top + 1 < world_height && world[i][top + 1] == block_air &&
snow_hash(i, top) % 10u < 3u)
world[i][top + 1] = block_cactus_flower;
} else if (kind == 3 && ground_y > 100 && !near_cherry(i, 6) &&
tree_site_clear(i, ground_y, 6)) { // high
int tree_h = 9 + static_cast<int>(snow_hash(i, 0x5350) % 8u);
int trunk_h = rand() % 2 + 1;
for (int j = 1; j <= tree_h; j++) {
const int existing = world[i][ground_y + j];
// Neighboring leaves used to truncate spruce trunks.
if ((existing > block_dirt && !is_tree_block(existing)) ||
world[i][ground_y] == block_little_snow)
break;
world[i][ground_y + j] = block_spruce_wood;
if (j > trunk_h && j % 2) {
int radius = min(6, (tree_h + 1 - j) / 2);
place_symmetric_branches(i, ground_y + j, radius, block_high_leaf_0,
(snow_hash(i, 0x534e) & 1u) != 0);
}
if (j > trunk_h)
world[i][ground_y + j] = block_high_leaf_1; // HighLeaf_1
}
} else if ((kind == 1 || plain_tree) &&
world[i][ground_y] == block_grass && !near_cherry(i, 6) &&
tree_site_clear(i, ground_y, 3)) {
if (plain_tree)
next_plain_tree =
i + 15 + static_cast<int>(snow_hash(i, 0x504c) % 6u);
if (snow_hash(i, 0x4f414b) % 10u < 3u &&
can_grow_big_oak(i, ground_y)) {
grow_big_oak(i, ground_y);
} else {
int tree_h = 6 + rand() % 3, trunk_h = rand() % 2 + 1;
for (int j = 1; j <= tree_h; j++) {
if (world[i][ground_y + j] == block_water)
break;
world[i][ground_y + j] = block_wood; // Wood
if (j > trunk_h)
place_symmetric_branches(i, ground_y + j, j < tree_h ? 2 : 1,
block_leaf_0);
if (j > trunk_h)
world[i][ground_y + j] = block_leaf_1;
}
}
}
const int percent = static_cast<int>(100LL * i / world_end);
if (percent / 5 != tree_progress) {
tree_progress = percent / 5;
page::progress(page::tree, percent);
}
}
make_cherry_forests(cherry_sites);
add_spruce_snow();
int cloud_progress = -1;
for (int i = 10; i < world_end - 100; i += rand() % 20 + 15) {
int j = rand() % 20 + 110;
if (rand() % 2) {
for (int k = i; k <= i + rand() % 10 + 10; ++k) {
if (can_place_cloud(k, j))
world[k][j] = block_cloud;
if (can_place_cloud(k + 2, j - 1))
world[k + 2][j - 1] = block_cloud;
}
} else {
for (int k = i; k <= i + rand() % 10 + 10; ++k) {
if (can_place_cloud(k - 2, j + 1))
world[k - 2][j + 1] = block_cloud;
if (can_place_cloud(k, j))
world[k][j] = block_cloud;
}
}
const int percent = static_cast<int>(100LL * i / world_end);
if (percent / 5 != cloud_progress) {
cloud_progress = percent / 5;
page::progress(page::cloud, percent);
}
}
for (int i = 1; i <= world_end; i++) {
for (int j = height_map[i];
world[i][j] == block_water || world[i][j] == block_sand && j <= 256;
j++) {
height_map[i] = j;
}
}
}
for (int i = 1; i <= world_end; i++) {
for (int j = height_map[i]; j >= 1; j--) {
if (world[i][j] == block_stone) {
int roll = ((rand() % 100000) * rand()) % 1000000;
if (roll < 5000) { // up
if (j < 20)
continue;
roll = rand() % 2;
if (roll == 0) {
fill_stone(i - 1, j, block_coal);
fill_stone(i - 2, j, block_coal);
fill_stone(i, j - 1, block_coal);
fill_stone(i - 1, j - 1, block_coal);
fill_stone(i - 2, j - 1, block_coal);
fill_stone(i - 3, j - 1, block_coal);
fill_stone(i - 1, j - 2, block_coal);
fill_stone(i - 2, j - 2, block_coal);
} else {
fill_stone(i - 1, j, block_coal);
fill_stone(i - 2, j, block_coal);
fill_stone(i, j - 1, block_coal);
fill_stone(i - 2, j - 1, block_coal);
fill_stone(i - 1, j - 2, block_coal);
fill_stone(i - 2, j - 2, block_coal);
fill_stone(i - 2, j - 3, block_coal);
fill_stone(i - 1, j - 3, block_coal);
fill_stone(i, j - 3, block_coal);
fill_stone(i + 1, j - 3, block_coal);
}
} else if (roll < 5000 + 2000) {
roll = rand() % 2;
if (roll == 0) {
fill_stone(i - 1, j, block_iron);
fill_stone(i - 2, j, block_iron);
fill_stone(i, j - 1, block_iron);
fill_stone(i - 1, j - 1, block_iron);
fill_stone(i - 2, j - 1, block_iron);
fill_stone(i - 3, j - 1, block_iron);
} else {
fill_stone(i - 1, j, block_iron);
fill_stone(i - 2, j, block_iron);
fill_stone(i, j - 1, block_iron);
fill_stone(i - 2, j - 1, block_iron);
fill_stone(i - 1, j - 2, block_iron);
fill_stone(i - 2, j - 2, block_iron);
}
} else if (roll < 5000 + 2000 + 1000) {
fill_stone(i, j, block_gold);
fill_stone(i - 1, j, block_gold);
fill_stone(i, j - 1, block_gold);
roll = rand() % 2;
if (roll == 0) {
fill_stone(i + 1, j - 1, block_gold);
} else {
fill_stone(i - 1, j - 1, block_gold);
}
} else if (roll < 5000 + 2000 + 1000 + 500) {
if (j > 30)
continue; // deep
fill_stone(i, j, block_diamond);
fill_stone(i - 1, j, block_diamond);
fill_stone(i, j - 1, block_diamond);
roll = rand() % 2;
if (roll == 0) {
fill_stone(i + 1, j - 1, block_diamond);
} else {
fill_stone(i - 1, j - 1, block_diamond);
}
} else if (roll < 5000 + 2000 + 1000 + 500 + 700) {
if (j < 64)
continue; // mountains
fill_stone(i, j, block_ruby);
if (rand() % 2)
fill_stone(i, j - 1, block_ruby);
}
}
}
}
for (int i = 1; i <= world_end; i += 100) {
int j = rand() % 30 + 5;
if (world[i][j] == block_stone)
make_hole(i, j, rand() % 8 + 5);
if (i % progress_step == 0) {
page::progress(page::minehole, static_cast<int>(100LL * i / world_end));
}
}
cam_pos =
coord::to_world(0, height_map[coord::origin_x] - coord::origin_y + 5);
player_x = cam_pos.first, player_y = cam_pos.second;
page::complete();
}
// Player movement and physics.
void player_fall() {
// Original horizontal collision can leave the feet inside a block on a slope.
// Preserve the step-up behavior while keeping indices in range.
while (player_y < world_height - 2 && is_solid(player_x, player_y))
++player_y;
// In water, keep a small downward velocity unless actively swimming up.
if (is_water_block(world[player_x][player_y])) {
if (dive_down) {
fall_sp = -0.48;
if (fall_rem > 0)
fall_rem = 0;
} else if (fall_sp <= 0) {
fall_sp = -0.14;
}
}
fall_rem += fall_sp;
while (fall_rem >= 1.0) {
// The player occupies the feet tile and the tile above it.
if (player_y + 2 >= world_height || is_solid(player_x, player_y + 2)) {
fall_sp = 0;
fall_rem = 0;
break;
}
++player_y;
fall_rem -= 1.0;
}
while (fall_rem <= -1.0) {
if (player_y <= 1 || is_solid(player_x, player_y - 1)) {
fall_sp = 0;
fall_rem = 0;
break;
}
--player_y;
fall_rem += 1.0;
if (is_water_block(world[player_x][player_y]))
fall_sp = dive_down ? -0.48 : -0.14;
}
if (fall_sp <= 0 && (player_y <= 1 || is_solid(player_x, player_y - 1))) {
fall_sp = 0;
fall_rem = 0;
} else {
fall_sp = max(-max_fall_sp, fall_sp - gravity);
}
}
void player_jump() {
if (is_water_block(world[player_x][player_y])) {
fall_sp = swim_sp;
} else if (fall_sp <= 0 && player_y > 0 && is_solid(player_x, player_y - 1)) {
fall_sp = jump_sp;
fall_rem = 0;
}
}
// Session setup and main loop.
void new_game() {
flat_world = 0; // flat
sun_water = 0; // strange sun
game_started = 0;
show_body = 1; // body
fly_mode = 0; // fly
time_on = 1; // time
small_sea = 0; // small sea
work_mode = 0; // work mode
seed = get_seed(-1); // seed
page::loading_scene(page::ground, 0);
memset(world, -1, sizeof(world)); // Air
gen_world();
reset_clock(); // Generation time is not counted as game time.
}
// Time between player movement steps. Smaller values move faster.
// Physics and rendering continue at their existing frame rate.
const int player_move_ms = 43;
bool allow_move(bool pressed, int interval_ms) {
using clock = std::chrono::steady_clock;
static clock::time_point last_move;
static bool held = false;
if (!pressed) {
held = false;
return false;
}
const auto now = clock::now();
if (!held ||
std::chrono::duration_cast<std::chrono::milliseconds>(now - last_move)
.count() >= interval_ms) {
last_move = now;
held = true;
return true;
}
return false;
}
int move_interval() {
// Preserve the original ice/water speed relationship.
if (world[player_x][player_y - 1] == block_ice)
return player_move_ms / 2;
if (is_water_block(world[player_x][player_y]))
return player_move_ms * 2;
return player_move_ms;
}
// A double press of the same jump key toggles flight once; holding a key
// never repeats the toggle. The three keys have independent timers.
bool double_tap(int index, bool down) {
using clock = std::chrono::steady_clock;
static bool was_down[3] = {false, false, false};
static bool has_press[3] = {false, false, false};
static clock::time_point last_press[3];
bool triggered = false;
if (down && !was_down[index]) {
auto now = clock::now();
if (has_press[index] &&
std::chrono::duration_cast<std::chrono::milliseconds>(now -
last_press[index])
.count() <= 300) {
triggered = true;
has_press[index] = false;
} else {
last_press[index] = now;
has_press[index] = true;
}
}
was_down[index] = down;
return triggered;
}
// A frame keeps the original render, input, physics and timing order.
void update_frame() {
const auto frame_start = std::chrono::steady_clock::now();
draw_world();
page::status();
prev_x = player_x, prev_y = player_y;
prev_cam = cam_pos;
const bool key_space = KEY_DOWN(32), key_up = KEY_DOWN(38),
key_w = KEY_DOWN('W');
// Bitwise OR evaluates each detector so all key states are updated.
const bool fly_toggle =
double_tap(0, key_space) | double_tap(1, key_up) | double_tap(2, key_w);
if (show_body && fly_toggle) {
fly_mode = !fly_mode;
fall_sp = 0;
fall_rem = 0;
}
const bool jump_down = key_space || key_up || key_w;
dive_down = !fly_mode && (KEY_DOWN(40) || KEY_DOWN('S'));
if (fly_mode && show_body) {
const bool up = jump_down, down = KEY_DOWN(40) || KEY_DOWN('S');
const bool right = KEY_DOWN(39) || KEY_DOWN('D'),
left = KEY_DOWN(37) || KEY_DOWN('A');
if (allow_move(up || down || right || left, 30)) {
if (up)
cam_pos.second++, player_y++;
if (down)
cam_pos.second--, player_y--;
if (right)
cam_pos.first++, player_x++;
if (left)
cam_pos.first--, player_x--;
}
} else if (!show_body) {
if ((KEY_DOWN(38) || KEY_DOWN('W')))
cam_pos.second++;
if ((KEY_DOWN(40) || KEY_DOWN('S')))
cam_pos.second--;
if ((KEY_DOWN(39) || KEY_DOWN('D')))
cam_pos.first++;
if ((KEY_DOWN(37) || KEY_DOWN('A')))
cam_pos.first--;
} else {
// Holding Up automatically jumps again when the player lands.
if (jump_down)
player_jump();
const bool right = KEY_DOWN(39) || KEY_DOWN('D'),
left = KEY_DOWN(37) || KEY_DOWN('A');
if (allow_move(right || left, move_interval())) {
if (right && !is_solid(player_x + 1, player_y + 1))
player_x++;
if (left && !is_solid(player_x - 1, player_y + 1))
player_x--;
}
player_fall();
cam_pos.first = player_x, cam_pos.second = player_y;
}
const int view_margin = 15;
cam_pos.first = max(view_margin, min(world_end - view_margin, cam_pos.first));
if (show_body)
player_x = max(view_margin, min(world_end - view_margin, player_x));
if (world[player_x][player_y] > block_air && is_solid(player_x, player_y) &&
show_body)
player_x = prev_x, player_y = prev_y, cam_pos = prev_cam;
// Update facing only after an actual, non-reverted horizontal movement.
if (show_body && player_x != prev_x)
face_right = player_x > prev_x;
(cam_pos.second += world_height) %= world_height;
int target_ms = frame_wait;
if (show_body && world[player_x][player_y - 1] == block_ice)
target_ms = frame_wait / 2;
else if (show_body && is_water_block(world[player_x][player_y]))
target_ms = frame_wait * 2;
// Rendering time counts toward the frame budget instead of adding latency.
const auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - frame_start)
.count();
if (elapsed_ms < target_ms)
Sleep(static_cast<DWORD>(target_ms - elapsed_ms));
(player_y += world_height) %= world_height;
tick_clock();
}
signed run_game() {
if (!enable_rgb()) {
cerr << "This program requires a Windows console with UTF-8 and virtual "
"terminal support.\n";
return 1;
}
system(("title " + game_name + " v" + version).c_str());
enter_screen();
hide_cursor();
new_game();
while (1) {
update_frame();
}
return 0;
}
} // namespace craft
int main() { return craft::run_game(); }
剪贴板
剪贴板 lj9cyod5
剪贴板信息
剪贴板信息
- 发布
- 收录
- 更新