Skip to content

Commit e6e1972

Browse files
authored
Support adv look cursor
* Update dwarfmode.lua - Make cursor fns support adv mode look * Update Gui.cpp - Make cursor fns support adv mode look * Update changevein.cpp - Use getCursorPos * Update createitem.cpp - Use getCursorPos * Update cursecheck.cpp - Doesn't use cursor * Update stripcaged.cpp - Doesn't use cursor * Update zone.cpp - Use getCursorPos
1 parent d88277f commit e6e1972

File tree

7 files changed

+109
-42
lines changed

7 files changed

+109
-42
lines changed

library/lua/gui/dwarfmode.lua

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local utils = require('utils')
77

88
local dscreen = dfhack.screen
99

10+
local a_look = df.global.game.main_interface.adventure.look
1011
local g_cursor = df.global.cursor
1112
local g_sel_rect = df.global.selection_rect
1213
local world_map = df.global.world.map
@@ -38,17 +39,35 @@ end
3839

3940
---@return df.coord|nil
4041
function getCursorPos()
41-
if g_cursor.x >= 0 then
42+
if dfhack.world.isAdventureMode() then
43+
if a_look.open then
44+
return copyall(a_look.cursor)
45+
end
46+
elseif g_cursor.x >= 0 then
4247
return copyall(g_cursor)
4348
end
4449
end
4550

4651
function setCursorPos(cursor)
47-
df.global.cursor = copyall(cursor)
52+
if dfhack.world.isAdventureMode() then
53+
a_look.cursor = copyall(cursor)
54+
else
55+
df.global.cursor = copyall(cursor)
56+
end
4857
end
4958

5059
function clearCursorPos()
51-
df.global.cursor = xyz2pos(nil)
60+
if dfhack.world.isAdventureMode() then
61+
if not a_look.open then
62+
return
63+
end
64+
local u = dfhack.world.getAdventurer()
65+
if u and u.pos:isValid() then
66+
a_look.cursor = copyall(u.pos)
67+
end
68+
else
69+
df.global.cursor = xyz2pos(nil)
70+
end
5271
end
5372

5473
function getSelection()

library/modules/Gui.cpp

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,9 +2744,18 @@ df::coord Gui::getViewportPos()
27442744
df::coord Gui::getCursorPos()
27452745
{
27462746
using df::global::cursor;
2747+
if (World::isAdventureMode())
2748+
{
2749+
if (!game)
2750+
return df::coord();
2751+
auto &look = game->main_interface.adventure.look;
2752+
if (!look.open)
2753+
return df::coord();
2754+
return look.cursor;
2755+
}
2756+
27472757
if (!cursor)
27482758
return df::coord();
2749-
27502759
return df::coord(cursor->x, cursor->y, cursor->z);
27512760
}
27522761

@@ -2911,56 +2920,91 @@ bool Gui::inRenameBuilding()
29112920
return false;
29122921
}
29132922

2914-
bool Gui::getViewCoords (int32_t &x, int32_t &y, int32_t &z)
2923+
bool Gui::getViewCoords(int32_t &x, int32_t &y, int32_t &z)
29152924
{
29162925
x = *df::global::window_x;
29172926
y = *df::global::window_y;
29182927
z = *df::global::window_z;
29192928
return true;
29202929
}
29212930

2922-
bool Gui::setViewCoords (const int32_t x, const int32_t y, const int32_t z)
2931+
bool Gui::setViewCoords(const int32_t x, const int32_t y, const int32_t z)
29232932
{
29242933
(*df::global::window_x) = x;
29252934
(*df::global::window_y) = y;
29262935
(*df::global::window_z) = z;
29272936
return true;
29282937
}
29292938

2930-
bool Gui::getCursorCoords (int32_t &x, int32_t &y, int32_t &z)
2939+
bool Gui::getCursorCoords(int32_t &x, int32_t &y, int32_t &z)
29312940
{
2932-
x = df::global::cursor->x;
2933-
y = df::global::cursor->y;
2934-
z = df::global::cursor->z;
2941+
using df::global::cursor;
2942+
bool is_adv = World::isAdventureMode();
2943+
if (is_adv || !cursor)
2944+
{
2945+
df::coord p;
2946+
if (is_adv && game)
2947+
{
2948+
auto &look = game->main_interface.adventure.look;
2949+
if (look.open)
2950+
p = look.cursor;
2951+
}
2952+
x = p.x; y = p.y; z = p.z;
2953+
return p.isValid();
2954+
}
2955+
2956+
x = cursor->x; y = cursor->y; z = cursor->z;
29352957
return has_cursor();
29362958
}
29372959

