2010-05-12 07:27:51 -06:00
|
|
|
/*
|
2011-06-16 15:53:39 -06:00
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
|
2010-05-12 07:27:51 -06:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-06-16 15:53:39 -06:00
|
|
|
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "Internal.h"
|
2011-04-10 02:19:15 -06:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <cstring>
|
|
|
|
using namespace std;
|
|
|
|
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "modules/World.h"
|
|
|
|
#include "MemAccess.h"
|
|
|
|
#include "VersionInfo.h"
|
|
|
|
#include "Types.h"
|
|
|
|
#include "Error.h"
|
2011-03-18 01:53:59 -06:00
|
|
|
#include "ModuleFactory.h"
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "Core.h"
|
2010-05-12 07:27:51 -06:00
|
|
|
|
2012-01-07 08:21:31 -07:00
|
|
|
#include "MiscUtils.h"
|
|
|
|
|
|
|
|
#include "DataDefs.h"
|
|
|
|
#include "df/world.h"
|
|
|
|
#include "df/historical_figure.h"
|
|
|
|
|
2010-05-12 07:27:51 -06:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2012-01-24 20:51:17 -07:00
|
|
|
using df::global::world;
|
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
Module* DFHack::createWorld()
|
2011-03-18 01:53:59 -06:00
|
|
|
{
|
2011-06-17 07:02:43 -06:00
|
|
|
return new World();
|
2011-03-18 01:53:59 -06:00
|
|
|
}
|
|
|
|
|
2010-05-12 07:27:51 -06:00
|
|
|
struct World::Private
|
|
|
|
{
|
2011-03-18 04:09:26 -06:00
|
|
|
Private()
|
|
|
|
{
|
2012-01-24 20:51:17 -07:00
|
|
|
Inited = PauseInited = StartedWeather = StartedMode = false;
|
2012-04-01 06:43:40 -06:00
|
|
|
next_persistent_id = 0;
|
2011-03-18 04:09:26 -06:00
|
|
|
}
|
2010-05-12 07:27:51 -06:00
|
|
|
bool Inited;
|
2011-03-18 04:09:26 -06:00
|
|
|
|
|
|
|
bool PauseInited;
|
|
|
|
bool StartedWeather;
|
|
|
|
bool StartedMode;
|
2011-07-16 17:00:50 -06:00
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
int next_persistent_id;
|
|
|
|
std::multimap<std::string, int> persistent_index;
|
|
|
|
|
2010-05-12 07:27:51 -06:00
|
|
|
Process * owner;
|
|
|
|
};
|
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
typedef std::pair<std::string, int> T_persistent_item;
|
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
World::World()
|
2010-05-12 07:27:51 -06:00
|
|
|
{
|
2011-06-17 07:02:43 -06:00
|
|
|
Core & c = Core::getInstance();
|
2010-05-12 07:27:51 -06:00
|
|
|
d = new Private;
|
2011-06-17 07:02:43 -06:00
|
|
|
d->owner = c.p;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2012-05-23 11:51:03 -06:00
|
|
|
if(df::global::pause_state)
|
2011-03-18 04:09:26 -06:00
|
|
|
d->PauseInited = true;
|
2012-01-24 20:51:17 -07:00
|
|
|
|
2012-05-23 11:51:03 -06:00
|
|
|
if(df::global::current_weather)
|
2010-09-01 10:03:52 -06:00
|
|
|
d->StartedWeather = true;
|
2012-06-16 04:42:56 -06:00
|
|
|
if (df::global::gamemode && df::global::gametype)
|
2012-05-23 11:51:03 -06:00
|
|
|
d->StartedMode = true;
|
2011-07-16 17:00:50 -06:00
|
|
|
|
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;
|
2012-05-23 11:51:03 -06:00
|
|
|
return *df::global::pause_state;
|
2011-03-18 04:09:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void World::SetPauseState(bool paused)
|
|
|
|
{
|
2012-05-23 11:51:03 -06:00
|
|
|
if (d->PauseInited)
|
|
|
|
*df::global::pause_state = paused;
|
2011-03-18 04:09:26 -06:00
|
|
|
}
|
|
|
|
|
2010-05-12 07:27:51 -06:00
|
|
|
uint32_t World::ReadCurrentYear()
|
|
|
|
{
|
2012-01-24 20:51:17 -07:00
|
|
|
return *df::global::cur_year;
|
2010-05-12 07:27:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t World::ReadCurrentTick()
|
|
|
|
{
|
2012-01-24 20:51:17 -07:00
|
|
|
return *df::global::cur_year_tick;
|
2010-05-12 07:27:51 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-06-16 04:42:56 -06:00
|
|
|
rd.g_mode = (DFHack::GameMode)*df::global::gamemode;
|
|
|
|
rd.g_type = (DFHack::GameType)*df::global::gametype;
|
2011-03-01 14:18:26 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool World::WriteGameMode(const t_gamemodes & wr)
|
|
|
|
{
|
|
|
|
if(d->Inited && d->StartedMode)
|
|
|
|
{
|
2012-06-16 04:42:56 -06:00
|
|
|
*df::global::gamemode = wr.g_mode;
|
|
|
|
*df::global::gametype = wr.g_type;
|
2011-03-01 14:18:26 -07:00
|
|
|
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)
|
2012-05-23 11:51:03 -06:00
|
|
|
return (*df::global::current_weather)[2][2];
|
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)
|
2012-05-23 11:51:03 -06:00
|
|
|
memset(df::global::current_weather, weather, 25);
|
2010-09-16 07:09:42 -06:00
|
|
|
}
|
2011-07-16 17:00:50 -06:00
|
|
|
|
|
|
|
string World::ReadWorldFolder()
|
|
|
|
{
|
2012-03-14 00:56:30 -06:00
|
|
|
return world->cur_savegame.save_dir;
|
2011-07-16 17:00:50 -06:00
|
|
|
}
|
2012-01-07 08:21:31 -07:00
|
|
|
|
|
|
|
static PersistentDataItem dataFromHFig(df::historical_figure *hfig)
|
|
|
|
{
|
|
|
|
return PersistentDataItem(hfig->id, hfig->name.first_name, &hfig->name.nickname, hfig->name.words);
|
|
|
|
}
|
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
void World::ClearPersistentCache()
|
|
|
|
{
|
|
|
|
d->next_persistent_id = 0;
|
|
|
|
d->persistent_index.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool World::BuildPersistentCache()
|
2012-01-07 08:21:31 -07:00
|
|
|
{
|
2012-04-01 06:43:40 -06:00
|
|
|
if (d->next_persistent_id)
|
|
|
|
return true;
|
|
|
|
if (!Core::getInstance().isWorldLoaded())
|
|
|
|
return false;
|
|
|
|
|
2012-01-07 08:21:31 -07:00
|
|
|
std::vector<df::historical_figure*> &hfvec = df::historical_figure::get_vector();
|
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
// Determine the next entry id as min(-100, lowest_id-1)
|
|
|
|
d->next_persistent_id = -100;
|
|
|
|
|
|
|
|
if (hfvec.size() > 0 && hfvec[0]->id <= -100)
|
|
|
|
d->next_persistent_id = hfvec[0]->id-1;
|
|
|
|
|
|
|
|
// Add the entries to the lookup table
|
|
|
|
d->persistent_index.clear();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < hfvec.size() && hfvec[i]->id <= -100; i++)
|
|
|
|
{
|
|
|
|
if (!hfvec[i]->name.has_name || hfvec[i]->name.first_name.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
d->persistent_index.insert(T_persistent_item(hfvec[i]->name.first_name, -hfvec[i]->id));
|
|
|
|
}
|
2012-04-05 02:32:10 -06:00
|
|
|
|
|
|
|
return true;
|
2012-04-01 06:43:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
PersistentDataItem World::AddPersistentData(const std::string &key)
|
|
|
|
{
|
|
|
|
if (!BuildPersistentCache() || key.empty())
|
|
|
|
return PersistentDataItem();
|
|
|
|
|
|
|
|
std::vector<df::historical_figure*> &hfvec = df::historical_figure::get_vector();
|
2012-01-07 08:21:31 -07:00
|
|
|
|
|
|
|
df::historical_figure *hfig = new df::historical_figure();
|
2012-04-01 06:43:40 -06:00
|
|
|
hfig->id = d->next_persistent_id--;
|
2012-01-07 08:21:31 -07:00
|
|
|
hfig->name.has_name = true;
|
|
|
|
hfig->name.first_name = key;
|
2012-01-08 09:02:12 -07:00
|
|
|
memset(hfig->name.words, 0xFF, sizeof(hfig->name.words));
|
2012-01-07 08:21:31 -07:00
|
|
|
|
|
|
|
hfvec.insert(hfvec.begin(), hfig);
|
2012-04-01 06:43:40 -06:00
|
|
|
|
|
|
|
d->persistent_index.insert(T_persistent_item(key, -hfig->id));
|
|
|
|
|
2012-01-07 08:21:31 -07:00
|
|
|
return dataFromHFig(hfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistentDataItem World::GetPersistentData(const std::string &key)
|
|
|
|
{
|
2012-04-01 06:43:40 -06:00
|
|
|
if (!BuildPersistentCache())
|
|
|
|
return PersistentDataItem();
|
2012-01-07 08:21:31 -07:00
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
auto it = d->persistent_index.find(key);
|
|
|
|
if (it != d->persistent_index.end())
|
|
|
|
return GetPersistentData(it->second);
|
2012-01-07 08:21:31 -07:00
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
return PersistentDataItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistentDataItem World::GetPersistentData(int entry_id)
|
|
|
|
{
|
|
|
|
if (entry_id < 100)
|
|
|
|
return PersistentDataItem();
|
|
|
|
|
|
|
|
auto hfig = df::historical_figure::find(-entry_id);
|
|
|
|
if (hfig && hfig->name.has_name)
|
|
|
|
return dataFromHFig(hfig);
|
2012-01-07 08:21:31 -07:00
|
|
|
|
|
|
|
return PersistentDataItem();
|
|
|
|
}
|
|
|
|
|
2012-04-05 01:32:23 -06:00
|
|
|
PersistentDataItem World::GetPersistentData(const std::string &key, bool *added)
|
|
|
|
{
|
2012-09-05 11:27:42 -06:00
|
|
|
if (added) *added = false;
|
2012-04-05 01:32:23 -06:00
|
|
|
|
|
|
|
PersistentDataItem rv = GetPersistentData(key);
|
|
|
|
|
|
|
|
if (!rv.isValid())
|
|
|
|
{
|
2012-09-05 11:27:42 -06:00
|
|
|
if (added) *added = true;
|
2012-04-05 01:32:23 -06:00
|
|
|
rv = AddPersistentData(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
void World::GetPersistentData(std::vector<PersistentDataItem> *vec, const std::string &key, bool prefix)
|
2012-01-07 08:21:31 -07:00
|
|
|
{
|
2012-09-12 10:57:25 -06:00
|
|
|
vec->clear();
|
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
if (!BuildPersistentCache())
|
|
|
|
return;
|
2012-01-07 08:21:31 -07:00
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
auto eqrange = d->persistent_index.equal_range(key);
|
|
|
|
|
|
|
|
if (prefix)
|
|
|
|
{
|
|
|
|
if (key.empty())
|
|
|
|
{
|
|
|
|
eqrange.first = d->persistent_index.begin();
|
|
|
|
eqrange.second = d->persistent_index.end();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string bound = key;
|
|
|
|
if (bound[bound.size()-1] != '/')
|
|
|
|
bound += "/";
|
|
|
|
eqrange.first = d->persistent_index.lower_bound(bound);
|
|
|
|
|
|
|
|
bound[bound.size()-1]++;
|
|
|
|
eqrange.second = d->persistent_index.lower_bound(bound);
|
|
|
|
}
|
|
|
|
}
|
2012-01-07 08:21:31 -07:00
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
for (auto it = eqrange.first; it != eqrange.second; ++it)
|
|
|
|
{
|
|
|
|
auto hfig = df::historical_figure::find(-it->second);
|
|
|
|
if (hfig && hfig->name.has_name)
|
2012-01-07 08:21:31 -07:00
|
|
|
vec->push_back(dataFromHFig(hfig));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
bool World::DeletePersistentData(const PersistentDataItem &item)
|
2012-01-07 08:21:31 -07:00
|
|
|
{
|
|
|
|
if (item.id > -100)
|
2012-04-01 06:43:40 -06:00
|
|
|
return false;
|
|
|
|
if (!BuildPersistentCache())
|
|
|
|
return false;
|
2012-01-07 08:21:31 -07:00
|
|
|
|
|
|
|
std::vector<df::historical_figure*> &hfvec = df::historical_figure::get_vector();
|
2012-04-01 06:43:40 -06:00
|
|
|
|
|
|
|
auto eqrange = d->persistent_index.equal_range(item.key_value);
|
|
|
|
|
2012-09-12 10:57:25 -06:00
|
|
|
for (auto it2 = eqrange.first; it2 != eqrange.second; )
|
2012-04-01 06:43:40 -06:00
|
|
|
{
|
2012-09-12 10:57:25 -06:00
|
|
|
auto it = it2; ++it2;
|
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
if (it->second != -item.id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
d->persistent_index.erase(it);
|
|
|
|
|
|
|
|
int idx = binsearch_index(hfvec, item.id);
|
|
|
|
|
|
|
|
if (idx >= 0) {
|
|
|
|
delete hfvec[idx];
|
|
|
|
hfvec.erase(hfvec.begin()+idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-01-07 08:21:31 -07:00
|
|
|
}
|
2012-04-01 06:43:40 -06:00
|
|
|
|
|
|
|
return false;
|
2012-01-07 08:21:31 -07:00
|
|
|
}
|