Implements plugin: spectate v1.0.2b

develop
Josh Cooper 2022-11-17 11:17:13 -08:00
parent b99e948b8a
commit 4a0abd1915
1 changed files with 26 additions and 15 deletions

@ -1,12 +1,12 @@
// //
// Created by josh on 7/28/21. // Created by josh on 7/28/21.
// Last updated: 11//10/22
// //
#include "pause.h" #include "pause.h"
#include <Debug.h> #include <Debug.h>
#include <Core.h> #include <Core.h>
#include <Console.h>
#include <Export.h> #include <Export.h>
#include <PluginManager.h> #include <PluginManager.h>
@ -24,8 +24,6 @@
#include <df/viewscreen.h> #include <df/viewscreen.h>
#include <df/creature_raw.h> #include <df/creature_raw.h>
#include <map>
#include <set>
#include <array> #include <array>
#include <random> #include <random>
#include <cinttypes> #include <cinttypes>
@ -87,6 +85,16 @@ namespace SP {
int32_t timestamp = -1; int32_t timestamp = -1;
std::default_random_engine RNG; std::default_random_engine RNG;
void DebugUnitVector(std::vector<df::unit*> units) {
for (auto unit : units) {
INFO(plugin).print("[id: %d]\n animal: %d\n hostile: %d\n visiting: %d\n",
unit->id,
Units::isAnimal(unit),
Units::isDanger(unit),
Units::isVisiting(unit));
}
}
void PrintStatus(color_ostream &out) { void PrintStatus(color_ostream &out) {
out.print("Spectate is %s\n", enabled ? "ENABLED." : "DISABLED."); out.print("Spectate is %s\n", enabled ? "ENABLED." : "DISABLED.");
out.print(" FEATURES:\n"); out.print(" FEATURES:\n");
@ -232,28 +240,30 @@ namespace SP {
size_t previous = first - 1; size_t previous = first - 1;
// first we get the end of the range // first we get the end of the range
ranges[second] = units.size() - 1; ranges[second] = units.size() - 1;
// then we calculate whether this indicates there is a range or not // then we calculate whether the range exists, and set the first index appropriately
if (first != 0) { if (idx == 0) {
range_exists[idx] = ranges[second] > ranges[previous];
} else {
range_exists[idx] = ranges[second] >= 0; range_exists[idx] = ranges[second] >= 0;
} ranges[first] = 0;
// lastly we set the start of the range } else {
range_exists[idx] = ranges[second] > ranges[previous];
ranges[first] = ranges[previous] + (range_exists[idx] ? 1 : 0); ranges[first] = ranges[previous] + (range_exists[idx] ? 1 : 0);
}
}; };
/// RANGE 0 (in view + working) /// RANGE 0 (in view + working)
// grab valid working units // grab valid working units
add_if([](df::unit* unit) { add_if([&](df::unit* unit) {
return valid(unit) && Units::isCitizen(unit, true) && unit->job.current_job; return valid(unit) &&
Units::isUnitInBox(unit, COORDARGS(viewMin), COORDARGS(viewMax)) &&
Units::isCitizen(unit, true) &&
unit->job.current_job;
}); });
// keep only those in the box
Units::getUnitsInBox(units, COORDARGS(viewMin), COORDARGS(viewMax));
build_range(0); build_range(0);
/// RANGE 1 (in view) /// RANGE 1 (in view)
add_if(valid); add_if([&](df::unit* unit) {
Units::getUnitsInBox(units, COORDARGS(viewMin), COORDARGS(viewMax)); return valid(unit) && Units::isUnitInBox(unit, COORDARGS(viewMin), COORDARGS(viewMax));
});
build_range(1); build_range(1);
/// RANGE 2 (working citizens) /// RANGE 2 (working citizens)
@ -296,6 +306,7 @@ namespace SP {
if (!at_least_one) { if (!at_least_one) {
return false; return false;
} }
//DebugUnitVector(units);
std::piecewise_linear_distribution<> follow_any(i.begin(), i.end(), w.begin()); std::piecewise_linear_distribution<> follow_any(i.begin(), i.end(), w.begin());
// if you're looking at a warning about a local address escaping, it means the unit* from units (which aren't local) // if you're looking at a warning about a local address escaping, it means the unit* from units (which aren't local)
size_t idx = follow_any(RNG); size_t idx = follow_any(RNG);