2938-
bool Gui::getCursorCoords (df::coord &pos)
2960+
bool Gui::getCursorCoords(df::coord &pos)
29392961
{
2940-
pos.x = df::global::cursor->x;
2941-
pos.y = df::global::cursor->y;
2942-
pos.z = df::global::cursor->z;
2943-
return has_cursor();
2962+
using df::global::cursor;
2963+
df::coord p;
2964+
if (World::isAdventureMode())
2965+
{
2966+
if (game)
2967+
{
2968+
auto &look = game->main_interface.adventure.look;
2969+
if (look.open)
2970+
p = look.cursor;
2971+
}
2972+
}
2973+
else if (cursor)
2974+
p = df::coord(cursor->x, cursor->y, cursor->z);
2975+
2976+
pos = p;
2977+
return p.isValid();
29442978
}
29452979

29462980
//FIXME: confine writing of coords to map bounds?
2947-
bool Gui::setCursorCoords (const int32_t x, const int32_t y, const int32_t z)
2981+
bool Gui::setCursorCoords(const int32_t x, const int32_t y, const int32_t z)
29482982
{
2949-
df::global::cursor->x = x;
2950-
df::global::cursor->y = y;
2951-
df::global::cursor->z = z;
2983+
using df::global::cursor;
2984+
if (World::isAdventureMode())
2985+
{
2986+
if (!game)
2987+
return false;
2988+
auto &look = game->main_interface.adventure.look;
2989+
look.cursor = df::coord(x, y, z);
2990+
return true;
2991+
}
2992+
if (!cursor)
2993+
return false;
2994+
2995+
cursor->x = x; cursor->y = y; cursor->z = z;
29522996
return true;
29532997
}
29542998

2955-
bool Gui::getDesignationCoords (int32_t &x, int32_t &y, int32_t &z)
2999+
bool Gui::getDesignationCoords(int32_t &x, int32_t &y, int32_t &z)
29563000
{
29573001
x = selection_rect->start_x;
29583002
y = selection_rect->start_y;
29593003
z = selection_rect->start_z;
29603004
return (x >= 0) ? false : true;
29613005
}
29623006

2963-
bool Gui::setDesignationCoords (const int32_t x, const int32_t y, const int32_t z)
3007+
bool Gui::setDesignationCoords(const int32_t x, const int32_t y, const int32_t z)
29643008
{
29653009
selection_rect->start_x = x;
29663010
selection_rect->start_y = y;

plugins/changevein.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Allow changing the material of a mineral inclusion
22

33
#include "Console.h"
4+
#include "DataDefs.h"
45
#include "Export.h"
56
#include "PluginManager.h"
7+
#include "TileTypes.h"
68

7-
#include "DataDefs.h"
9+
#include "modules/Gui.h"
810
#include "modules/Maps.h"
911
#include "modules/Materials.h"
10-
#include "TileTypes.h"
1112

1213
#include "df/block_square_event.h"
1314
#include "df/block_square_event_mineralst.h"
@@ -21,7 +22,6 @@ using namespace df::enums;
2122

2223
DFHACK_PLUGIN("changevein");
2324
REQUIRE_GLOBAL(world);
24-
REQUIRE_GLOBAL(cursor);
2525

2626
constexpr uint8_t NORTH = 0;
2727
constexpr uint8_t EAST = 1;
@@ -212,7 +212,8 @@ command_result df_changevein (color_ostream &out, vector <string> & parameters)
212212
out.printerr("Map is not available!\n");
213213
return CR_FAILURE;
214214
}
215-
if (!cursor || cursor->x == -30000)
215+
auto pos = Gui::getCursorPos();
216+
if (!pos.isValid())
216217
{
217218
out.printerr("No cursor detected - please place the cursor over a mineral vein.\n");
218219
return CR_FAILURE;
@@ -232,14 +233,14 @@ command_result df_changevein (color_ostream &out, vector <string> & parameters)
232233
return CR_FAILURE;
233234
}
234235

