| 91 | } |
| 92 | |
| 93 | void LuaState::bindSimulation(Simulation& simulation) { |
| 94 | if (!impl_) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | impl_->api = std::make_unique<ScriptAPI>(simulation); |
| 99 | |
| 100 | impl_->lua.new_usertype<ScriptBatch>("ScriptBatch", |
| 101 | "spawn", &ScriptBatch::spawn, |
| 102 | "random_spawn", &ScriptBatch::random_spawn, |
| 103 | "finish", &ScriptBatch::finish |
| 104 | ); |
| 105 | |
| 106 | impl_->lua.new_usertype<ScriptAPI>("ScriptAPI", |
| 107 | "clear", &ScriptAPI::clear, |
| 108 | "set_world_title", &ScriptAPI::set_world_title, |
| 109 | "set_box", &ScriptAPI::set_box, |
| 110 | "world_size", &ScriptAPI::world_size, |
| 111 | "load_molecules", &ScriptAPI::load_molecules, |
| 112 | "begin_batch", &ScriptAPI::begin_batch, |
| 113 | "lj_min", &ScriptAPI::lj_min, |
| 114 | "random_fill", &ScriptAPI::random_fill, |
| 115 | "lattice_fill", &ScriptAPI::lattice_fill |
| 116 | ); |
| 117 | |
| 118 | sol::table atoms = impl_->lua.create_named_table("atoms"); |
| 119 | for (size_t i = 0; i < static_cast<size_t>(AtomData::Type::COUNT); ++i) { |
| 120 | const AtomData::Type type = static_cast<AtomData::Type>(i); |
| 121 | const std::string_view symbol = AtomData::symbol(type); |
| 122 | if (!symbol.empty()) { |
| 123 | atoms[std::string(symbol)] = std::string(symbol); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | sol::object moleculeObject = sol::make_object(impl_->lua, impl_->lua.create_table()); |
| 128 | const std::filesystem::path moleculeModulePath = std::filesystem::path("Mods") / "Base" / "API" / "molecule.lua"; |
| 129 | if (std::filesystem::exists(moleculeModulePath)) { |
| 130 | const sol::load_result loaded = impl_->lua.load_file(moleculeModulePath.string()); |
| 131 | if (loaded.valid()) { |
| 132 | const sol::protected_function module = loaded; |
| 133 | const sol::protected_function_result result = module(); |
| 134 | if (result.valid() && result.get_type() == sol::type::table) { |
| 135 | moleculeObject = sol::make_object(impl_->lua, result.get<sol::table>()); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | impl_->lua["molecule"] = moleculeObject; |
| 141 | impl_->lua["scene"] = std::ref(*impl_->api); |
| 142 | } |
| 143 | |
| 144 | const char* LuaState::lastError() const noexcept { |
| 145 | return lastError_.c_str(); |