2022-03-14 20:33:41 -06:00
|
|
|
//
|
|
|
|
// Created by josh on 7/28/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Core.h"
|
2022-06-05 10:06:55 -06:00
|
|
|
#include <modules/Gui.h>
|
2022-03-14 20:33:41 -06:00
|
|
|
#include <Console.h>
|
|
|
|
#include <Export.h>
|
|
|
|
#include <PluginManager.h>
|
2022-08-28 19:11:50 -06:00
|
|
|
#include <modules/World.h>
|
2022-03-14 20:33:41 -06:00
|
|
|
#include <modules/EventManager.h>
|
|
|
|
#include <modules/Job.h>
|
|
|
|
#include <modules/Units.h>
|
|
|
|
#include <df/job.h>
|
|
|
|
#include <df/unit.h>
|
|
|
|
#include <df/historical_figure.h>
|
|
|
|
#include <df/global_objects.h>
|
|
|
|
#include <df/world.h>
|
2022-06-05 10:06:55 -06:00
|
|
|
#include <df/viewscreen.h>
|
2022-03-14 20:33:41 -06:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
std::map<uint16_t,uint16_t> freq;
|
|
|
|
std::set<int32_t> job_tracker;
|
|
|
|
std::default_random_engine RNG;
|
|
|
|
|
|
|
|
//#include "df/world.h"
|
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
|
|
|
|
|
|
|
DFHACK_PLUGIN("spectate");
|
|
|
|
DFHACK_PLUGIN_IS_ENABLED(enabled);
|
2022-06-05 10:06:55 -06:00
|
|
|
bool dismiss_pause_events = false;
|
2022-03-14 20:33:41 -06:00
|
|
|
bool following_dwarf = false;
|
2022-06-05 10:06:55 -06:00
|
|
|
df::unit* our_dorf = nullptr;
|
2022-03-14 20:33:41 -06:00
|
|
|
void* job_watched = nullptr;
|
|
|
|
int32_t timestamp = -1;
|
|
|
|
REQUIRE_GLOBAL(world);
|
|
|
|
REQUIRE_GLOBAL(ui);
|
2022-06-05 10:06:55 -06:00
|
|
|
REQUIRE_GLOBAL(pause_state);
|
|
|
|
REQUIRE_GLOBAL(d_init);
|
|
|
|
|
|
|
|
// todo: implement as user configurable variables
|
|
|
|
#define tick_span 50
|
|
|
|
#define base 0.99
|
2022-08-28 19:11:50 -06:00
|
|
|
Pausing::AnnouncementLock* pause_lock = nullptr;
|
|
|
|
bool lock_collision = false;
|
2022-03-14 20:33:41 -06:00
|
|
|
|
|
|
|
command_result spectate (color_ostream &out, std::vector <std::string> & parameters);
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands) {
|
|
|
|
commands.push_back(PluginCommand("spectate",
|
|
|
|
"Automated spectator mode.",
|
2022-08-24 12:45:16 -06:00
|
|
|
spectate,
|
|
|
|
false,
|
|
|
|
""
|
|
|
|
" spectate\n"
|
|
|
|
" displays plugin status\n"
|
|
|
|
" spectate enable\n"
|
|
|
|
" spectate disable\n"
|
|
|
|
" spectate auto-unpause\n"
|
|
|
|
" toggle auto-dismissal of game pause events. e.g. a siege event pause\n"
|
|
|
|
"\n"));
|
2022-08-28 19:11:50 -06:00
|
|
|
pause_lock = World::AcquireAnnouncementPauseLock("spectate");
|
2022-03-14 20:33:41 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2022-06-05 10:06:55 -06:00
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
|
2022-08-28 19:11:50 -06:00
|
|
|
if (enabled && world) {
|
2022-06-05 10:06:55 -06:00
|
|
|
switch (event) {
|
|
|
|
case SC_MAP_UNLOADED:
|
|
|
|
case SC_BEGIN_UNLOAD:
|
|
|
|
case SC_WORLD_UNLOADED:
|
|
|
|
our_dorf = nullptr;
|
2022-09-02 21:52:14 -06:00
|
|
|
job_watched = nullptr;
|
|
|
|
following_dwarf = false;
|
2022-06-05 10:06:55 -06:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-14 20:33:41 -06:00
|
|
|
DFhackCExport command_result plugin_shutdown (color_ostream &out) {
|
2022-08-28 19:11:50 -06:00
|
|
|
World::ReleasePauseLock(pause_lock);
|
2022-03-14 20:33:41 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2022-08-28 19:11:50 -06:00
|
|
|
DFhackCExport command_result plugin_onupdate(color_ostream &out) {
|
|
|
|
if (lock_collision) {
|
|
|
|
if (dismiss_pause_events) {
|
|
|
|
// player asked for auto-unpause enabled
|
|
|
|
World::SaveAnnouncementSettings();
|
|
|
|
if (World::DisableAnnouncementPausing()){
|
|
|
|
// now that we've got what we want, we can lock it down
|
|
|
|
lock_collision = false;
|
|
|
|
pause_lock->lock();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (World::RestoreAnnouncementSettings()) {
|
|
|
|
lock_collision = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (dismiss_pause_events && !world->status.popups.empty()) {
|
|
|
|
// dismiss announcement popup(s)
|
|
|
|
Gui::getCurViewscreen(true)->feed_key(interface_key::CLOSE_MEGA_ANNOUNCEMENT);
|
|
|
|
}
|
|
|
|
return DFHack::CR_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-14 20:33:41 -06:00
|
|
|
void onTick(color_ostream& out, void* tick);
|
|
|
|
void onJobStart(color_ostream &out, void* job);
|
|
|
|
void onJobCompletion(color_ostream &out, void* job);
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
|
|
|
|
namespace EM = EventManager;
|
|
|
|
if (enable && !enabled) {
|
|
|
|
out.print("Spectate mode enabled!\n");
|
|
|
|
using namespace EM::EventType;
|
|
|
|
EM::EventHandler ticking(onTick, 15);
|
|
|
|
EM::EventHandler start(onJobStart, 0);
|
|
|
|
EM::EventHandler complete(onJobCompletion, 0);
|
|
|
|
EM::registerListener(EventType::TICK, ticking, plugin_self);
|
|
|
|
EM::registerListener(EventType::JOB_STARTED, start, plugin_self);
|
|
|
|
EM::registerListener(EventType::JOB_COMPLETED, complete, plugin_self);
|
|
|
|
} else if (!enable && enabled) {
|
2022-06-05 10:06:55 -06:00
|
|
|
// warp 8, engage!
|
2022-03-14 20:33:41 -06:00
|
|
|
out.print("Spectate mode disabled!\n");
|
|
|
|
EM::unregisterAll(plugin_self);
|
|
|
|
job_tracker.clear();
|
|
|
|
freq.clear();
|
|
|
|
}
|
|
|
|
enabled = enable;
|
2022-08-24 12:45:16 -06:00
|
|
|
return DFHack::CR_OK;
|
2022-03-14 20:33:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
command_result spectate (color_ostream &out, std::vector <std::string> & parameters) {
|
2022-06-05 10:06:55 -06:00
|
|
|
if(!parameters.empty()) {
|
2022-09-02 21:52:14 -06:00
|
|
|
if (parameters[0] == "auto-unpause") {
|
2022-06-05 10:06:55 -06:00
|
|
|
dismiss_pause_events = !dismiss_pause_events;
|
2022-08-28 19:11:50 -06:00
|
|
|
|
|
|
|
// update the announcement settings if we can
|
|
|
|
if (dismiss_pause_events) {
|
|
|
|
if (World::SaveAnnouncementSettings()) {
|
|
|
|
World::DisableAnnouncementPausing();
|
|
|
|
pause_lock->lock();
|
|
|
|
} else {
|
|
|
|
lock_collision = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pause_lock->unlock();
|
|
|
|
if (!World::RestoreAnnouncementSettings()) {
|
|
|
|
// this in theory shouldn't happen, if others use the lock like we do in spectate
|
|
|
|
lock_collision = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// report to the user how things went
|
|
|
|
if (!lock_collision){
|
|
|
|
out.print(dismiss_pause_events ? "auto-unpause: on\n" : "auto-unpause: off\n");
|
|
|
|
} else {
|
2022-09-02 21:52:14 -06:00
|
|
|
out.print("auto-unpause: must wait for another Pausing::AnnouncementLock to be lifted. This setting will complete when the lock lifts.\n");
|
2022-08-28 19:11:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// probably a typo
|
2022-06-05 10:06:55 -06:00
|
|
|
if (parameters.size() == 2) {
|
2022-09-02 21:52:14 -06:00
|
|
|
out.print("If you want additional options open an issue on github, or mention it on discord.\n\n");
|
2022-06-05 10:06:55 -06:00
|
|
|
return DFHack::CR_WRONG_USAGE;
|
|
|
|
}
|
2022-09-02 22:12:27 -06:00
|
|
|
} else if (parameters[0] == "disengage") {
|
|
|
|
//todo: cannibalize follow
|
2022-09-02 21:52:14 -06:00
|
|
|
} else {
|
|
|
|
return DFHack::CR_WRONG_USAGE;
|
2022-06-05 10:06:55 -06:00
|
|
|
}
|
2022-08-24 12:45:16 -06:00
|
|
|
} else {
|
|
|
|
out.print(enabled ? "Spectate is enabled.\n" : "Spectate is disabled.\n");
|
|
|
|
if(enabled) {
|
|
|
|
out.print(dismiss_pause_events ? "auto-unpause: on.\n" : "auto-unpause: off.\n");
|
|
|
|
}
|
2022-06-05 10:06:55 -06:00
|
|
|
}
|
2022-08-24 12:45:16 -06:00
|
|
|
return DFHack::CR_OK;
|
2022-03-14 20:33:41 -06:00
|
|
|
}
|
|
|
|
|
2022-06-05 10:06:55 -06:00
|
|
|
// every tick check whether to decide to follow a dwarf
|
2022-03-14 20:33:41 -06:00
|
|
|
void onTick(color_ostream& out, void* ptr) {
|
|
|
|
int32_t tick = df::global::world->frame_counter;
|
2022-06-05 10:06:55 -06:00
|
|
|
if(our_dorf){
|
|
|
|
if(!Units::isAlive(our_dorf)){
|
|
|
|
following_dwarf = false;
|
|
|
|
df::global::ui->follow_unit = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!following_dwarf || (tick - timestamp) > tick_span || job_watched == nullptr) {
|
2022-03-14 20:33:41 -06:00
|
|
|
std::vector<df::unit*> dwarves;
|
|
|
|
for (auto unit: df::global::world->units.active) {
|
|
|
|
if (!Units::isCitizen(unit)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dwarves.push_back(unit);
|
|
|
|
}
|
|
|
|
std::uniform_int_distribution<uint64_t> follow_any(0, dwarves.size() - 1);
|
|
|
|
if (df::global::ui) {
|
2022-06-05 10:06:55 -06:00
|
|
|
our_dorf = dwarves[follow_any(RNG)];
|
|
|
|
df::global::ui->follow_unit = our_dorf->id;
|
|
|
|
job_watched = our_dorf->job.current_job;
|
2022-03-14 20:33:41 -06:00
|
|
|
following_dwarf = true;
|
|
|
|
if (!job_watched) {
|
|
|
|
timestamp = tick;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 10:06:55 -06:00
|
|
|
// every new worked job needs to be considered
|
2022-03-14 20:33:41 -06:00
|
|
|
void onJobStart(color_ostream& out, void* job_ptr) {
|
2022-06-05 10:06:55 -06:00
|
|
|
// todo: detect mood jobs
|
2022-03-14 20:33:41 -06:00
|
|
|
int32_t tick = df::global::world->frame_counter;
|
|
|
|
auto job = (df::job*) job_ptr;
|
2022-06-05 10:06:55 -06:00
|
|
|
// don't forget about it
|
2022-03-14 20:33:41 -06:00
|
|
|
int zcount = ++freq[job->pos.z];
|
|
|
|
job_tracker.emplace(job->id);
|
2022-06-05 10:06:55 -06:00
|
|
|
// if we're not doing anything~ then let's pick something
|
|
|
|
if (job_watched == nullptr || (tick - timestamp) > tick_span) {
|
2022-03-14 20:33:41 -06:00
|
|
|
following_dwarf = true;
|
2022-06-05 10:06:55 -06:00
|
|
|
// todo: allow the user to configure b, and also revise the math
|
|
|
|
const double b = base;
|
|
|
|
double p = b * ((double) zcount / job_tracker.size());
|
2022-03-14 20:33:41 -06:00
|
|
|
std::bernoulli_distribution follow_job(p);
|
|
|
|
if (!job->flags.bits.special && follow_job(RNG)) {
|
|
|
|
job_watched = job_ptr;
|
|
|
|
df::unit* unit = Job::getWorker(job);
|
|
|
|
if (df::global::ui && unit) {
|
2022-06-05 10:06:55 -06:00
|
|
|
our_dorf = unit;
|
2022-03-14 20:33:41 -06:00
|
|
|
df::global::ui->follow_unit = unit->id;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
timestamp = tick;
|
|
|
|
std::vector<df::unit*> nonworkers;
|
|
|
|
for (auto unit: df::global::world->units.active) {
|
|
|
|
if (!Units::isCitizen(unit) || unit->job.current_job) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nonworkers.push_back(unit);
|
|
|
|
}
|
|
|
|
std::uniform_int_distribution<> follow_drunk(0, nonworkers.size() - 1);
|
|
|
|
if (df::global::ui) {
|
|
|
|
df::global::ui->follow_unit = nonworkers[follow_drunk(RNG)]->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 10:06:55 -06:00
|
|
|
// every job completed can be forgotten about
|
2022-03-14 20:33:41 -06:00
|
|
|
void onJobCompletion(color_ostream &out, void* job_ptr) {
|
|
|
|
auto job = (df::job*)job_ptr;
|
2022-06-05 10:06:55 -06:00
|
|
|
// forget about it
|
2022-03-14 20:33:41 -06:00
|
|
|
freq[job->pos.z]--;
|
|
|
|
freq[job->pos.z] = freq[job->pos.z] < 0 ? 0 : freq[job->pos.z];
|
|
|
|
job_tracker.erase(job->id);
|
2022-06-05 10:06:55 -06:00
|
|
|
// the job doesn't exist, so we definitely need to get rid of that
|
|
|
|
if (job_watched == job_ptr) {
|
2022-03-14 20:33:41 -06:00
|
|
|
job_watched = nullptr;
|
|
|
|
}
|
|
|
|
}
|