Add celseed: Minecraft seed finder with CEL filter expressions

Single-file C CLI program that searches Minecraft seeds using cubiomes
for biome/structure generation and a built-in CEL-subset expression
evaluator for filtering. Supports all MC versions from Beta 1.7 to 26.3.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Arigadam 2026-10-13 09:27:20 +00:00
commit 843f1ce8f3
8 changed files with 1286 additions and 0 deletions

964
celseed.c Normal file
View file

@ -0,0 +1,964 @@
/*
* celseed - Minecraft seed finder using cubiomes + CEL filter expressions
*
* Filter syntax (subset of Common Expression Language):
* Literals: 42, 3.14, "text", true, false
* Variable: seed
* Operators: && || ! == != < <= > >= + - * / %
* Functions: biome(x,z) biome_id(x,z) has_biome(name,x,z,r)
* structure(type,rx,rz) struct_x(type,rx,rz) struct_z(type,rx,rz)
* spawn_x() spawn_z()
*
* MIT License – see LICENSE
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <time.h>
#include "cubiomes/generator.h"
#include "cubiomes/finders.h"
#include "cubiomes/biomes.h"
#include "cubiomes/util.h"
/* ============================================================
* Value type
* ========================================================== */
typedef enum { VT_NULL, VT_INT, VT_FLOAT, VT_STR, VT_BOOL, VT_ERR } VType;
typedef struct {
VType type;
int64_t i;
double d;
bool b;
const char *s; /* static string or points into sbuf */
char sbuf[64];
} Val;
static Val vint(int64_t v) { Val r; memset(&r,0,sizeof(r)); r.type=VT_INT; r.i=v; return r; }
static Val vfloat(double v) { Val r; memset(&r,0,sizeof(r)); r.type=VT_FLOAT; r.d=v; return r; }
static Val vbool(bool v) { Val r; memset(&r,0,sizeof(r)); r.type=VT_BOOL; r.b=v; return r; }
static Val vstr(const char *s) { Val r; memset(&r,0,sizeof(r)); r.type=VT_STR; r.s=s; return r; }
static Val verr(const char *m) { Val r; memset(&r,0,sizeof(r)); r.type=VT_ERR; r.s=m; return r; }
static bool vtrue(Val v) {
switch (v.type) {
case VT_BOOL: return v.b;
case VT_INT: return v.i != 0;
case VT_FLOAT: return v.d != 0.0;
case VT_STR: return v.s && v.s[0];
default: return false;
}
}
static int vcmp(Val a, Val b) {
if (a.type == VT_INT && b.type == VT_INT)
return (a.i > b.i) - (a.i < b.i);
if ((a.type == VT_INT || a.type == VT_FLOAT) &&
(b.type == VT_INT || b.type == VT_FLOAT)) {
double da = (a.type == VT_INT) ? (double)a.i : a.d;
double db = (b.type == VT_INT) ? (double)b.i : b.d;
return (da > db) - (da < db);
}
if (a.type == VT_STR && b.type == VT_STR)
return strcmp(a.s, b.s);
if (a.type == VT_BOOL && b.type == VT_BOOL)
return (int)a.b - (int)b.b;
return -2;
}
static bool veq(Val a, Val b) {
if (a.type == VT_STR && b.type == VT_STR)
return strcmp(a.s, b.s) == 0;
return vcmp(a, b) == 0;
}
/* ============================================================
* Lexer
* ========================================================== */
typedef enum {
TK_EOF, TK_INT, TK_FLOAT, TK_STR, TK_IDENT,
TK_PLUS, TK_MINUS, TK_STAR, TK_SLASH, TK_PCT,
TK_EQ, TK_NEQ, TK_LT, TK_LE, TK_GT, TK_GE,
TK_AND, TK_OR, TK_BANG,
TK_LP, TK_RP, TK_COMMA,
TK_ERR
} TKind;
#define SMAX 256
typedef struct {
TKind k;
int64_t ival;
double dval;
char sval[SMAX];
} Tok;
typedef struct {
const char *src;
int pos;
bool peeked;
Tok peek;
char err[SMAX];
} Lex;
static void lex_init(Lex *l, const char *src) {
l->src = src;
l->pos = 0;
l->peeked = false;
l->err[0] = '\0';
}
static Tok lex_read(Lex *l) {
Tok t; t.sval[0] = '\0';
const char *s = l->src;
while (s[l->pos] == ' ' || s[l->pos] == '\t') l->pos++;
char c = s[l->pos];
if (!c) { t.k = TK_EOF; return t; }
if (c == '"') {
l->pos++;
int i = 0;
while (s[l->pos] && s[l->pos] != '"' && i < SMAX-1)
t.sval[i++] = s[l->pos++];
t.sval[i] = '\0';
if (s[l->pos] == '"') l->pos++;
t.k = TK_STR;
return t;
}
if (c >= '0' && c <= '9') {
char *end;
t.ival = strtoll(s + l->pos, &end, 10);
if (*end == '.' || *end == 'e' || *end == 'E') {
t.dval = strtod(s + l->pos, &end);
t.k = TK_FLOAT;
} else {
t.k = TK_INT;
}
l->pos = (int)(end - s);
return t;
}
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_') {
int i = 0;
while ((s[l->pos] >= 'a' && s[l->pos] <= 'z') ||
(s[l->pos] >= 'A' && s[l->pos] <= 'Z') ||
(s[l->pos] >= '0' && s[l->pos] <= '9') ||
s[l->pos] == '_')
t.sval[i++] = s[l->pos++];
t.sval[i] = '\0';
t.k = TK_IDENT;
return t;
}
l->pos++;
switch (c) {
case '+': t.k = TK_PLUS; break;
case '-': t.k = TK_MINUS; break;
case '*': t.k = TK_STAR; break;
case '/': t.k = TK_SLASH; break;
case '%': t.k = TK_PCT; break;
case '(': t.k = TK_LP; break;
case ')': t.k = TK_RP; break;
case ',': t.k = TK_COMMA; break;
case '!':
if (s[l->pos]=='=') { l->pos++; t.k=TK_NEQ; }
else t.k = TK_BANG;
break;
case '<':
if (s[l->pos]=='=') { l->pos++; t.k=TK_LE; }
else t.k = TK_LT;
break;
case '>':
if (s[l->pos]=='=') { l->pos++; t.k=TK_GE; }
else t.k = TK_GT;
break;
case '=':
if (s[l->pos]=='=') { l->pos++; t.k=TK_EQ; }
else { snprintf(l->err,SMAX,"use '==' not '='"); t.k=TK_ERR; }
break;
case '&':
if (s[l->pos]=='&') { l->pos++; t.k=TK_AND; }
else { snprintf(l->err,SMAX,"use '&&' not '&'"); t.k=TK_ERR; }
break;
case '|':
if (s[l->pos]=='|') { l->pos++; t.k=TK_OR; }
else { snprintf(l->err,SMAX,"use '||' not '|'"); t.k=TK_ERR; }
break;
default:
snprintf(l->err, SMAX, "unexpected char '%c'", c);
t.k = TK_ERR;
break;
}
return t;
}
static Tok lpeek(Lex *l) {
if (!l->peeked) { l->peek = lex_read(l); l->peeked = true; }
return l->peek;
}
static Tok lconsume(Lex *l) {
Tok t = lpeek(l);
l->peeked = false;
return t;
}
/* ============================================================
* AST
* ========================================================== */
typedef enum {
ND_INT, ND_FLOAT, ND_STR, ND_BOOL, ND_VAR, ND_CALL,
ND_NEG, ND_NOT,
ND_MUL, ND_DIV, ND_MOD,
ND_ADD, ND_SUB,
ND_LT, ND_LE, ND_GT, ND_GE,
ND_EQ, ND_NEQ,
ND_AND, ND_OR,
} NKind;
#define MAX_NODES 512
#define MAX_ARGS 8
typedef struct Node Node;
struct Node {
NKind k;
int64_t ival;
double dval;
bool bval;
char name[SMAX];
Node *lhs, *rhs;
Node *args[MAX_ARGS];
int nargs;
};
typedef struct {
Node pool[MAX_NODES];
int used;
char err[SMAX];
Lex lex;
} Parser;
static Node *palloc(Parser *p) {
if (p->used >= MAX_NODES) {
snprintf(p->err, SMAX, "expression too complex");
return NULL;
}
Node *n = &p->pool[p->used++];
memset(n, 0, sizeof(*n));
return n;
}
static Node *parse_expr(Parser *p);
static Node *parse_or(Parser *p);
static Node *parse_and(Parser *p);
static Node *parse_eq(Parser *p);
static Node *parse_cmp(Parser *p);
static Node *parse_add(Parser *p);
static Node *parse_mul(Parser *p);
static Node *parse_unary(Parser *p);
static Node *parse_primary(Parser *p);
static Node *parse_expr(Parser *p) { return parse_or(p); }
static Node *parse_or(Parser *p) {
Node *n = parse_and(p); if (!n) return NULL;
while (lpeek(&p->lex).k == TK_OR) {
lconsume(&p->lex);
Node *r = parse_and(p); if (!r) return NULL;
Node *op = palloc(p); if (!op) return NULL;
op->k = ND_OR; op->lhs = n; op->rhs = r; n = op;
}
return n;
}
static Node *parse_and(Parser *p) {
Node *n = parse_eq(p); if (!n) return NULL;
while (lpeek(&p->lex).k == TK_AND) {
lconsume(&p->lex);
Node *r = parse_eq(p); if (!r) return NULL;
Node *op = palloc(p); if (!op) return NULL;
op->k = ND_AND; op->lhs = n; op->rhs = r; n = op;
}
return n;
}
static Node *parse_eq(Parser *p) {
Node *n = parse_cmp(p); if (!n) return NULL;
for (;;) {
TKind k = lpeek(&p->lex).k;
if (k != TK_EQ && k != TK_NEQ) break;
lconsume(&p->lex);
Node *r = parse_cmp(p); if (!r) return NULL;
Node *op = palloc(p); if (!op) return NULL;
op->k = (k == TK_EQ) ? ND_EQ : ND_NEQ;
op->lhs = n; op->rhs = r; n = op;
}
return n;
}
static Node *parse_cmp(Parser *p) {
Node *n = parse_add(p); if (!n) return NULL;
for (;;) {
TKind k = lpeek(&p->lex).k;
if (k!=TK_LT && k!=TK_LE && k!=TK_GT && k!=TK_GE) break;
lconsume(&p->lex);
Node *r = parse_add(p); if (!r) return NULL;
Node *op = palloc(p); if (!op) return NULL;
switch (k) {
case TK_LT: op->k=ND_LT; break;
case TK_LE: op->k=ND_LE; break;
case TK_GT: op->k=ND_GT; break;
default: op->k=ND_GE; break;
}
op->lhs = n; op->rhs = r; n = op;
}
return n;
}
static Node *parse_add(Parser *p) {
Node *n = parse_mul(p); if (!n) return NULL;
for (;;) {
TKind k = lpeek(&p->lex).k;
if (k!=TK_PLUS && k!=TK_MINUS) break;
lconsume(&p->lex);
Node *r = parse_mul(p); if (!r) return NULL;
Node *op = palloc(p); if (!op) return NULL;
op->k = (k==TK_PLUS) ? ND_ADD : ND_SUB;
op->lhs = n; op->rhs = r; n = op;
}
return n;
}
static Node *parse_mul(Parser *p) {
Node *n = parse_unary(p); if (!n) return NULL;
for (;;) {
TKind k = lpeek(&p->lex).k;
if (k!=TK_STAR && k!=TK_SLASH && k!=TK_PCT) break;
lconsume(&p->lex);
Node *r = parse_unary(p); if (!r) return NULL;
Node *op = palloc(p); if (!op) return NULL;
switch (k) {
case TK_STAR: op->k=ND_MUL; break;
case TK_SLASH: op->k=ND_DIV; break;
default: op->k=ND_MOD; break;
}
op->lhs = n; op->rhs = r; n = op;
}
return n;
}
static Node *parse_unary(Parser *p) {
TKind k = lpeek(&p->lex).k;
if (k == TK_BANG || k == TK_MINUS) {
lconsume(&p->lex);
Node *op = palloc(p); if (!op) return NULL;
op->k = (k == TK_BANG) ? ND_NOT : ND_NEG;
op->lhs = parse_unary(p);
if (!op->lhs) return NULL;
return op;
}
return parse_primary(p);
}
static Node *parse_primary(Parser *p) {
Tok t = lconsume(&p->lex);
Node *n;
switch (t.k) {
case TK_INT:
n = palloc(p); if (!n) return NULL;
n->k = ND_INT; n->ival = t.ival;
return n;
case TK_FLOAT:
n = palloc(p); if (!n) return NULL;
n->k = ND_FLOAT; n->dval = t.dval;
return n;
case TK_STR:
n = palloc(p); if (!n) return NULL;
n->k = ND_STR;
strncpy(n->name, t.sval, SMAX-1);
return n;
case TK_IDENT:
if (!strcmp(t.sval,"true")) { n=palloc(p); if(!n) return NULL; n->k=ND_BOOL; n->bval=true; return n; }
if (!strcmp(t.sval,"false")) { n=palloc(p); if(!n) return NULL; n->k=ND_BOOL; n->bval=false; return n; }
if (lpeek(&p->lex).k == TK_LP) {
lconsume(&p->lex);
n = palloc(p); if (!n) return NULL;
n->k = ND_CALL;
strncpy(n->name, t.sval, SMAX-1);
if (lpeek(&p->lex).k != TK_RP) {
for (;;) {
if (n->nargs >= MAX_ARGS) {
snprintf(p->err, SMAX, "too many args to %s()", n->name);
return NULL;
}
Node *a = parse_expr(p); if (!a) return NULL;
n->args[n->nargs++] = a;
if (lpeek(&p->lex).k != TK_COMMA) break;
lconsume(&p->lex);
}
}
if (lpeek(&p->lex).k != TK_RP) {
snprintf(p->err, SMAX, "expected ')' after args to %s()", n->name);
return NULL;
}
lconsume(&p->lex);
return n;
}
n = palloc(p); if (!n) return NULL;
n->k = ND_VAR;
strncpy(n->name, t.sval, SMAX-1);
return n;
case TK_LP: {
Node *inner = parse_expr(p); if (!inner) return NULL;
if (lpeek(&p->lex).k != TK_RP) {
snprintf(p->err, SMAX, "expected ')'");
return NULL;
}
lconsume(&p->lex);
return inner;
}
default:
snprintf(p->err, SMAX, "unexpected token (kind=%d sval='%s')", t.k, t.sval);
return NULL;
}
}
/* ============================================================
* Evaluation context
* ========================================================== */
typedef struct {
uint64_t seed;
int mc;
int dim;
uint32_t flags;
Generator *gen;
bool gen_ready;
bool spawn_ready;
Pos spawn;
} Ctx;
static void ensure_gen(Ctx *c) {
if (!c->gen_ready) {
applySeed(c->gen, c->dim, c->seed);
c->gen_ready = true;
}
}
static Val eval_node(const Node *n, Ctx *ctx);
/* ============================================================
* Built-in functions
* ========================================================== */
/* strip optional "minecraft:" prefix for comparison */
static const char *strip_ns(const char *s) {
if (s && strncmp(s, "minecraft:", 10) == 0) return s + 10;
return s;
}
static bool biome_name_eq(const char *query, const char *result) {
if (!result) return false;
return strcasecmp(strip_ns(query), strip_ns(result)) == 0;
}
static bool arg_int(const Node *n, Ctx *ctx, int idx, int *out) {
Val v = eval_node(n->args[idx], ctx);
if (v.type == VT_INT) { *out = (int)v.i; return true; }
if (v.type == VT_FLOAT) { *out = (int)v.d; return true; }
return false;
}
static Val fn_biome(const Node *n, Ctx *ctx) {
if (n->nargs != 2) return verr("biome(x,z) takes 2 args");
int x, z;
if (!arg_int(n,ctx,0,&x) || !arg_int(n,ctx,1,&z))
return verr("biome() args must be numbers");
ensure_gen(ctx);
int id = getBiomeAt(ctx->gen, 4, x>>2, 0, z>>2);
if (id < 0) return vstr("none");
const char *nm = biome2str(ctx->mc, id);
return vstr(nm ? strip_ns(nm) : "unknown");
}
static Val fn_biome_id(const Node *n, Ctx *ctx) {
if (n->nargs != 2) return verr("biome_id(x,z) takes 2 args");
int x, z;
if (!arg_int(n,ctx,0,&x) || !arg_int(n,ctx,1,&z))
return verr("biome_id() args must be numbers");
ensure_gen(ctx);
int id = getBiomeAt(ctx->gen, 4, x>>2, 0, z>>2);
return vint(id);
}
static Val fn_has_biome(const Node *n, Ctx *ctx) {
if (n->nargs != 4) return verr("has_biome(name,x,z,r) takes 4 args");
int cx, cz, cr;
if (!arg_int(n,ctx,1,&cx) || !arg_int(n,ctx,2,&cz) || !arg_int(n,ctx,3,&cr))
return verr("has_biome() args 2,3,4 must be numbers");
Val bval = eval_node(n->args[0], ctx);
int target = -1;
if (bval.type == VT_INT) {
target = (int)bval.i;
} else if (bval.type == VT_STR) {
/* search by name across all valid IDs */
for (int id = 0; id < 256; id++) {
const char *nm = biome2str(ctx->mc, id);
if (biome_name_eq(bval.s, nm)) { target = id; break; }
}
/* fallback numeric lookup table for pre-1.13 */
if (target < 0) {
static const struct { const char *name; int id; } tbl[] = {
{"ocean",0},{"plains",1},{"desert",2},{"mountains",3},
{"forest",4},{"taiga",5},{"swamp",6},{"river",7},
{"frozen_ocean",10},{"frozen_river",11},{"snowy_tundra",12},
{"snowy_mountains",13},{"mushroom_fields",14},{"beach",16},
{"desert_hills",17},{"wooded_hills",18},{"taiga_hills",19},
{"jungle",21},{"jungle_hills",22},{"jungle_edge",23},
{"deep_ocean",24},{"stone_shore",25},{"snowy_beach",26},
{"birch_forest",27},{"birch_forest_hills",28},{"dark_forest",29},
{"snowy_taiga",30},{"snowy_taiga_hills",31},
{"giant_tree_taiga",32},{"giant_tree_taiga_hills",33},
{"wooded_mountains",34},{"savanna",35},{"savanna_plateau",36},
{"badlands",37},{"wooded_badlands_plateau",38},
{"badlands_plateau",39},{"warm_ocean",44},{"lukewarm_ocean",45},
{"cold_ocean",46},{"deep_warm_ocean",47},
{"deep_lukewarm_ocean",48},{"deep_cold_ocean",49},
{"deep_frozen_ocean",50},
{NULL,0}
};
for (int i = 0; tbl[i].name; i++)
if (strcasecmp(tbl[i].name, bval.s)==0) { target=tbl[i].id; break; }
}
if (target < 0) return verr("has_biome(): unknown biome name");
} else {
return verr("has_biome() arg 1 must be string or int");
}
ensure_gen(ctx);
int r4 = (cr >> 2) + 1;
int cx4 = cx >> 2, cz4 = cz >> 2;
int step = (r4 / 32) + 1;
for (int z4 = cz4-r4; z4 <= cz4+r4; z4 += step) {
for (int x4 = cx4-r4; x4 <= cx4+r4; x4 += step) {
if (getBiomeAt(ctx->gen, 4, x4, 0, z4) == target)
return vbool(true);
}
}
return vbool(false);
}
static int parse_stype(const char *s) {
static const struct { const char *n; int v; } map[] = {
{"desert_pyramid",Desert_Pyramid},{"jungle_temple",Jungle_Temple},
{"swamp_hut",Swamp_Hut},{"igloo",Igloo},{"village",Village},
{"ocean_ruin",Ocean_Ruin},{"shipwreck",Shipwreck},
{"monument",Monument},{"mansion",Mansion},{"outpost",Outpost},
{"ruined_portal",Ruined_Portal},{"ancient_city",Ancient_City},
{"treasure",Treasure},{"fortress",Fortress},{"bastion",Bastion},
{"end_city",End_City},{"stronghold",Stronghold},
{"trail_ruins",Trail_Ruins},{"trial_chambers",Trial_Chambers},
{NULL,0}
};
for (int i = 0; map[i].n; i++)
if (strcasecmp(map[i].n, s)==0) return map[i].v;
return -1;
}
static Val fn_structure(const Node *n, Ctx *ctx) {
if (n->nargs != 3) return verr("structure(type,rx,rz) takes 3 args");
Val tv = eval_node(n->args[0], ctx);
if (tv.type != VT_STR) return verr("structure() arg 1 must be string");
int stype = parse_stype(tv.s);
if (stype < 0) return verr("structure(): unknown structure type");
int rx, rz;
if (!arg_int(n,ctx,1,&rx) || !arg_int(n,ctx,2,&rz))
return verr("structure() args 2,3 must be numbers");
StructureConfig sc;
if (!getStructureConfig(stype, ctx->mc, &sc)) return vbool(false);
Pos pos;
if (!getStructurePos(stype, ctx->mc, ctx->seed, rx, rz, &pos)) return vbool(false);
ensure_gen(ctx);
return vbool(isViableStructurePos(stype, ctx->gen, pos.x, pos.z, 0) != 0);
}
static Val fn_struct_x(const Node *n, Ctx *ctx) {
if (n->nargs != 3) return verr("struct_x(type,rx,rz) takes 3 args");
Val tv = eval_node(n->args[0], ctx);
if (tv.type != VT_STR) return verr("struct_x() arg 1 must be string");
int stype = parse_stype(tv.s);
if (stype < 0) return verr("struct_x(): unknown structure type");
int rx, rz;
if (!arg_int(n,ctx,1,&rx) || !arg_int(n,ctx,2,&rz))
return verr("struct_x() args 2,3 must be numbers");
StructureConfig sc;
if (!getStructureConfig(stype, ctx->mc, &sc)) return vint(0);
Pos pos;
if (!getStructurePos(stype, ctx->mc, ctx->seed, rx, rz, &pos)) return vint(0);
return vint(pos.x);
}
static Val fn_struct_z(const Node *n, Ctx *ctx) {
if (n->nargs != 3) return verr("struct_z(type,rx,rz) takes 3 args");
Val tv = eval_node(n->args[0], ctx);
if (tv.type != VT_STR) return verr("struct_z() arg 1 must be string");
int stype = parse_stype(tv.s);
if (stype < 0) return verr("struct_z(): unknown structure type");
int rx, rz;
if (!arg_int(n,ctx,1,&rx) || !arg_int(n,ctx,2,&rz))
return verr("struct_z() args 2,3 must be numbers");
StructureConfig sc;
if (!getStructureConfig(stype, ctx->mc, &sc)) return vint(0);
Pos pos;
if (!getStructurePos(stype, ctx->mc, ctx->seed, rx, rz, &pos)) return vint(0);
return vint(pos.z);
}
static Val fn_spawn(const Node *n, Ctx *ctx, bool want_z) {
if (n->nargs != 0) return verr("spawn_x()/spawn_z() take no args");
ensure_gen(ctx);
if (!ctx->spawn_ready) {
ctx->spawn = getSpawn(ctx->gen);
ctx->spawn_ready = true;
}
return vint(want_z ? ctx->spawn.z : ctx->spawn.x);
}
/* ============================================================
* AST evaluator
* ========================================================== */
static Val eval_node(const Node *n, Ctx *ctx) {
if (!n) return verr("null node");
switch (n->k) {
case ND_INT: return vint(n->ival);
case ND_FLOAT: return vfloat(n->dval);
case ND_STR: return vstr(n->name);
case ND_BOOL: return vbool(n->bval);
case ND_VAR:
if (!strcmp(n->name,"seed")) return vint((int64_t)ctx->seed);
{ char buf[SMAX]; snprintf(buf,SMAX,"unknown variable '%s'",n->name); return verr(buf); }
case ND_CALL:
if (!strcmp(n->name,"biome")) return fn_biome(n,ctx);
if (!strcmp(n->name,"biome_id")) return fn_biome_id(n,ctx);
if (!strcmp(n->name,"has_biome")) return fn_has_biome(n,ctx);
if (!strcmp(n->name,"structure")) return fn_structure(n,ctx);
if (!strcmp(n->name,"struct_x")) return fn_struct_x(n,ctx);
if (!strcmp(n->name,"struct_z")) return fn_struct_z(n,ctx);
if (!strcmp(n->name,"spawn_x")) return fn_spawn(n,ctx,false);
if (!strcmp(n->name,"spawn_z")) return fn_spawn(n,ctx,true);
{ char buf[SMAX]; snprintf(buf,SMAX,"unknown function '%s'",n->name); return verr(buf); }
case ND_NOT: {
Val v = eval_node(n->lhs,ctx);
return (v.type==VT_ERR) ? v : vbool(!vtrue(v));
}
case ND_NEG: {
Val v = eval_node(n->lhs,ctx);
if (v.type==VT_INT) return vint(-v.i);
if (v.type==VT_FLOAT) return vfloat(-v.d);
return verr("unary minus on non-numeric");
}
case ND_ADD: case ND_SUB: case ND_MUL: case ND_DIV: case ND_MOD: {
Val a=eval_node(n->lhs,ctx), b=eval_node(n->rhs,ctx);
if (a.type==VT_ERR) return a;
if (b.type==VT_ERR) return b;
if (a.type==VT_INT && b.type==VT_INT) {
int64_t ai=a.i, bi=b.i;
switch (n->k) {
case ND_ADD: return vint(ai+bi);
case ND_SUB: return vint(ai-bi);
case ND_MUL: return vint(ai*bi);
case ND_DIV: return bi ? vint(ai/bi) : verr("division by zero");
default: return bi ? vint(ai%bi) : verr("modulo by zero");
}
}
double da=(a.type==VT_INT)?(double)a.i:a.d;
double db=(b.type==VT_INT)?(double)b.i:b.d;
switch (n->k) {
case ND_ADD: return vfloat(da+db);
case ND_SUB: return vfloat(da-db);
case ND_MUL: return vfloat(da*db);
default: return db ? vfloat(da/db) : verr("division by zero");
}
}
case ND_LT: case ND_LE: case ND_GT: case ND_GE: {
Val a=eval_node(n->lhs,ctx), b=eval_node(n->rhs,ctx);
if (a.type==VT_ERR) return a;
if (b.type==VT_ERR) return b;
int c=vcmp(a,b);
switch (n->k) {
case ND_LT: return vbool(c<0);
case ND_LE: return vbool(c<=0);
case ND_GT: return vbool(c>0);
default: return vbool(c>=0);
}
}
case ND_EQ: {
Val a=eval_node(n->lhs,ctx), b=eval_node(n->rhs,ctx);
if (a.type==VT_ERR) return a;
if (b.type==VT_ERR) return b;
return vbool(veq(a,b));
}
case ND_NEQ: {
Val a=eval_node(n->lhs,ctx), b=eval_node(n->rhs,ctx);
if (a.type==VT_ERR) return a;
if (b.type==VT_ERR) return b;
return vbool(!veq(a,b));
}
case ND_AND: {
Val a=eval_node(n->lhs,ctx);
if (a.type==VT_ERR) return a;
if (!vtrue(a)) return vbool(false);
Val b=eval_node(n->rhs,ctx);
return (b.type==VT_ERR) ? b : vbool(vtrue(b));
}
case ND_OR: {
Val a=eval_node(n->lhs,ctx);
if (a.type==VT_ERR) return a;
if (vtrue(a)) return vbool(true);
Val b=eval_node(n->rhs,ctx);
return (b.type==VT_ERR) ? b : vbool(vtrue(b));
}
default:
return verr("unknown node kind");
}
}
/* ============================================================
* xorshift64 for random seed generation
* ========================================================== */
static uint64_t xorshift64(uint64_t s) {
s ^= s << 13;
s ^= s >> 7;
s ^= s << 17;
return s;
}
/* ============================================================
* Usage
* ========================================================== */
static void usage(const char *prog) {
printf(
"Usage: %s [OPTIONS]\n"
"\n"
"Options:\n"
" --mc VERSION Minecraft version (default: newest, e.g. 1.21, 1.18, 1.7, b1.8)\n"
" --filter EXPR CEL filter expression (default: accept all seeds)\n"
" --count N Stop after N matches (default: 10)\n"
" --from SEED Starting seed, signed int64 (default: 0)\n"
" --to SEED Ending seed exclusive (default: unlimited)\n"
" --step N Seed increment (default: 1)\n"
" --random Use random seeds instead of sequential\n"
" --dim DIM Dimension: overworld|nether|end (default: overworld)\n"
" --large Use large biomes flag\n"
" --quiet Print only seed values, no labels\n"
" --verbose Print seed index alongside value\n"
" --list-versions Print supported Minecraft versions and exit\n"
" --help, -h Show this help and exit\n"
"\n"
"Filter functions (x,z are block coordinates; rx,rz are region indices):\n"
" biome(x, z) biome name at block coords -> string\n"
" biome_id(x, z) biome numeric ID -> int\n"
" has_biome(name, x, z, r) biome present within r blocks -> bool\n"
" structure(type, rx, rz) viable structure at region -> bool\n"
" struct_x(type, rx, rz) structure block X coord -> int\n"
" struct_z(type, rx, rz) structure block Z coord -> int\n"
" spawn_x() world spawn X coordinate -> int\n"
" spawn_z() world spawn Z coordinate -> int\n"
"\n"
"Filter variable:\n"
" seed current seed (int64)\n"
"\n"
"Structure types:\n"
" village mansion monument outpost desert_pyramid jungle_temple\n"
" swamp_hut igloo ocean_ruin shipwreck ruined_portal ancient_city\n"
" fortress bastion end_city stronghold trail_ruins trial_chambers\n"
"\n"
"Examples:\n"
" %s --mc 1.21 --filter 'biome(0,0) == \"jungle\"'\n"
" %s --mc 1.18 --filter 'has_biome(\"mushroom_fields\",0,0,500)' --count 5\n"
" %s --mc 1.16 --filter 'structure(\"village\",0,0) && structure(\"mansion\",1,0)'\n"
" %s --random --filter 'spawn_x() > -100 && spawn_x() < 100'\n"
" %s --from -100000 --to 100000 --filter 'seed %% 7 == 0' --count 20\n"
"\n", prog, prog, prog, prog, prog, prog);
}
static void list_versions(void) {
printf("Supported Minecraft versions:\n");
for (int mc = MC_B1_7; mc <= MC_NEWEST; mc++) {
const char *v = mc2str(mc);
if (v && strcmp(v,"?")!=0) printf(" %s\n", v);
}
}
/* ============================================================
* Main
* ========================================================== */
int main(int argc, char **argv) {
int mc = MC_NEWEST;
const char *filter = NULL;
uint64_t count = 10;
int64_t from_s = 0;
int64_t to_s = 0;
bool has_to = false;
int64_t step = 1;
bool random_m = false;
int dim = DIM_OVERWORLD;
uint32_t flags = 0;
bool quiet = false;
bool verbose = false;
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i],"--mc") && i+1<argc) {
mc = str2mc(argv[++i]);
if (mc == MC_UNDEF) {
fprintf(stderr,"Unknown version '%s'. Use --list-versions.\n",argv[i]);
return 1;
}
} else if (!strcmp(argv[i],"--filter") && i+1<argc) {
filter = argv[++i];
} else if (!strcmp(argv[i],"--count") && i+1<argc) {
count = (uint64_t)strtoull(argv[++i], NULL, 10);
} else if (!strcmp(argv[i],"--from") && i+1<argc) {
from_s = strtoll(argv[++i], NULL, 10);
} else if (!strcmp(argv[i],"--to") && i+1<argc) {
to_s = strtoll(argv[++i], NULL, 10);
has_to = true;
} else if (!strcmp(argv[i],"--step") && i+1<argc) {
step = strtoll(argv[++i], NULL, 10);
if (step <= 0) { fprintf(stderr,"--step must be > 0\n"); return 1; }
} else if (!strcmp(argv[i],"--random")) {
random_m = true;
} else if (!strcmp(argv[i],"--dim") && i+1<argc) {
++i;
if (!strcmp(argv[i],"overworld")) dim = DIM_OVERWORLD;
else if (!strcmp(argv[i],"nether")) dim = DIM_NETHER;
else if (!strcmp(argv[i],"end")) dim = DIM_END;
else { fprintf(stderr,"Unknown dim '%s'\n",argv[i]); return 1; }
} else if (!strcmp(argv[i],"--large")) {
flags |= LARGE_BIOMES;
} else if (!strcmp(argv[i],"--quiet")) {
quiet = true;
} else if (!strcmp(argv[i],"--verbose")) {
verbose = true;
} else if (!strcmp(argv[i],"--list-versions")) {
list_versions();
return 0;
} else if (!strcmp(argv[i],"--help") || !strcmp(argv[i],"-h")) {
usage(argv[0]);
return 0;
} else {
fprintf(stderr,"Unknown option '%s'. Use --help.\n",argv[i]);
return 1;
}
}
/* Parse filter expression */
Parser parser;
Node *ast = NULL;
if (filter) {
memset(&parser, 0, sizeof(parser));
lex_init(&parser.lex, filter);
ast = parse_expr(&parser);
if (!ast || parser.err[0]) {
fprintf(stderr,"Filter parse error: %s\n",
parser.err[0] ? parser.err : "unknown");
return 1;
}
if (lpeek(&parser.lex).k != TK_EOF) {
fprintf(stderr,"Filter parse error: unexpected trailing input\n");
return 1;
}
}
if (!quiet) {
fprintf(stderr,"# celseed mc=%s dim=%d count=%"PRIu64" filter=%s\n",
mc2str(mc), dim, count, filter ? filter : "(none)");
}
/* Set up generator (once for the mc version / flags) */
Generator gen;
setupGenerator(&gen, mc, flags);
/* Initialize RNG for random mode */
uint64_t rng = (uint64_t)time(NULL) ^ (uint64_t)(uintptr_t)&rng;
if (rng == 0) rng = 1;
rng = xorshift64(rng);
uint64_t seed = (uint64_t)from_s;
uint64_t found = 0;
uint64_t tested = 0;
while (found < count) {
if (!random_m && has_to && (int64_t)seed == to_s) break;
bool match = true;
if (ast) {
Ctx ctx;
ctx.seed = seed;
ctx.mc = mc;
ctx.dim = dim;
ctx.flags = flags;
ctx.gen = &gen;
ctx.gen_ready = false;
ctx.spawn_ready = false;
ctx.spawn.x = ctx.spawn.z = 0;
Val r = eval_node(ast, &ctx);
if (r.type == VT_ERR) {
fprintf(stderr,"# filter error seed=%"PRId64": %s\n",
(int64_t)seed, r.s);
match = false;
} else {
match = vtrue(r);
}
}
if (match) {
found++;
if (quiet) {
printf("%"PRId64"\n", (int64_t)seed);
} else if (verbose) {
printf("%-22"PRId64" [%"PRIu64"/%"PRIu64"]\n",
(int64_t)seed, found, count);
} else {
printf("%"PRId64"\n", (int64_t)seed);
}
fflush(stdout);
}
tested++;
if (random_m) {
rng = xorshift64(rng);
seed = rng;
} else {
seed = (uint64_t)((int64_t)seed + step);
}
}
if (!quiet)
fprintf(stderr,"# done tested=%"PRIu64" found=%"PRIu64"\n", tested, found);
return 0;
}