// src/gol.cpp #include #include #include #include #include #include using namespace godot; class GoL : public RefCounted { GDCLASS(GoL, RefCounted); static void _bind_methods() { ClassDB::bind_method(D_METHOD("step_once", "arr", "n"), &GoL::step_once); } static int neighs(const Array &a, int n, int x, int y) { int s = 0; for (int dy = -1; dy <= 1; ++dy) { for (int dx = -1; dx <= 1; ++dx) { if (dx == 0 && dy == 0) continue; const int nx = ((x + dx) % n + n) % n; const int ny = ((y + dy) % n + n) % n; const Array row = a[ny]; s += (int)row[nx]; } } return s; } public: Array step_once(const Array &arr, int n) const { Array next; next.resize(n); for (int y = 0; y < n; ++y) { Array row; row.resize(n); const Array crow = arr[y]; for (int x = 0; x < n; ++x) { const bool alive = ((int)crow[x]) == 1; const int cn = neighs(arr, n, x, y); row.set(x, alive ? int(cn == 2 || cn == 3) : int(cn == 3)); } next.set(y, row); } return next; } }; extern "C" GDExtensionBool GDE_EXPORT gol_library_init(GDExtensionInterfaceGetProcAddress get_proc_address, GDExtensionClassLibraryPtr library, GDExtensionInitialization *r_initialization) { static GDExtensionBinding::InitObject init(get_proc_address, library, r_initialization); init.register_initializer([](ModuleInitializationLevel p_level) { if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) return; ClassDB::register_class(); }); init.register_terminator([](ModuleInitializationLevel /*p_level*/) { // no-op }); init.set_minimum_library_initialization_level( MODULE_INITIALIZATION_LEVEL_SCENE); return init.init(); }