dfhack/library/modules/Position.cpp

161 lines
4.5 KiB
C++

/*
www.sourceforge.net/projects/dfhack
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
2010-05-25 22:48:23 -06:00
#include "dfhack/DFCommonInternal.h"
#include "../private/APIPrivate.h"
2010-05-26 00:42:09 -06:00
#include "dfhack/modules/Position.h"
2010-05-25 22:48:23 -06:00
#include "dfhack/DFMemInfo.h"
#include "dfhack/DFProcess.h"
using namespace DFHack;
2010-04-04 16:48:19 -06:00
struct Position::Private
{
uint32_t window_x_offset;
uint32_t window_y_offset;
uint32_t window_z_offset;
uint32_t cursor_xyz_offset;
uint32_t window_dims_offset;
2010-05-25 19:22:17 -06:00
2010-04-10 18:32:50 -06:00
uint32_t hotkey_start;
uint32_t hotkey_mode_offset;
uint32_t hotkey_xyz_offset;
uint32_t hotkey_size;
2010-05-25 19:22:17 -06:00
2010-05-23 15:06:10 -06:00
DFContextPrivate *d;
2010-04-18 13:30:02 -06:00
Process * owner;
2010-04-04 16:48:19 -06:00
bool Inited;
bool Started;
2010-04-10 18:32:50 -06:00
bool StartedHotkeys;
2010-04-04 16:48:19 -06:00
};
2010-05-23 15:06:10 -06:00
Position::Position(DFContextPrivate * d_)
2010-04-04 16:48:19 -06:00
{
d = new Private;
d->d = d_;
2010-04-18 13:30:02 -06:00
d->owner = d_->p;
2010-04-10 18:32:50 -06:00
d->Inited = true;
d->StartedHotkeys = d->Started = false;
memory_info * mem;
try
{
mem = d->d->offset_descriptor;
d->window_x_offset = mem->getAddress ("window_x");
d->window_y_offset = mem->getAddress ("window_y");
d->window_z_offset = mem->getAddress ("window_z");
d->cursor_xyz_offset = mem->getAddress ("cursor_xyz");
d->window_dims_offset = mem->getAddress ("window_dims");
d->Started = true;
2010-05-25 19:22:17 -06:00
2010-04-10 18:32:50 -06:00
d->hotkey_start = mem->getAddress("hotkey_start");
d->hotkey_mode_offset = mem->getOffset ("hotkey_mode");
d->hotkey_xyz_offset = mem->getOffset("hotkey_xyz");
d->hotkey_size = mem->getHexValue("hotkey_size");
d->StartedHotkeys = true;
}
catch(exception &){};
2010-04-04 16:48:19 -06:00
}
Position::~Position()
{
delete d;
}
2010-04-10 18:32:50 -06:00
bool Position::ReadHotkeys(t_hotkey hotkeys[])
{
2010-05-25 19:22:17 -06:00
if (!d->StartedHotkeys)
{
return false;
}
2010-04-10 18:32:50 -06:00
uint32_t currHotkey = d->hotkey_start;
2010-04-18 13:30:02 -06:00
Process * p = d->owner;
2010-05-25 19:22:17 -06:00
2010-04-10 18:32:50 -06:00
for(uint32_t i = 0 ; i < NUM_HOTKEYS ;i++)
{
2010-04-18 13:30:02 -06:00
p->readSTLString(currHotkey,hotkeys[i].name,10);
hotkeys[i].mode = p->readWord(currHotkey+d->hotkey_mode_offset);
p->read (currHotkey + d->hotkey_xyz_offset, 3*sizeof (int32_t), (uint8_t *) &hotkeys[i].x);
2010-04-10 18:32:50 -06:00
currHotkey+=d->hotkey_size;
}
2010-04-10 18:32:50 -06:00
return true;
}
2010-04-10 18:32:50 -06:00
2010-04-04 16:48:19 -06:00
bool Position::getViewCoords (int32_t &x, int32_t &y, int32_t &z)
{
2010-04-04 16:48:19 -06:00
if (!d->Inited) return false;
2010-04-18 13:30:02 -06:00
Process * p = d->owner;
2010-05-25 19:22:17 -06:00
2010-04-18 13:30:02 -06:00
p->readDWord (d->window_x_offset, (uint32_t &) x);
p->readDWord (d->window_y_offset, (uint32_t &) y);
p->readDWord (d->window_z_offset, (uint32_t &) z);
return true;
}
//FIXME: confine writing of coords to map bounds?
2010-04-04 16:48:19 -06:00
bool Position::setViewCoords (const int32_t x, const int32_t y, const int32_t z)
{
2010-05-25 19:22:17 -06:00
if (!d->Inited)
{
return false;
}
2010-04-18 13:30:02 -06:00
Process * p = d->owner;
2010-05-25 19:22:17 -06:00
2010-04-18 13:30:02 -06:00
p->writeDWord (d->window_x_offset, (uint32_t) x);
p->writeDWord (d->window_y_offset, (uint32_t) y);
p->writeDWord (d->window_z_offset, (uint32_t) z);
return true;
}
2010-04-04 16:48:19 -06:00
bool Position::getCursorCoords (int32_t &x, int32_t &y, int32_t &z)
{
2010-04-04 16:48:19 -06:00
if(!d->Inited) return false;
int32_t coords[3];
2010-04-18 13:30:02 -06:00
d->owner->read (d->cursor_xyz_offset, 3*sizeof (int32_t), (uint8_t *) coords);
x = coords[0];
y = coords[1];
z = coords[2];
if (x == -30000) return false;
return true;
}
//FIXME: confine writing of coords to map bounds?
2010-04-04 16:48:19 -06:00
bool Position::setCursorCoords (const int32_t x, const int32_t y, const int32_t z)
{
2010-04-04 16:48:19 -06:00
if (!d->Inited) return false;
int32_t coords[3] = {x, y, z};
2010-04-18 13:30:02 -06:00
d->owner->write (d->cursor_xyz_offset, 3*sizeof (int32_t), (uint8_t *) coords);
return true;
}
2010-04-04 16:48:19 -06:00
bool Position::getWindowSize (int32_t &width, int32_t &height)
{
2010-04-04 16:48:19 -06:00
if(!d->Inited) return false;
2010-05-25 19:22:17 -06:00
int32_t coords[2];
2010-04-18 13:30:02 -06:00
d->owner->read (d->window_dims_offset, 2*sizeof (int32_t), (uint8_t *) coords);
width = coords[0];
height = coords[1];
return true;
2010-05-01 18:38:18 -06:00
}