2010-05-12 07:27:51 -06:00
|
|
|
/*
|
|
|
|
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-26 04:24:45 -06:00
|
|
|
#include "Internal.h"
|
|
|
|
#include "ContextShared.h"
|
2010-05-26 00:42:09 -06:00
|
|
|
#include "dfhack/modules/World.h"
|
2010-05-25 22:48:23 -06:00
|
|
|
#include "dfhack/DFProcess.h"
|
2010-08-20 06:10:05 -06:00
|
|
|
#include "dfhack/VersionInfo.h"
|
2010-05-25 22:48:23 -06:00
|
|
|
#include "dfhack/DFTypes.h"
|
2010-09-01 10:03:52 -06:00
|
|
|
#include "dfhack/DFError.h"
|
2011-03-18 01:53:59 -06:00
|
|
|
#include "ModuleFactory.h"
|
2010-05-12 07:27:51 -06:00
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
|
2011-03-18 01:53:59 -06:00
|
|
|
Module* DFHack::createWorld(DFContextShared * d)
|
|
|
|
{
|
|
|
|
return new World(d);
|
|
|
|
}
|
|
|
|
|
2010-05-12 07:27:51 -06:00
|
|
|
struct World::Private
|
|
|
|
{
|
2011-03-18 04:09:26 -06:00
|
|
|
Private()
|
|
|
|
{
|
|
|
|
Inited = StartedTime = StartedWeather = StartedMode = PauseInited = false;
|
|
|
|
}
|
2010-05-12 07:27:51 -06:00
|
|
|
bool Inited;
|
2011-03-18 04:09:26 -06:00
|
|
|
|
|
|
|
bool PauseInited;
|
|
|
|
uint32_t pause_state_offset;
|
|
|
|
|
2010-09-01 10:03:52 -06:00
|
|
|
bool StartedTime;
|
2010-05-12 07:27:51 -06:00
|
|
|
uint32_t year_offset;
|
|
|
|
uint32_t tick_offset;
|
2011-03-18 04:09:26 -06:00
|
|
|
|
|
|
|
bool StartedWeather;
|
2010-09-01 09:22:19 -06:00
|
|
|
uint32_t weather_offset;
|
2011-03-18 04:09:26 -06:00
|
|
|
|
|
|
|
bool StartedMode;
|
2011-02-28 22:59:23 -07:00
|
|
|
uint32_t gamemode_offset;
|
2011-03-01 14:18:26 -07:00
|
|
|
uint32_t controlmode_offset;
|
|
|
|
uint32_t controlmodecopy_offset;
|
2010-05-26 04:24:45 -06:00
|
|
|
DFContextShared *d;
|
2010-05-12 07:27:51 -06:00
|
|
|
Process * owner;
|
|
|
|
};
|
|
|
|
|
2010-05-26 04:24:45 -06:00
|
|
|
World::World(DFContextShared * _d)
|
2010-05-12 07:27:51 -06:00
|
|
|
{
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-05-12 07:27:51 -06:00
|
|
|
d = new Private;
|
|
|
|
d->d = _d;
|
|
|
|
d->owner = _d->p;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2010-08-29 16:08:17 -06:00
|
|
|
OffsetGroup * OG_World = d->d->offset_descriptor->getGroup("World");
|
2010-09-01 10:03:52 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
d->year_offset = OG_World->getAddress( "current_year" );
|
|
|
|
d->tick_offset = OG_World->getAddress( "current_tick" );
|
|
|
|
d->StartedTime = true;
|
|
|
|
}
|
|
|
|
catch(Error::All &){};
|
2011-03-18 04:09:26 -06:00
|
|
|
OffsetGroup * OG_Gui = d->d->offset_descriptor->getGroup("GUI"); // FIXME: legacy
|
|
|
|
try
|
|
|
|
{
|
|
|
|
d->pause_state_offset = OG_Gui->getAddress ("pause_state");
|
|
|
|
d->PauseInited = true;
|
|
|
|
}
|
|
|
|
catch(exception &){};
|
2010-09-01 10:03:52 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
d->weather_offset = OG_World->getAddress( "current_weather" );
|
|
|
|
d->StartedWeather = true;
|
|
|
|
}
|
|
|
|
catch(Error::All &){};
|
2011-02-28 22:59:23 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
d->gamemode_offset = OG_World->getAddress( "game_mode" );
|
2011-03-01 14:18:26 -07:00
|
|
|
d->controlmode_offset = OG_World->getAddress( "control_mode" );
|
2011-02-28 22:59:23 -07:00
|
|
|
d->StartedMode = true;
|
|
|
|
}
|
|
|
|
catch(Error::All &){};
|
2010-09-01 10:03:52 -06:00
|
|
|
d->Inited = true;
|
2010-05-12 07:27:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
World::~World()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool World::Start()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool World::Finish()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-18 04:09:26 -06:00
|
|
|
bool World::ReadPauseState()
|
|
|
|
{
|
|
|
|
if(!d->PauseInited) return false;
|
|
|
|
|
|
|
|
uint32_t pauseState = d->owner->readDWord (d->pause_state_offset);
|
|
|
|
return pauseState & 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void World::SetPauseState(bool paused)
|
|
|
|
{
|
|
|
|
if(!d->PauseInited) return;
|
|
|
|
d->owner->writeDWord (d->pause_state_offset, paused);
|
|
|
|
}
|
|
|
|
|
2010-05-12 07:27:51 -06:00
|
|
|
uint32_t World::ReadCurrentYear()
|
|
|
|
{
|
2010-09-01 10:03:52 -06:00
|
|
|
if(d->Inited && d->StartedTime)
|
2010-05-12 07:27:51 -06:00
|
|
|
return(d->owner->readDWord(d->year_offset));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t World::ReadCurrentTick()
|
|
|
|
{
|
2010-09-01 10:03:52 -06:00
|
|
|
if(d->Inited && d->StartedTime)
|
2010-05-12 07:27:51 -06:00
|
|
|
return(d->owner->readDWord(d->tick_offset));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-01 14:18:26 -07:00
|
|
|
bool World::ReadGameMode(t_gamemodes& rd)
|
2011-02-28 22:59:23 -07:00
|
|
|
{
|
|
|
|
if(d->Inited && d->StartedMode)
|
2011-03-01 14:18:26 -07:00
|
|
|
{
|
|
|
|
rd.control_mode = (ControlMode) d->owner->readDWord( d->controlmode_offset);
|
|
|
|
rd.game_mode = (GameMode) d->owner->readDWord(d->gamemode_offset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool World::WriteGameMode(const t_gamemodes & wr)
|
|
|
|
{
|
|
|
|
if(d->Inited && d->StartedMode)
|
|
|
|
{
|
|
|
|
d->owner->writeDWord(d->gamemode_offset,wr.game_mode);
|
|
|
|
d->owner->writeDWord(d->controlmode_offset,wr.control_mode);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-02-28 22:59:23 -07:00
|
|
|
}
|
|
|
|
|
2011-03-18 04:09:26 -06:00
|
|
|
/*
|
|
|
|
FIXME: Japa said that he had to do this with the time stuff he got from here
|
|
|
|
Investigate.
|
|
|
|
|
|
|
|
currentYear = Wold->ReadCurrentYear();
|
|
|
|
currentTick = Wold->ReadCurrentTick();
|
|
|
|
currentMonth = (currentTick+9)/33600;
|
|
|
|
currentDay = ((currentTick+9)%33600)/1200;
|
|
|
|
currentHour = ((currentTick+9)-(((currentMonth*28)+currentDay)*1200))/50;
|
|
|
|
currentTickRel = (currentTick+9)-(((((currentMonth*28)+currentDay)*24)+currentHour)*50);
|
|
|
|
*/
|
|
|
|
|
2010-08-20 06:10:05 -06:00
|
|
|
// FIX'D according to this:
|
2010-06-05 16:56:09 -06:00
|
|
|
/*
|
|
|
|
World::ReadCurrentMonth and World::ReadCurrentDay
|
|
|
|
« Sent to: peterix on: June 04, 2010, 04:44:30 »
|
|
|
|
« You have forwarded or responded to this message. »
|
2011-03-19 23:20:23 -06:00
|
|
|
|
2010-06-05 16:56:09 -06:00
|
|
|
Shouldn't these be /28 and %28 instead of 24? There're 28 days in a DF month.
|
|
|
|
Using 28 and doing the calculation on the value stored at the memory location
|
|
|
|
specified by memory.xml gets me the current month/date.
|
|
|
|
*/
|
2010-05-12 07:27:51 -06:00
|
|
|
uint32_t World::ReadCurrentMonth()
|
|
|
|
{
|
2010-06-05 16:56:09 -06:00
|
|
|
return this->ReadCurrentTick() / 1200 / 28;
|
2010-05-12 07:27:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t World::ReadCurrentDay()
|
|
|
|
{
|
2010-06-05 16:56:09 -06:00
|
|
|
return ((this->ReadCurrentTick() / 1200) % 28) + 1;
|
2010-05-12 07:27:51 -06:00
|
|
|
}
|
2010-09-01 09:22:19 -06:00
|
|
|
|
|
|
|
uint8_t World::ReadCurrentWeather()
|
|
|
|
{
|
2010-09-01 10:03:52 -06:00
|
|
|
if (d->Inited && d->StartedWeather)
|
2010-09-16 07:09:42 -06:00
|
|
|
return(d->owner->readByte(d->weather_offset + 12));
|
2010-09-01 09:22:19 -06:00
|
|
|
return 0;
|
|
|
|
}
|
2011-03-18 04:09:26 -06:00
|
|
|
|
2010-09-16 07:09:42 -06:00
|
|
|
void World::SetCurrentWeather(uint8_t weather)
|
|
|
|
{
|
|
|
|
if (d->Inited && d->StartedWeather)
|
|
|
|
{
|
|
|
|
uint8_t buf[25];
|
|
|
|
memset(&buf,weather, sizeof(buf));
|
|
|
|
d->owner->write(d->weather_offset,sizeof(buf),buf);
|
|
|
|
}
|
|
|
|
}
|