2012-02-21 01:44:10 -07:00
|
|
|
// Make the camera follow the selected unit
|
|
|
|
|
2016-08-13 19:44:01 -06:00
|
|
|
#include "Console.h"
|
2012-02-21 01:44:10 -07:00
|
|
|
#include "Core.h"
|
|
|
|
#include "DataDefs.h"
|
2016-08-13 19:44:01 -06:00
|
|
|
#include "DFHack.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
|
2012-02-21 01:44:10 -07:00
|
|
|
#include "modules/Gui.h"
|
|
|
|
#include "modules/Maps.h"
|
2016-08-13 19:44:01 -06:00
|
|
|
#include "modules/World.h"
|
|
|
|
|
|
|
|
#include "df/creature_raw.h"
|
|
|
|
#include "df/unit.h"
|
|
|
|
#include "df/world.h"
|
2012-02-21 01:44:10 -07:00
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
|
|
|
|
2014-12-06 16:47:35 -07:00
|
|
|
DFHACK_PLUGIN("follow");
|
|
|
|
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
|
|
|
|
REQUIRE_GLOBAL(world);
|
2012-02-21 01:44:10 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result follow (color_ostream &out, std::vector <std::string> & parameters);
|
2012-02-21 01:44:10 -07:00
|
|
|
|
|
|
|
df::unit *followedUnit;
|
|
|
|
int32_t prevX, prevY, prevZ;
|
2012-02-22 00:30:44 -07:00
|
|
|
uint8_t prevMenuWidth;
|
2012-02-21 01:44:10 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
|
2012-02-21 01:44:10 -07:00
|
|
|
{
|
|
|
|
commands.push_back(PluginCommand(
|
2015-11-09 20:37:45 -07:00
|
|
|
"follow", "Make the screen follow the selected unit",
|
2012-03-03 06:38:24 -07:00
|
|
|
follow, Gui::view_unit_hotkey,
|
2012-03-10 04:55:42 -07:00
|
|
|
" Select a unit and run this plugin to make the camera follow it.\n"
|
|
|
|
" Moving the camera yourself deactivates the plugin.\n"
|
2012-02-21 01:44:10 -07:00
|
|
|
));
|
|
|
|
followedUnit = 0;
|
|
|
|
prevX=prevY=prevZ = -1;
|
2012-02-22 00:30:44 -07:00
|
|
|
prevMenuWidth = 0;
|
2012-02-21 01:44:10 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
2012-02-21 01:44:10 -07:00
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
|
2012-02-21 01:44:10 -07:00
|
|
|
{
|
|
|
|
switch (event) {
|
2012-03-31 18:56:54 -06:00
|
|
|
case SC_MAP_LOADED:
|
|
|
|
case SC_MAP_UNLOADED: //Make sure our plugin's variables are clean
|
2012-02-21 01:44:10 -07:00
|
|
|
followedUnit = 0;
|
|
|
|
prevX=prevY=prevZ = -1;
|
2012-02-22 00:30:44 -07:00
|
|
|
prevMenuWidth = 0;
|
2013-09-30 03:19:51 -06:00
|
|
|
is_enabled = false;
|
2012-02-21 01:44:10 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_onupdate ( color_ostream &out )
|
2012-02-21 01:44:10 -07:00
|
|
|
{
|
2012-02-22 00:30:44 -07:00
|
|
|
if (!followedUnit) return CR_OK; //Don't do anything if we're not following a unit
|
|
|
|
|
2012-10-06 03:46:20 -06:00
|
|
|
if (World::ReadPauseState() && prevX==-1) return CR_OK; //Wait until the game is unpaused after first running "follow" to begin following
|
2012-02-22 00:30:44 -07:00
|
|
|
|
2012-02-21 01:44:10 -07:00
|
|
|
df::coord &unitPos = followedUnit->pos;
|
2012-02-22 00:30:44 -07:00
|
|
|
|
2012-03-03 06:38:24 -07:00
|
|
|
//Get all of the relevant data for determining the size of the map on the window
|
2012-02-22 00:30:44 -07:00
|
|
|
int32_t x,y,z,w,h,c_x,c_y,c_z;
|
|
|
|
uint8_t menu_width, area_map_width;
|
2012-03-03 06:38:24 -07:00
|
|
|
Gui::getViewCoords(x,y,z);
|
|
|
|
Gui::getWindowSize(w,h);
|
|
|
|
Gui::getMenuWidth(menu_width, area_map_width);
|
|
|
|
Gui::getCursorCoords(c_x,c_y,c_z);
|
2012-02-22 00:30:44 -07:00
|
|
|
|
2012-03-03 06:38:24 -07:00
|
|
|
// FIXME: is this really needed? does it ever evaluate to 'true'?
|
2012-02-28 10:23:02 -07:00
|
|
|
if (c_x != -30000 && menu_width == 3) menu_width = 2; //Presence of the cursor means that there's actually a width-2 menu open
|
2012-02-22 00:30:44 -07:00
|
|
|
|
|
|
|
h -= 2; //account for vertical borders
|
|
|
|
|
|
|
|
if (menu_width == 1) w -= 57; //Menu is open doubly wide
|
|
|
|
else if (menu_width == 2 && area_map_width == 3) w -= 33; //Just the menu is open
|
|
|
|
else if (menu_width == 2 && area_map_width == 2) w -= 26; //Just the area map is open
|
|
|
|
else w -= 2; //No menu or area map, just account for borders
|
2012-02-22 10:43:14 -07:00
|
|
|
|
2012-02-22 00:30:44 -07:00
|
|
|
if (prevMenuWidth == 0) prevMenuWidth = menu_width; //have we already had a menu width?
|
|
|
|
|
|
|
|
if (prevX==-1) //have we already had previous values for the window location?
|
2012-02-21 01:44:10 -07:00
|
|
|
{
|
|
|
|
prevX = x;
|
|
|
|
prevY = y;
|
|
|
|
prevZ = z;
|
|
|
|
}
|
2012-02-22 00:30:44 -07:00
|
|
|
else if((prevX != x || prevY != y || prevZ != z) && prevMenuWidth == menu_width) //User has manually moved the window, stop following the unit
|
2012-02-21 01:44:10 -07:00
|
|
|
{
|
2013-09-30 03:19:51 -06:00
|
|
|
is_enabled = false;
|
2012-02-21 01:44:10 -07:00
|
|
|
followedUnit = 0;
|
|
|
|
prevX=prevY=prevZ = -1;
|
2012-02-22 00:30:44 -07:00
|
|
|
prevMenuWidth = 0;
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("No longer following anything.\n");
|
2012-02-21 01:44:10 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
2012-02-22 10:43:14 -07:00
|
|
|
|
2012-03-03 06:38:24 -07:00
|
|
|
//Get map size in tiles so we can prevent the camera from going off the edge
|
2012-02-21 01:44:10 -07:00
|
|
|
uint32_t x_max, y_max, z_max;
|
2012-03-03 06:38:24 -07:00
|
|
|
Maps::getSize(x_max, y_max, z_max);
|
2012-02-21 01:44:10 -07:00
|
|
|
x_max *= 16;
|
|
|
|
y_max *= 16;
|
|
|
|
|
2012-03-03 06:38:24 -07:00
|
|
|
//Calculate a new screen position centered on the selected unit
|
|
|
|
x = unitPos.x + w/2 >= x_max ? x_max-w : (unitPos.x >= w/2 ? unitPos.x - w/2 : 0);
|
2012-02-22 00:30:44 -07:00
|
|
|
y = unitPos.y + h/2 >= y_max ? y_max-h : (unitPos.y >= h/2 ? unitPos.y - h/2 : 0);
|
|
|
|
z = unitPos.z;
|
|
|
|
|
2012-03-03 06:38:24 -07:00
|
|
|
//Set the new screen position!
|
|
|
|
Gui::setViewCoords(x, y, z);
|
2012-02-21 01:44:10 -07:00
|
|
|
|
2012-03-03 06:38:24 -07:00
|
|
|
//If, for some reason, the cursor is active and the screen is still moving, move the cursor along with the screen
|
2012-10-06 03:46:20 -06:00
|
|
|
if (c_x != -30000 && !World::ReadPauseState())
|
2012-03-03 06:38:24 -07:00
|
|
|
Gui::setCursorCoords(c_x - (prevX-x), c_y - (prevY-y), z);
|
2012-02-22 10:43:14 -07:00
|
|
|
|
2012-03-03 06:38:24 -07:00
|
|
|
//Save this round's stuff for next time so we can monitor for changes made by the user
|
2015-02-14 20:53:06 -07:00
|
|
|
prevX = x;
|
2012-02-22 00:30:44 -07:00
|
|
|
prevY = y;
|
|
|
|
prevZ = z;
|
|
|
|
prevMenuWidth = menu_width;
|
|
|
|
|
2012-02-21 01:44:10 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result follow (color_ostream &out, std::vector <std::string> & parameters)
|
2012-02-22 10:43:14 -07:00
|
|
|
{
|
|
|
|
// HOTKEY COMMAND: CORE ALREADY SUSPENDED
|
|
|
|
|
2012-02-21 01:44:10 -07:00
|
|
|
if (!parameters.empty())
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
2012-02-22 00:30:44 -07:00
|
|
|
if (followedUnit)
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("No longer following previously selected unit.\n");
|
2012-02-22 00:30:44 -07:00
|
|
|
followedUnit = 0;
|
|
|
|
}
|
2012-03-10 04:55:42 -07:00
|
|
|
followedUnit = Gui::getSelectedUnit(out);
|
2012-02-21 01:44:10 -07:00
|
|
|
if (followedUnit)
|
|
|
|
{
|
2013-09-30 03:19:51 -06:00
|
|
|
is_enabled = true;
|
2012-02-22 10:43:14 -07:00
|
|
|
std::ostringstream ss;
|
2014-08-11 14:07:52 -06:00
|
|
|
ss << "Unpause to begin following " << world->raws.creatures.all[followedUnit->race]->name[0];
|
2012-02-22 10:43:14 -07:00
|
|
|
if (followedUnit->name.has_name) ss << " " << followedUnit->name.first_name;
|
|
|
|
ss << ". Simply manually move the view to break the following.\n";
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print(ss.str().c_str());
|
2012-02-21 01:44:10 -07:00
|
|
|
}
|
|
|
|
else followedUnit = 0;
|
2013-09-30 03:19:51 -06:00
|
|
|
is_enabled = (followedUnit != NULL);
|
2012-02-21 01:44:10 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|