Make CircularQueue

This commit is contained in:
Bigjango13 2024-10-27 01:44:27 -07:00
parent 1ff1ceaff8
commit 2ea637581d

View File

@ -110,32 +110,34 @@ struct ChatEvent {
}; };
constexpr int EVENT_SIZE = 50; constexpr int EVENT_SIZE = 50;
static int hit_events_at = 0, hit_events_start = 0; template <typename T, int SIZE = EVENT_SIZE>
static ProjectileHitEvent hitEvents[EVENT_SIZE]; struct CircularQueue {
static int chat_events_at = 0, chat_events_start = 0; int at = 0;
static ChatEvent chatEvents[EVENT_SIZE]; int start = 0;
T buf[SIZE];
template <typename T> void push(T event) {
static void push_circular_queue(T event, T events[], int &start, int &at) { buf[at] = event;
events[at] = event;
at++; at++;
at %= EVENT_SIZE; at %= SIZE;
if (at == start) { if (at == start) {
start++; start++;
start %= EVENT_SIZE; start %= SIZE;
}
} }
}
template <typename T> T pop() {
static T pop_circular_queue(T events[], int &start, int &at) {
if (start == at) { if (start == at) {
ERR("Over popped projectile hit events!"); ERR("Over popped projectile hit events!");
} }
T ret = events[start]; T ret = buf[start];
start++; start++;
start %= EVENT_SIZE; start %= EVENT_SIZE;
return ret; return ret;
} }
};
CircularQueue<ProjectileHitEvent> hitEvents{};
CircularQueue<ChatEvent> chatEvents{};
static const std::string fail = "Fail\n"; static const std::string fail = "Fail\n";
std::unordered_map<int, int> modern_entity_id_mapping = { std::unordered_map<int, int> modern_entity_id_mapping = {
@ -232,8 +234,8 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
return std::to_string(removed) + "\n"; return std::to_string(removed) + "\n";
} else if (cmd == "events.chat.posts") { } else if (cmd == "events.chat.posts") {
std::string ret = ""; std::string ret = "";
while (chat_events_at != chat_events_start) { while (chatEvents.at != chatEvents.start) {
ChatEvent ce = pop_circular_queue(chatEvents, chat_events_start, chat_events_at); ChatEvent ce = chatEvents.pop();
std::string message = ce.toString(); std::string message = ce.toString();
if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\'); if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\');
else message = misc_base64_encode(message); else message = misc_base64_encode(message);
@ -243,8 +245,8 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
return ret + "\n"; return ret + "\n";
} else if (cmd == "player.events.chat.posts") { } else if (cmd == "player.events.chat.posts") {
std::string ret = ""; std::string ret = "";
while (chat_events_at != chat_events_start) { while (chatEvents.at != chatEvents.start) {
ChatEvent ce = pop_circular_queue(chatEvents, chat_events_start, chat_events_at); ChatEvent ce = chatEvents.pop();
if (ce.from_player) { if (ce.from_player) {
std::string message = ce.toString(); std::string message = ce.toString();
if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\'); if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\');
@ -256,8 +258,8 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
return ret + "\n"; return ret + "\n";
} else if (cmd == "events.projectile.hits") { } else if (cmd == "events.projectile.hits") {
std::string result = ""; std::string result = "";
while (hit_events_at != hit_events_start) { while (hitEvents.at != hitEvents.start) {
result += pop_circular_queue(hitEvents, hit_events_start, hit_events_at).toString(commandserver) + "|"; result += hitEvents.pop().toString(commandserver) + "|";
} }
if (result.size() > 1) result.pop_back(); if (result.size() > 1) result.pop_back();
return result + "\n"; return result + "\n";
@ -479,13 +481,13 @@ static HitResult *Arrow_tick_HitResult_constructor_injection(HitResult *self, En
self->constructor(target); self->constructor(target);
// Add event // Add event
if (shooter && shooter->isPlayer()) { if (shooter && shooter->isPlayer()) {
push_circular_queue(ProjectileHitEvent{ hitEvents.push(ProjectileHitEvent{
.x = (int) target->x, .x = (int) target->x,
.y = (int) target->y, .y = (int) target->y,
.z = (int) target->z, .z = (int) target->z,
.owner = misc_get_player_username((Player *) shooter), .owner = misc_get_player_username((Player *) shooter),
.targetId = target->id .targetId = target->id
}, hitEvents, hit_events_start, hit_events_at); });
} }
return self; return self;
} }
@ -498,13 +500,13 @@ static void Arrow_tick_injection(Arrow_tick_t old, Arrow *self) {
if (self && !self->pending_removal && self->grounded && oldFlightTime != self->flight_time) { if (self && !self->pending_removal && self->grounded && oldFlightTime != self->flight_time) {
if (shooter && shooter->isPlayer()) { if (shooter && shooter->isPlayer()) {
// Hit! Get the data // Hit! Get the data
push_circular_queue(ProjectileHitEvent{ hitEvents.push(ProjectileHitEvent{
.x = self->hit_x, .x = self->hit_x,
.y = self->hit_y, .y = self->hit_y,
.z = self->hit_z, .z = self->hit_z,
.owner = misc_get_player_username((Player *) shooter), .owner = misc_get_player_username((Player *) shooter),
.targetId = 0 .targetId = 0
}, hitEvents, hit_events_start, hit_events_at); });
} }
} }
} }
@ -534,7 +536,7 @@ static void Throwable_tick_Throwable_onHit_injection(Throwable *self, HitResult
.targetId = 0 .targetId = 0
}; };
} }
push_circular_queue(event, hitEvents, hit_events_start, hit_events_at); hitEvents.push(event);
self->onHit(res); self->onHit(res);
} }
@ -543,10 +545,10 @@ static void Gui_addMessage_injection(Gui_addMessage_t original, Gui *gui, const
if (recursing) { if (recursing) {
original(gui, text); original(gui, text);
} else { } else {
push_circular_queue(ChatEvent{ chatEvents.push(ChatEvent{
text, text,
chat_is_sending() chat_is_sending()
}, chatEvents, chat_events_start, chat_events_at); });
recursing = true; recursing = true;
original(gui, text); original(gui, text);
recursing = false; recursing = false;