235-
df::map_block *block = Maps::getTileBlock(cursor->x, cursor->y, cursor->z);
236+
auto block = Maps::getTileBlock(pos);
236237
if (!block)
237238
{
238239
out.printerr("Invalid tile selected.\n");
239240
return CR_FAILURE;
240241
}
241242
df::block_square_event_mineralst *mineral = NULL;
242-
int tx = cursor->x % 16, ty = cursor->y % 16;
243+
int tx = pos.x % 16, ty = pos.y % 16;
243244
for (auto evt : block->block_events)
244245
{
245246
if (evt->getType() != block_square_event_type::mineral)

plugins/createitem.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ using namespace DFHack;
3030
using namespace df::enums;
3131

3232
DFHACK_PLUGIN("createitem");
33-
REQUIRE_GLOBAL(cursor);
3433
REQUIRE_GLOBAL(world);
3534
REQUIRE_GLOBAL(gametype);
3635
REQUIRE_GLOBAL(cur_year_tick);
@@ -84,7 +83,10 @@ bool makeItem(df::unit *unit, df::item_type type, int16_t subtype, int16_t mat_t
8483
out_items[i]->moveToGround(building->centerx, building->centery, building->z);
8584
}
8685
else if (move_to_cursor)
87-
out_items[i]->moveToGround(cursor->x, cursor->y, cursor->z);
86+
{
87+
auto pos = Gui::getCursorPos();
88+
out_items[i]->moveToGround(pos.x, pos.y, pos.z);
89+
}
8890
// else createItem() already put it on the floor at the unit's feet, so we're good
8991

9092
// Special logic for creating proper gloves in pairs
@@ -395,11 +397,13 @@ command_result df_createitem (color_ostream &out, vector<string> &parameters) {
395397

396398
auto unit = Gui::getSelectedUnit(out, true);
397399
if (!unit) {
400+
auto pos = Gui::getCursorPos();
398401
if (*gametype == game_type::ADVENTURE_ARENA || World::isAdventureMode())
399402
{ // Use the adventurer unit
400403
unit = World::getAdventurer();
404+
move_to_cursor = pos.isValid();
401405
}
402-
else if (cursor->x >= 0)
406+
else if (pos.isValid())
403407
{ // Use the first possible citizen if possible, otherwise the first unit
404408
for (auto u : Units::citizensRange(world->units.active)) {
405409
unit = u;

plugins/cursecheck.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// cursecheck plugin
22
//
3-
// check single tile or whole map/world for cursed creatures by checking if a valid curse date (!=-1) is set
4-
// if a cursor is active only the selected tile will be observed
5-
// without cursor the whole map will be checked
3+
// check unit or whole map/world for cursed creatures by checking if a valid curse date (!=-1) is set
4+
// if a unit is selected only the selected unit will be observed
5+
// otherwise the whole map will be checked
66
// by default cursed creatures will be only counted
77
//
88
// the tool was intended to help finding vampires but it will also list necromancers, werebeasts and zombies
@@ -38,7 +38,6 @@ using namespace df::enums;
3838

3939
DFHACK_PLUGIN("cursecheck");
4040
REQUIRE_GLOBAL(world);
41-
REQUIRE_GLOBAL(cursor);
4241

4342
enum curses {
4443
None = 0,

plugins/devel/stripcaged.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ using std::string;
3535
using namespace DFHack;
3636
using namespace df::enums;
3737
using df::global::world;
38-
using df::global::cursor;
3938
using df::global::plotinfo;
4039

4140
using namespace DFHack::Gui;

plugins/zone.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ using std::vector;
5555
5656
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
5757
58-
REQUIRE_GLOBAL(cursor);
5958
REQUIRE_GLOBAL(gps);
6059
REQUIRE_GLOBAL(plotinfo);
6160
REQUIRE_GLOBAL(ui_building_item_cursor);
@@ -844,14 +843,15 @@ static void chainInfo(color_ostream & out, df::building* building, bool list_ref
844843
static df::building* getAssignableBuildingAtCursor(color_ostream& out)
845844
{
846845
// set building at cursor position to be new target building
847-
if (cursor->x == -30000)
846+
auto pos = Gui::getCursorPos();
847+
if (!pos.isValid())
848848
{
849849
out.printerr("No cursor; place cursor over activity zone, pen,"
850850
" pasture, pit, pond, chain, or cage.\n");
851851
return NULL;
852852
}
853853
854-
auto building_at_tile = Buildings::findAtTile(Gui::getCursorPos());
854+
auto building_at_tile = Buildings::findAtTile(pos);
855855
856856
// cagezone wants a pen/pit as starting point
857857
if (isCage(building_at_tile))
@@ -861,7 +861,7 @@ static df::building* getAssignableBuildingAtCursor(color_ostream& out)
861861
}
862862
else
863863
{
864-
auto zone_at_tile = Buildings::findPenPitAt(Gui::getCursorPos());
864+
auto zone_at_tile = Buildings::findPenPitAt(pos);
865865
if(!zone_at_tile)
866866
{
867867
out << "No pen/pasture, pit, or cage under cursor!" << endl;
@@ -1069,7 +1069,8 @@ static command_result df_zone(color_ostream &out, vector <string> & parameters)
10691069
}
10701070
else if(p0 == "zinfo")
10711071
{
1072-
if (cursor->x == -30000) {
1072+
auto pos = Gui::getCursorPos();
1073+
if (!pos.isValid()) {
10731074
out.color(COLOR_RED);
10741075
out << "No cursor; place cursor over activity zone, chain, or cage." << endl;
10751076
out.reset_color();
@@ -1081,10 +1082,10 @@ static command_result df_zone(color_ostream &out, vector <string> & parameters)
10811082
// (doesn't use the findXyzAtCursor() methods because zones might
10821083
// overlap and contain a cage or chain)
10831084
vector<df::building_civzonest*> zones;
1084-
Buildings::findCivzonesAt(&zones, Gui::getCursorPos());
1085+
Buildings::findCivzonesAt(&zones, pos);
10851086
for (auto zone = zones.begin(); zone != zones.end(); ++zone)
10861087
zoneInfo(out, *zone, verbose);
1087-
df::building* building = Buildings::findAtTile(Gui::getCursorPos());
1088+
df::building* building = Buildings::findAtTile(pos);
10881089
chainInfo(out, building, verbose);
10891090
cageInfo(out, building, verbose);
10901091
return CR_OK;

0 commit comments

Comments
 (0)