Use fewer pointers refactor: get rid of visible_world_ in drivenengine. The world is now fully owned by the drivenengine.

This commit is contained in:
2026-02-21 19:27:42 -05:00
parent 48b7bf37da
commit 00ef81bf0d
4 changed files with 47 additions and 63 deletions

View File

@@ -161,10 +161,6 @@ SharedChannel DrivenEngine::new_incoming_channel() {
} }
} }
void DrivenEngine::set_visible_world_and_actor(World *w, int64_t id) {
visible_world_ = w;
visible_actor_id_ = id;
}
void DrivenEngine::stop_driver() { void DrivenEngine::stop_driver() {
stop_driver_ = true; stop_driver_ = true;
@@ -176,11 +172,6 @@ void DrivenEngine::stop_driver() {
} }
DrivenEngine::DrivenEngine() { DrivenEngine::DrivenEngine() {
next_unused_chid_ = 1;
rescan_lua_source_ = false;
clock_ = 0.0;
have_prints_ = false;
stop_driver_ = false;
} }
DrivenEngine::~DrivenEngine() {} DrivenEngine::~DrivenEngine() {}
@@ -377,20 +368,20 @@ bool DrivenEngine::drv_get_stop_driver() const {
} }
int64_t DrivenEngine::drv_get_actor_id() const { int64_t DrivenEngine::drv_get_actor_id() const {
return visible_actor_id_; return actor_id_;
} }
void DrivenEngine::drv_get_tangibles_near(int64_t tanid, double rx, double ry, double rz, uint32_t *count, int64_t **ids) { void DrivenEngine::drv_get_tangibles_near(int64_t tanid, double rx, double ry, double rz, uint32_t *count, int64_t **ids) {
uint32_t hash1 = eng::memhash(); uint32_t hash1 = eng::memhash();
scan_result_.clear(); scan_result_.clear();
if ((visible_world_ != 0) && (tanid != 0)) { if (world_ && (tanid != 0)) {
PlaneScan scan; PlaneScan scan;
scan.set_near(tanid, true); scan.set_near(tanid, true);
scan.set_omit_nowhere(true); scan.set_omit_nowhere(true);
scan.set_sorted(false); scan.set_sorted(false);
scan.set_radius(util::XYZ(rx, ry, rz)); scan.set_radius(util::XYZ(rx, ry, rz));
scan.set_shape(PlaneScan::CYLINDER); scan.set_shape(PlaneScan::CYLINDER);
visible_world_->get_near(scan, &scan_result_); world_->get_near(scan, &scan_result_);
} }
*count = scan_result_.size(); *count = scan_result_.size();
if (*count > 0) { if (*count > 0) {
@@ -405,12 +396,12 @@ void DrivenEngine::drv_get_tangibles_near(int64_t tanid, double rx, double ry, d
void DrivenEngine::drv_get_animation_queues(uint32_t count, const int64_t *ids, uint32_t *lengths, const char **strings) { void DrivenEngine::drv_get_animation_queues(uint32_t count, const int64_t *ids, uint32_t *lengths, const char **strings) {
anim_queues_.resize(count); anim_queues_.resize(count);
if (visible_world_ == nullptr) { if (!world_) {
for (int i = 0; i < int(count); i++) { for (int i = 0; i < int(count); i++) {
anim_queues_[i] = AnimQueue::get_encoded_blank_queue(); anim_queues_[i] = AnimQueue::get_encoded_blank_queue();
} }
} else { } else {
visible_world_->get_encoded_animation_queues(count, ids, anim_queues_); world_->get_encoded_animation_queues(count, ids, anim_queues_);
} }
for (int i = 0; i < int(count); i++) { for (int i = 0; i < int(count); i++) {
lengths[i] = anim_queues_[i]->size(); lengths[i] = anim_queues_[i]->size();

View File

@@ -211,14 +211,6 @@ public:
// //
void set_have_prints(bool b) { have_prints_ = b; } void set_have_prints(bool b) { have_prints_ = b; }
// Set the world pointer and the actor ID.
//
// This allows the graphics engine to query the DrivenEngine
// about the state of the world and the player. It is legal to set these
// to zero, in which case queries will return null results.
//
void set_visible_world_and_actor(World *w, int64_t actor);
// Stop the driver. The engine should call this when it's done // Stop the driver. The engine should call this when it's done
// and there's nothing left to do. // and there's nothing left to do.
// //
@@ -287,21 +279,36 @@ private:
// Get the channel associated with the specified channel ID. // Get the channel associated with the specified channel ID.
Channel *get_chid(int chid) const; Channel *get_chid(int chid) const;
protected:
// The DrivenEngine can optionally contain
// a world model. This is initialized to
// nullptr, but classes that derive from
// DrivenEngine can store a world model here.
// If they do, then functions like get_near
// will reference this model.
//
std::unique_ptr<World> world_;
// When the Driver calls get_actor_id,
// we return this value. This is initialized
// to zero, but classes that derive from
// DrivenEngine can store an actor_id here.
//
int64_t actor_id_ = 0;
private: private:
SharedChannel channels_[DRV_MAX_CHAN]; SharedChannel channels_[DRV_MAX_CHAN];
int next_unused_chid_; int next_unused_chid_ = 1;
eng::vector<SharedChannel> accepted_channels_; eng::vector<SharedChannel> accepted_channels_;
eng::vector<uint32_t> new_outgoing_; eng::vector<uint32_t> new_outgoing_;
eng::vector<uint32_t> listen_ports_; eng::vector<uint32_t> listen_ports_;
World *visible_world_;
int64_t visible_actor_id_;
util::IdVector scan_result_; util::IdVector scan_result_;
std::vector<util::SharedStdString> anim_queues_; std::vector<util::SharedStdString> anim_queues_;
StreamBuffer call_function_retpk_; StreamBuffer call_function_retpk_;
bool rescan_lua_source_; bool rescan_lua_source_ = false;
double clock_; double clock_ = 0.0;
bool have_prints_; bool have_prints_ = false;
bool stop_driver_; bool stop_driver_ = false;
friend class Channel; friend class Channel;
}; };

View File

@@ -11,8 +11,6 @@
class LpxClient : public DrivenEngine { class LpxClient : public DrivenEngine {
public: public:
UniqueWorld world_;
int64_t actor_id_;
InvocationQueue unack_; InvocationQueue unack_;
SharedChannel channel_; SharedChannel channel_;
PrintChanneler print_channeler_; PrintChanneler print_channeler_;
@@ -47,9 +45,6 @@ public:
// Clear the unack command queue. // Clear the unack command queue.
unack_.clear(); unack_.clear();
// Export stuff to the graphics engine.
set_visible_world_and_actor(world_.get(), actor_id_);
// Reset the print channeler // Reset the print channeler
print_channeler_.reset(); print_channeler_.reset();
@@ -73,9 +68,6 @@ public:
// Clear the unack command queue. // Clear the unack command queue.
unack_.clear(); unack_.clear();
// Export stuff to the graphics engine.
set_visible_world_and_actor(world_.get(), actor_id_);
// Reset the print channeler // Reset the print channeler
print_channeler_.reset(); print_channeler_.reset();
@@ -139,7 +131,6 @@ public:
util::dprint("Actor ID changing: ", actor_id); util::dprint("Actor ID changing: ", actor_id);
print_channeler_.reset(); print_channeler_.reset();
actor_id_ = actor_id; actor_id_ = actor_id;
set_visible_world_and_actor(world_.get(), actor_id_);
} }
void receive_ack_from_server(StreamBuffer *sb) { void receive_ack_from_server(StreamBuffer *sb) {

View File

@@ -23,13 +23,11 @@ using ClientVector = eng::vector<UniqueClient>;
class LpxServer : public DrivenEngine { class LpxServer : public DrivenEngine {
public: public:
UniqueWorld master_;
ClientVector clients_; ClientVector clients_;
PrintChanneler print_channeler_; PrintChanneler print_channeler_;
HttpChannelMap http_client_channels_; HttpChannelMap http_client_channels_;
HttpChannelVec http_server_channels_; HttpChannelVec http_server_channels_;
eng::vector<Invocation> delayed_invocations_; eng::vector<Invocation> delayed_invocations_;
int64_t admin_id_ = 0;
double next_tick_ = 0; double next_tick_ = 0;
lua_State *lua_syntax_checker_; lua_State *lua_syntax_checker_;
@@ -40,13 +38,13 @@ public:
lua_syntax_checker_ = LuaCoreStack::newstate(eng::l_alloc); lua_syntax_checker_ = LuaCoreStack::newstate(eng::l_alloc);
// Create the master world model. // Create the master world model.
master_.reset(new World(WORLD_TYPE_MASTER)); world_.reset(new World(WORLD_TYPE_MASTER));
// Create the admin actor. Note: there isn't any 'init' function yet. // Create the admin actor. Note: there isn't any 'init' function yet.
admin_id_ = master_->create_login_actor(); actor_id_ = world_->create_login_actor();
// Print out admin ID for debugging purposes. // Print out admin ID for debugging purposes.
util::dprint("Admin actor id = ", admin_id_); util::dprint("Admin actor id = ", actor_id_);
// Enable listening on port 8085 (client connections) // Enable listening on port 8085 (client connections)
listen_port(8085); listen_port(8085);
@@ -54,9 +52,6 @@ public:
// Enable listening on port 8080 (http server connections) // Enable listening on port 8080 (http server connections)
listen_port(8080); listen_port(8080);
// Export stuff to the graphics engine.
set_visible_world_and_actor(master_.get(), admin_id_);
// Reset the print channeler. // Reset the print channeler.
print_channeler_.reset(); print_channeler_.reset();
@@ -75,7 +70,7 @@ public:
void delete_client(UniqueClient &client) { void delete_client(UniqueClient &client) {
util::dprint("Client closed: actor id=", client->actor_id_); util::dprint("Client closed: actor id=", client->actor_id_);
master_->disconnected(client->actor_id_); world_->disconnected(client->actor_id_);
client.reset(); client.reset();
} }
@@ -85,7 +80,7 @@ public:
sb->write_uint32(0); sb->write_uint32(0);
int64_t tw_1 = sb->total_writes(); int64_t tw_1 = sb->total_writes();
// util::dprint("Sending diffs to client ", client->actor_id_); // util::dprint("Sending diffs to client ", client->actor_id_);
client->sync_->diff(client->actor_id_, full, master_.get(), sb); client->sync_->diff(client->actor_id_, full, world_.get(), sb);
int64_t tw_2 = sb->total_writes(); int64_t tw_2 = sb->total_writes();
sb->overwrite_int32(tw_1, tw_2 - tw_1); sb->overwrite_int32(tw_1, tw_2 - tw_1);
} }
@@ -114,7 +109,7 @@ public:
return false; return false;
} }
// util::dprint("Invoking: ", inv.debug_string()); // util::dprint("Invoking: ", inv.debug_string());
master_->invoke(inv); world_->invoke(inv);
client->channel_->out()->write_uint8(util::MSG_ACK); client->channel_->out()->write_uint8(util::MSG_ACK);
client->channel_->out()->write_uint32(0); client->channel_->out()->write_uint32(0);
client->sync_->invoke(inv); client->sync_->invoke(inv);
@@ -123,20 +118,20 @@ public:
} }
virtual void event_access(AccessKind kind, int64_t place_id, std::string_view datapk, StreamBuffer *retpk) override { virtual void event_access(AccessKind kind, int64_t place_id, std::string_view datapk, StreamBuffer *retpk) override {
if (place_id == 0) place_id = admin_id_; if (place_id == 0) place_id = actor_id_;
switch (kind) { switch (kind) {
case AccessKind::INVOKE_LUA_CALL: case AccessKind::INVOKE_LUA_CALL:
case AccessKind::INVOKE_LUA_EXPR: case AccessKind::INVOKE_LUA_EXPR:
case AccessKind::INVOKE_FLUSH_PRINTS: case AccessKind::INVOKE_FLUSH_PRINTS:
case AccessKind::INVOKE_TICK: case AccessKind::INVOKE_TICK:
case AccessKind::INVOKE_LUA_SOURCE: { case AccessKind::INVOKE_LUA_SOURCE: {
delayed_invocations_.emplace_back(kind, admin_id_, place_id, datapk); delayed_invocations_.emplace_back(kind, actor_id_, place_id, datapk);
break; break;
} }
case AccessKind::PROBE_LUA_CALL: { case AccessKind::PROBE_LUA_CALL: {
master_->snapshot(); world_->snapshot();
master_->probe_lua_call(admin_id_, place_id, datapk, retpk); world_->probe_lua_call(actor_id_, place_id, datapk, retpk);
master_->rollback(); world_->rollback();
break; break;
} }
case AccessKind::VALIDATE_LUA_EXPR: { case AccessKind::VALIDATE_LUA_EXPR: {
@@ -148,8 +143,8 @@ public:
} }
case AccessKind::CHANNEL_PRINTS: { case AccessKind::CHANNEL_PRINTS: {
// If there's nothing new in the printbuffer, this is very fast. // If there's nothing new in the printbuffer, this is very fast.
if (print_channeler_.channel(master_->get_printbuffer(admin_id_), retpk)) { if (print_channeler_.channel(world_->get_printbuffer(actor_id_), retpk)) {
master_->invoke(print_channeler_.invocation(admin_id_)); world_->invoke(print_channeler_.invocation(actor_id_));
} }
set_have_prints(false); set_have_prints(false);
break; break;
@@ -171,7 +166,7 @@ public:
// Execute any queued invocations. // Execute any queued invocations.
// We just feed these directly into the master model. // We just feed these directly into the master model.
for (const Invocation &inv : delayed_invocations_) { for (const Invocation &inv : delayed_invocations_) {
master_->invoke(inv); world_->invoke(inv);
} }
delayed_invocations_.clear(); delayed_invocations_.clear();
@@ -181,7 +176,7 @@ public:
if (chan == nullptr) break; if (chan == nullptr) break;
if (chan->port() == 8085) { if (chan->port() == 8085) {
Client *client = new Client; Client *client = new Client;
client->actor_id_ = master_->create_login_actor(); client->actor_id_ = world_->create_login_actor();
// TODO: initialize the login actor on the master. // TODO: initialize the login actor on the master.
client->channel_ = std::move(chan); client->channel_ = std::move(chan);
client->async_diff_ = true; client->async_diff_ = true;
@@ -202,7 +197,7 @@ public:
// If the clock has advanced far enough, tick the master model. // If the clock has advanced far enough, tick the master model.
if (clock >= next_tick_) { if (clock >= next_tick_) {
master_->invoke(Invocation(AccessKind::INVOKE_TICK, 0, 0, "")); world_->invoke(Invocation(AccessKind::INVOKE_TICK, 0, 0, ""));
for (UniqueClient &client : clients_) { for (UniqueClient &client : clients_) {
client->async_diff_ = true; client->async_diff_ = true;
} }
@@ -248,7 +243,7 @@ public:
util::remove_nullptrs(clients_); util::remove_nullptrs(clients_);
// Look for new outgoing HTTP client requests. // Look for new outgoing HTTP client requests.
for (const auto &pair : master_->http_requests()) { for (const auto &pair : world_->http_requests()) {
const HttpClientRequest &request = pair.second; const HttpClientRequest &request = pair.second;
HttpChannel &channel = http_client_channels_[request.request_id()]; HttpChannel &channel = http_client_channels_[request.request_id()];
if (channel.channel_ == nullptr) { if (channel.channel_ == nullptr) {
@@ -281,7 +276,7 @@ public:
for (const HttpParser &response : http_responses) { for (const HttpParser &response : http_responses) {
http_client_channels_.erase(response.request_id()); http_client_channels_.erase(response.request_id());
} }
master_->http_responses(http_responses); world_->http_responses(http_responses);
// Maintain incoming HTTP server channels. // Maintain incoming HTTP server channels.
for (HttpChannel &htchan : http_server_channels_) { for (HttpChannel &htchan : http_server_channels_) {
@@ -292,7 +287,7 @@ public:
htchan.parsed_bytes_ = chan->in()->fill(); htchan.parsed_bytes_ = chan->in()->fill();
if (parser.complete()) { if (parser.complete()) {
StreamBuffer *sb = chan->out(); StreamBuffer *sb = chan->out();
HttpServerResponse resp = master_->http_serve(parser); HttpServerResponse resp = world_->http_serve(parser);
resp.send(sb); resp.send(sb);
htchan.channel_ = nullptr; htchan.channel_ = nullptr;
} }
@@ -301,7 +296,7 @@ public:
util::remove_marked_items(http_server_channels_); util::remove_marked_items(http_server_channels_);
// Notify the driver if there are any prints. // Notify the driver if there are any prints.
set_have_prints(print_channeler_.have_prints(master_->get_printbuffer(admin_id_))); set_have_prints(print_channeler_.have_prints(world_->get_printbuffer(actor_id_)));
} }
}; };