#pragma once #include "ActionFile.h" #include #include #include #include #include #include namespace Bilicraft { // Single-threaded cooperative scheduler. Only the owner may execute commands; // enqueue/submit are safe from that owner's Yield callback, never other threads. class ActionScheduler { public: using Clock = std::chrono::steady_clock; using TaskId = std::uint64_t; enum class State { Ready, Running, Blocked }; struct TaskInfo { TaskId id; std::string name; State state; std::size_t nextCommand; std::size_t commandCount; Clock::time_point wakeAt; }; struct Work { ActionCommand command; TaskId taskId; // 0 for an interactive command bool interactive; }; TaskId Enqueue(ActionProgram program, Clock::time_point now = Clock::now()); void WakeReady(Clock::time_point now); void SubmitInteractive(ActionCommand command); // Call only when no command is outstanding. Wait is handled here, never // passed to the game executor. Timers don't preempt a running action. std::optional Next(Clock::time_point now); void Finish(bool success); bool InteractivePending() const; bool HasWork() const; std::optional NextWake() const; std::vector Tasks() const; std::vector DrainEvents(); private: struct Task { TaskId id; ActionProgram program; std::size_t pc = 0; State state = State::Ready; Clock::time_point wakeAt{}; std::uint64_t readyOrder = 0; }; void RemoveActive(bool success); std::map tasks; std::optional active; std::optional interactive; bool inFlight = false; bool interactiveInFlight = false; TaskId nextId = 1; std::uint64_t nextOrder = 1; std::size_t storedCommands = 0; std::vector events; }; } // namespace Bilicraft