dfhack/shmserver/mod-maps.cpp

119 lines
4.1 KiB
C++

2010-03-04 10:35:26 -07:00
#include <string>
#include <vector>
#include <integers.h>
#include "shms.h"
2010-03-04 16:05:01 -07:00
#include "mod-core.h"
2010-03-04 10:35:26 -07:00
#include "mod-maps.h"
#include <DFTypes.h>
using namespace DFHack;
using namespace DFHack::Maps;
#include <string.h>
#include <malloc.h>
extern char *shm;
//TODO: circular buffer streaming primitives required
//TODO: commands can fail without the proper offsets. Hot to handle that?
#define SHMHDR ((shm_maps_hdr *)shm)
#define SHMCMD ((shm_cmd *)shm)->pingpong
2010-03-11 16:13:50 -07:00
#define SHMDATA(type) ((type *)(shm + SHM_HEADER))
2010-03-04 10:35:26 -07:00
void NullCommand (void* data)
{
};
void InitOffsets (void* data)
{
maps_modulestate * state = (maps_modulestate *) data;
2010-03-11 16:13:50 -07:00
memcpy((void *) &(state->offsets), SHMDATA(void), sizeof(maps_offsets));
2010-03-04 10:35:26 -07:00
((maps_modulestate *) data)->inited = true;
}
void GetMapSize (void *data)
{
maps_modulestate * state = (maps_modulestate *) data;
if(state->inited)
{
2010-03-04 10:46:37 -07:00
SHMHDR->x = *(uint32_t *) (state->offsets.x_count_offset);
SHMHDR->y = *(uint32_t *) (state->offsets.y_count_offset);
SHMHDR->z = *(uint32_t *) (state->offsets.z_count_offset);
2010-03-04 10:35:26 -07:00
SHMHDR->error = false;
}
else
{
SHMHDR->error = true;
}
}
struct mblock
{
uint32_t * ptr_to_dirty;
};
2010-03-05 08:06:02 -07:00
inline void ReadBlockByAddress (void * data)
2010-03-04 10:35:26 -07:00
{
maps_modulestate * state = (maps_modulestate *) data;
maps_offsets & offsets = state->offsets;
2010-03-05 08:06:02 -07:00
mblock * block = (mblock *) SHMHDR->address;
2010-03-04 10:35:26 -07:00
if(block)
{
2010-03-11 16:13:50 -07:00
memcpy(&(SHMDATA(mapblock40d)->tiletypes), ((char *) block) + offsets.tile_type_offset, sizeof(SHMDATA(mapblock40d)->tiletypes));
2010-03-25 16:07:43 -06:00
memcpy(&(SHMDATA(mapblock40d)->designation), ((char *) block) + offsets.designation_offset, sizeof(SHMDATA(mapblock40d)->designation));
2010-03-11 16:13:50 -07:00
memcpy(&(SHMDATA(mapblock40d)->occupancy), ((char *) block) + offsets.occupancy_offset, sizeof(SHMDATA(mapblock40d)->occupancy));
memcpy(&(SHMDATA(mapblock40d)->biome_indices), ((char *) block) + offsets.biome_stuffs, sizeof(SHMDATA(mapblock40d)->biome_indices));
SHMDATA(mapblock40d)->blockflags.whole = *block->ptr_to_dirty;
2010-03-11 16:13:50 -07:00
SHMDATA(mapblock40d)->origin = (uint32_t)block;
2010-03-04 10:35:26 -07:00
SHMHDR->error = false;
}
else
{
SHMHDR->error = true;
}
}
2010-03-05 08:06:02 -07:00
void ReadBlockByCoords (void * data)
{
maps_modulestate * state = (maps_modulestate *) data;
maps_offsets & offsets = state->offsets;
/* map_offset is a pointer to
a pointer to
an X block of pointers to
an Y blocks of pointers to
a Z blocks of pointers to
map blocks
only Z blocks can have NULL pointers? TODO: verify
*/
mblock * *** mapArray = *(mblock * ****)offsets.map_offset;
SHMHDR->address = (uint32_t) mapArray[SHMHDR->x][SHMHDR->y][SHMHDR->z];
ReadBlockByAddress(data); // I wonder... will this inline properly?
}
2010-03-04 16:05:01 -07:00
DFPP_module InitMaps( void )
2010-03-04 10:35:26 -07:00
{
DFPP_module maps;
maps.name = "Maps";
maps.version = MAPS_VERSION;
// freed by the core
maps.modulestate = malloc(sizeof(maps_modulestate)); // we store a flag
memset(maps.modulestate,0,sizeof(maps_modulestate));
maps.reserve(NUM_MAPS_CMDS);
// client sends a maps_offsets struct -> inited = true;
maps.set_command(MAP_INIT, FUNCTION, "Supply the module with offsets",InitOffsets,CORE_SUSPENDED);
maps.set_command(MAP_GET_SIZE, FUNCTION, "Get map size in 16x16x1 tile blocks", GetMapSize, CORE_SUSPENDED);
maps.set_command(MAP_READ_BLOCK_BY_COORDS, FUNCTION, "Read the whole block with specified coords", ReadBlockByCoords, CORE_SUSPENDED);
2010-03-05 08:06:02 -07:00
maps.set_command(MAP_READ_BLOCK_BY_ADDRESS, FUNCTION, "Read the whole block from an address", ReadBlockByAddress, CORE_SUSPENDED);
2010-03-04 10:35:26 -07:00
// will it fit into 1MB? We shouldn't assume this is the case
maps.set_command(MAP_READ_BLOCKTREE, FUNCTION,"Get the tree of block pointers as a single structure", NullCommand, CORE_SUSPENDED);
2010-03-04 16:05:01 -07:00
2010-03-04 10:35:26 -07:00
// really doesn't fit into 1MB, there should be a streaming variant to better utilize context switches
maps.set_command(MAP_READ_BLOCKS_3D, FUNCTION, "Read a range of blocks between two sets of coords", NullCommand, CORE_SUSPENDED);
2010-03-04 16:05:01 -07:00
return maps;
2010-03-04 10:35:26 -07:00
}