Make CircularQueue
This commit is contained in:
parent
1ff1ceaff8
commit
2ea637581d
@ -110,32 +110,34 @@ struct ChatEvent {
|
||||
};
|
||||
|
||||
constexpr int EVENT_SIZE = 50;
|
||||
static int hit_events_at = 0, hit_events_start = 0;
|
||||
static ProjectileHitEvent hitEvents[EVENT_SIZE];
|
||||
static int chat_events_at = 0, chat_events_start = 0;
|
||||
static ChatEvent chatEvents[EVENT_SIZE];
|
||||
template <typename T, int SIZE = EVENT_SIZE>
|
||||
struct CircularQueue {
|
||||
int at = 0;
|
||||
int start = 0;
|
||||
T buf[SIZE];
|
||||
|
||||
template <typename T>
|
||||
static void push_circular_queue(T event, T events[], int &start, int &at) {
|
||||
events[at] = event;
|
||||
void push(T event) {
|
||||
buf[at] = event;
|
||||
at++;
|
||||
at %= EVENT_SIZE;
|
||||
at %= SIZE;
|
||||
if (at == start) {
|
||||
start++;
|
||||
start %= EVENT_SIZE;
|
||||
start %= SIZE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static T pop_circular_queue(T events[], int &start, int &at) {
|
||||
T pop() {
|
||||
if (start == at) {
|
||||
ERR("Over popped projectile hit events!");
|
||||
}
|
||||
T ret = events[start];
|
||||
T ret = buf[start];
|
||||
start++;
|
||||
start %= EVENT_SIZE;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
};
|
||||
CircularQueue<ProjectileHitEvent> hitEvents{};
|
||||
CircularQueue<ChatEvent> chatEvents{};
|
||||
|
||||
static const std::string fail = "Fail\n";
|
||||
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";
|
||||
} else if (cmd == "events.chat.posts") {
|
||||
std::string ret = "";
|
||||
while (chat_events_at != chat_events_start) {
|
||||
ChatEvent ce = pop_circular_queue(chatEvents, chat_events_start, chat_events_at);
|
||||
while (chatEvents.at != chatEvents.start) {
|
||||
ChatEvent ce = chatEvents.pop();
|
||||
std::string message = ce.toString();
|
||||
if (compat_mode) std::replace(message.begin(), message.end(), '|', '\\');
|
||||
else message = misc_base64_encode(message);
|
||||
@ -243,8 +245,8 @@ std::string CommandServer_parse_injection(CommandServer_parse_t old, CommandServ
|
||||
return ret + "\n";
|
||||
} else if (cmd == "player.events.chat.posts") {
|
||||
std::string ret = "";
|
||||
while (chat_events_at != chat_events_start) {
|
||||
ChatEvent ce = pop_circular_queue(chatEvents, chat_events_start, chat_events_at);
|
||||
while (chatEvents.at != chatEvents.start) {
|
||||
ChatEvent ce = chatEvents.pop();
|
||||
if (ce.from_player) {
|
||||
std::string message = ce.toString();
|
||||
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";
|
||||
} else if (cmd == "events.projectile.hits") {
|
||||
std::string result = "";
|
||||
while (hit_events_at != hit_events_start) {
|
||||
result += pop_circular_queue(hitEvents, hit_events_start, hit_events_at).toString(commandserver) + "|";
|
||||
while (hitEvents.at != hitEvents.start) {
|
||||
result += hitEvents.pop().toString(commandserver) + "|";
|
||||
}
|
||||
if (result.size() > 1) result.pop_back();
|
||||
return result + "\n";
|
||||
@ -479,13 +481,13 @@ static HitResult *Arrow_tick_HitResult_constructor_injection(HitResult *self, En
|
||||
self->constructor(target);
|
||||
// Add event
|
||||
if (shooter && shooter->isPlayer()) {
|
||||
push_circular_queue(ProjectileHitEvent{
|
||||
hitEvents.push(ProjectileHitEvent{
|
||||
.x = (int) target->x,
|
||||
.y = (int) target->y,
|
||||
.z = (int) target->z,
|
||||
.owner = misc_get_player_username((Player *) shooter),
|
||||
.targetId = target->id
|
||||
}, hitEvents, hit_events_start, hit_events_at);
|
||||
});
|
||||
}
|
||||
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 (shooter && shooter->isPlayer()) {
|
||||
// Hit! Get the data
|
||||
push_circular_queue(ProjectileHitEvent{
|
||||
hitEvents.push(ProjectileHitEvent{
|
||||
.x = self->hit_x,
|
||||
.y = self->hit_y,
|
||||
.z = self->hit_z,
|
||||
.owner = misc_get_player_username((Player *) shooter),
|
||||
.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
|
||||
};
|
||||
}
|
||||
push_circular_queue(event, hitEvents, hit_events_start, hit_events_at);
|
||||
hitEvents.push(event);
|
||||
self->onHit(res);
|
||||
}
|
||||
|
||||
@ -543,10 +545,10 @@ static void Gui_addMessage_injection(Gui_addMessage_t original, Gui *gui, const
|
||||
if (recursing) {
|
||||
original(gui, text);
|
||||
} else {
|
||||
push_circular_queue(ChatEvent{
|
||||
chatEvents.push(ChatEvent{
|
||||
text,
|
||||
chat_is_sending()
|
||||
}, chatEvents, chat_events_start, chat_events_at);
|
||||
});
|
||||
recursing = true;
|
||||
original(gui, text);
|
||||
recursing = false;
|
||||
|
Loading…
Reference in New Issue
Block a user