diff --git a/dfhack/CMakeLists.txt b/dfhack/CMakeLists.txt index 92d74b16d..f9a078363 100644 --- a/dfhack/CMakeLists.txt +++ b/dfhack/CMakeLists.txt @@ -25,6 +25,7 @@ APIPrivate.cpp DFTileTypes.cpp DFVector.cpp DFHackAPI_C.cpp +DFTypes_C.cpp depends/md5/md5.cpp depends/md5/md5wrapper.cpp diff --git a/dfhack/DFTypes_C.cpp b/dfhack/DFTypes_C.cpp new file mode 100644 index 000000000..c5f39908f --- /dev/null +++ b/dfhack/DFTypes_C.cpp @@ -0,0 +1,226 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +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. +*/ + +#include "integers.h" +#include +#include "string.h" +#include +#include + +using namespace std; + +#include "DFCommonInternal.h" +#include "DFTypes.h" +#include "modules/Materials.h" +#include "DFTypes_C.h" + +using namespace DFHack; + +#ifdef __cplusplus +extern "C" { +#endif + +c_colormodifier* ColorModifier_New() +{ + c_colormodifier* temp; + + temp = (c_colormodifier*)malloc(sizeof(c_colormodifier)); + + if(temp == NULL) + return NULL; + + temp->part[0] = '\0'; + temp->colorlist = NULL; + temp->colorlistLength = 0; + + return temp; +} + +void ColorModifier_Free(c_colormodifier* src) +{ + if(src != NULL) + { + if(src->colorlist != NULL) + free(src->colorlist); + + free(src); + } +} + +c_creaturecaste* CreatureCaste_New() +{ + c_creaturecaste* temp; + + temp = (c_creaturecaste*)malloc(sizeof(c_creaturecaste)); + + if(temp == NULL) + return NULL; + + temp->rawname[0] = '\0'; + temp->singular[0] = '\0'; + temp->plural[0] = '\0'; + temp->adjective[0] = '\0'; + + temp->ColorModifier = NULL; + temp->colorModifierLength = 0; + + temp->bodypart = NULL; + temp->bodypartLength = 0; + + return temp; +} + +void CreatureCaste_Free(c_creaturecaste* src) +{ + if(src != NULL) + { + if(src->bodypart != NULL) + free(src->bodypart); + + if(src->ColorModifier != NULL) + { + for(int i = 0; i < src->colorModifierLength; i++) + ColorModifier_Free(&src->ColorModifier[i]); + + free(src->ColorModifier); + } + + free(src); + } +} + +c_creaturetype* CreatureType_New() +{ + c_creaturetype* temp; + + temp = (c_creaturetype*)malloc(sizeof(c_creaturetype)); + + if(temp == NULL) + return NULL; + + temp->rawname[0] = '\0'; + + temp->castes = NULL; + temp->castesCount = 0; + + temp->extract = NULL; + temp->extractCount = 0; + + temp->tile_character = 0; + + temp->tilecolor.fore = 0; + temp->tilecolor.back = 0; + temp->tilecolor.bright = 0; + + return temp; +} + +void CreatureType_Free(c_creaturetype* src) +{ + if(src != NULL) + { + if(src->castes != NULL) + { + for(int i = 0; i < src->castesCount; i++) + CreatureCaste_Free(&src->castes[i]); + + free(src->castes); + } + + if(src->extract != NULL) + free(src->extract); + + free(src); + } +} + +#ifdef __cplusplus +} +#endif + +int ColorListConvert(t_colormodifier* src, c_colormodifier* dest) +{ + if(src == NULL || dest == NULL) + return -1; + + strcpy(dest->part, src->part); + + dest->colorlistLength = src->colorlist.size(); + dest->colorlist = (uint32_t*)malloc(sizeof(uint32_t) * dest->colorlistLength); + + copy(src->colorlist.begin(), src->colorlist.end(), dest->colorlist); + + return 1; +} + +int CreatureCasteConvert(t_creaturecaste* src, c_creaturecaste* dest) +{ + if(src == NULL || dest == NULL) + return -1; + + strcpy(dest->rawname, src->rawname); + strcpy(dest->singular, src->singular); + strcpy(dest->plural, src->plural); + strcpy(dest->adjective, src->adjective); + + dest->colorModifierLength = src->ColorModifier.size(); + dest->ColorModifier = (c_colormodifier*)malloc(sizeof(c_colormodifier) * dest->colorModifierLength); + + for(int i = 0; i < dest->colorModifierLength; i++) + ColorListConvert(&src->ColorModifier[i], &dest->ColorModifier[i]); + + dest->bodypartLength = src->bodypart.size(); + dest->bodypart = (t_bodypart*)malloc(sizeof(t_bodypart) * dest->bodypartLength); + + copy(src->bodypart.begin(), src->bodypart.end(), dest->bodypart); + + return 1; +} + +int CreatureTypeConvert(t_creaturetype* src, c_creaturetype* dest) +{ + if(src == NULL || dest == NULL) + return -1; + + strcpy(dest->rawname, src->rawname); + + dest->tilecolor.fore = src->tilecolor.fore; + dest->tilecolor.back = src->tilecolor.back; + dest->tilecolor.bright = src->tilecolor.bright; + + dest->tile_character = src->tile_character; + + dest->castesCount = src->castes.size(); + dest->castes = (c_creaturecaste*)malloc(sizeof(c_creaturecaste) * dest->castesCount); + + for(int i = 0; i < dest->castesCount; i++) + CreatureCasteConvert(&src->castes[i], &dest->castes[i]); + + dest->extractCount = src->extract.size(); + dest->extract = (t_creatureextract*)malloc(sizeof(t_creatureextract) * dest->extractCount); + + copy(src->extract.begin(), src->extract.end(), dest->extract); + + return 1; +} \ No newline at end of file diff --git a/dfhack/include/DFTypes_C.h b/dfhack/include/DFTypes_C.h new file mode 100644 index 000000000..db078de44 --- /dev/null +++ b/dfhack/include/DFTypes_C.h @@ -0,0 +1,98 @@ +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf, doomchild + +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. +*/ + +#ifndef TYPES_C_API +#define TYPES_C_API + +#include "Export.h" +#include "integers.h" +#include "DFTypes.h" +#include "modules/Materials.h" +#include "DFHackAPI_C.h" + +using namespace DFHack; + +#ifdef __cplusplus +extern "C" { +#endif + +struct c_colormodifier +{ + char part[128]; + uint32_t* colorlist; + uint32_t colorlistLength; +}; + +c_colormodifier* ColorModifier_New(); +void ColorModifier_Free(c_colormodifier* src); + +struct c_creaturecaste +{ + char rawname[128]; + char singular[128]; + char plural[128]; + char adjective[128]; + + c_colormodifier* ColorModifier; + uint32_t colorModifierLength; + + t_bodypart* bodypart; + uint32_t bodypartLength; +}; + +c_creaturecaste* CreatureCaste_New(); +void CreatureCaste_Free(c_creaturecaste* src); + +struct c_creaturetype +{ + char rawname[128]; + + c_creaturecaste* castes; + uint32_t castesCount; + + t_creatureextract* extract; + uint32_t extractCount; + + uint8_t tile_character; + + struct + { + uint16_t fore; + uint16_t back; + uint16_t bright; + } tilecolor; +}; + +c_creaturetype* CreatureType_New(); +void CreatureType_Free(c_creaturetype* src); + +#ifdef __cplusplus +} +#endif + +int CreatureTypeConvert(t_creaturetype* src, c_creaturetype* dest); +int CreatureCasteConvert(t_creaturecaste* src, c_creaturecaste* dest); +int ColorListConvert(t_colormodifier* src, c_colormodifier* dest); + +#endif diff --git a/dfhack/include/modules/Materials_C.h b/dfhack/include/modules/Materials_C.h index 2e7765fdc..448f045de 100644 --- a/dfhack/include/modules/Materials_C.h +++ b/dfhack/include/modules/Materials_C.h @@ -30,6 +30,7 @@ distribution. #include "DFTypes.h" #include "modules/Materials.h" #include "DFHackAPI_C.h" +#include "DFTypes_C.h" using namespace DFHack; @@ -37,6 +38,10 @@ using namespace DFHack; extern "C" { #endif +typedef t_matgloss* (*MatglossBufferFunc)(int); +typedef t_descriptor_color* (*DescriptorColorBufferFunc)(int); +typedef t_matglossOther* (*MatglossOtherBufferFunc)(int); + DFHACK_EXPORT int Materials_ReadInorganicMaterials(DFHackObject* mat); DFHACK_EXPORT int Materials_ReadOrganicMaterials(DFHackObject* mat); DFHACK_EXPORT int Materials_ReadWoodMaterials(DFHackObject* mat); @@ -59,20 +64,20 @@ DFHACK_EXPORT int Materials_getRaceExSize(DFHackObject* mat); DFHACK_EXPORT int Materials_getColorSize(DFHackObject* mat); DFHACK_EXPORT int Materials_getOtherSize(DFHackObject* mat); -DFHACK_EXPORT int Materials_getInorganic(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create)(int)); -DFHACK_EXPORT int Materials_getOrganic(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create)( int)); -DFHACK_EXPORT int Materials_getTree(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create)(int)); -DFHACK_EXPORT int Materials_getPlant(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create)(int)); -DFHACK_EXPORT int Materials_getRace(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create)(int)); +DFHACK_EXPORT int Materials_getInorganic(DFHackObject* mat, MatglossBufferFunc callback); +DFHACK_EXPORT int Materials_getOrganic(DFHackObject* mat, MatglossBufferFunc callback); +DFHACK_EXPORT int Materials_getTree(DFHackObject* mat, MatglossBufferFunc callback); +DFHACK_EXPORT int Materials_getPlant(DFHackObject* mat, MatglossBufferFunc callback); +DFHACK_EXPORT int Materials_getRace(DFHackObject* mat, MatglossBufferFunc callback); /*doomchild: I haven't done getRaceEx yet, because I'm not sure about the best way to make the t_creaturetype struct accessible from C. */ -// DFHACK_EXPORT int Materials_getRaceEx(DFHackObject* mat, t_creaturetype* (*t_creaturetype_buffer_create)(int)); +//DFHACK_EXPORT int Materials_getRaceEx(DFHackObject* mat, c_creaturetype* (*c_creaturetype_buffer_create)(c_creaturetype_descriptor*, int)); -DFHACK_EXPORT int Materials_getColor(DFHackObject* mat, t_descriptor_color* (*t_descriptor_color_buffer_create)(int)); -DFHACK_EXPORT int Materials_getOther(DFHackObject* mat, t_matglossOther* (*t_matglossOther_buffer_create)(int)); +DFHACK_EXPORT int Materials_getColor(DFHackObject* mat, DescriptorColorBufferFunc callback); +DFHACK_EXPORT int Materials_getOther(DFHackObject* mat, MatglossOtherBufferFunc callback); #ifdef __cplusplus } diff --git a/dfhack/modules/Materials_C.cpp b/dfhack/modules/Materials_C.cpp index a0a6bd5b9..a78a8dab4 100644 --- a/dfhack/modules/Materials_C.cpp +++ b/dfhack/modules/Materials_C.cpp @@ -32,6 +32,7 @@ using namespace std; #include "DFCommonInternal.h" #include "DFTypes.h" #include "modules/Materials.h" +#include "DFTypes_C.h" #include "modules/Materials_C.h" using namespace DFHack; @@ -224,7 +225,7 @@ int Materials_getOtherSize(DFHackObject* mat) //vector getters -int Materials_getInorganic(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create)(int)) +int Materials_getInorganic(DFHackObject* mat, MatglossBufferFunc callback) { if(mat != NULL) { @@ -232,7 +233,7 @@ int Materials_getInorganic(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_cr if(materials->inorganic.size() > 0) { - t_matgloss* buf = ((*t_matgloss_buffer_create)(materials->inorganic.size())); + t_matgloss* buf = ((*callback)(materials->inorganic.size())); if(buf != NULL) { @@ -250,7 +251,7 @@ int Materials_getInorganic(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_cr return -1; } -int Materials_getOrganic(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create)(int)) +int Materials_getOrganic(DFHackObject* mat, MatglossBufferFunc callback) { if(mat != NULL) { @@ -258,7 +259,7 @@ int Materials_getOrganic(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_crea if(materials->organic.size() > 0) { - t_matgloss* buf = ((*t_matgloss_buffer_create)(materials->organic.size())); + t_matgloss* buf = ((*callback)(materials->organic.size())); if(buf != NULL) { @@ -276,7 +277,7 @@ int Materials_getOrganic(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_crea return -1; } -int Materials_getTree(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create)(int)) +int Materials_getTree(DFHackObject* mat, MatglossBufferFunc callback) { if(mat != NULL) { @@ -284,7 +285,7 @@ int Materials_getTree(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create) if(materials->tree.size() > 0) { - t_matgloss* buf = ((*t_matgloss_buffer_create)(materials->tree.size())); + t_matgloss* buf = ((*callback)(materials->tree.size())); if(buf != NULL) { @@ -302,7 +303,7 @@ int Materials_getTree(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create) return -1; } -int Materials_getPlant(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create)(int)) +int Materials_getPlant(DFHackObject* mat, MatglossBufferFunc callback) { if(mat != NULL) { @@ -310,7 +311,7 @@ int Materials_getPlant(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create if(materials->plant.size() > 0) { - t_matgloss* buf = ((*t_matgloss_buffer_create)(materials->plant.size())); + t_matgloss* buf = ((*callback)(materials->plant.size())); if(buf != NULL) { @@ -328,7 +329,7 @@ int Materials_getPlant(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create return -1; } -int Materials_getRace(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create)(int)) +int Materials_getRace(DFHackObject* mat, MatglossBufferFunc callback) { if(mat != NULL) { @@ -336,7 +337,7 @@ int Materials_getRace(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create) if(materials->race.size() > 0) { - t_matgloss* buf = ((*t_matgloss_buffer_create)(materials->race.size())); + t_matgloss* buf = ((*callback)(materials->race.size())); if(buf != NULL) { @@ -355,8 +356,48 @@ int Materials_getRace(DFHackObject* mat, t_matgloss* (*t_matgloss_buffer_create) } //race_ex getter goes here... +// int Materials_getRaceEx(DFHackObject* mat, c_creaturetype* (*c_creaturetype_buffer_create)(c_creaturetype_descriptor*, int)) +// { + // if(mat != NULL) + // { + // DFHack::Materials* materials = (DFHack::Materials*)mat; + + // if(materials->raceEx.size() > 0) + // { + // std::vector types = materials->raceEx; + // int typessize = types.size(); + + // c_creaturetype_descriptor* descriptors = (c_creaturetype_descriptor*)malloc(sizeof(c_creaturetype_descriptor) * typessize); + + // for(int i = 0; i < typessize; i++) + // { + // descriptors[i].castesCount = types[i].castes.size(); + // descriptors[i].extractCount = types[i].extract.size(); + // } + + // c_creaturetype* buf = ((*c_creaturetype_buffer_create)(descriptors, typessize)); + + // for(int i = 0; i < typessize; i++) + // { + // t_creaturetype current = types[i]; + + // strncpy(buf[i].rawname, current.rawname, 128); + // buf[i].rawname[127] = '\0'; + + // buf[i].tile_character = current.tile_character; + // buf[i].tilecolor = current.tilecolor; + + // current.extract.copy(buf[i].extract, current.extract.size()); + // } + + // free(descriptors); + // } + // } + + // return -1; +// } -int Materials_getColor(DFHackObject* mat, t_descriptor_color* (*t_descriptor_color_buffer_create)(int)) +int Materials_getColor(DFHackObject* mat, DescriptorColorBufferFunc callback) { if(mat != NULL) { @@ -364,7 +405,7 @@ int Materials_getColor(DFHackObject* mat, t_descriptor_color* (*t_descriptor_col if(materials->color.size() > 0) { - t_descriptor_color* buf = ((*t_descriptor_color_buffer_create)(materials->color.size())); + t_descriptor_color* buf = ((*callback)(materials->color.size())); if(buf != NULL) { @@ -382,7 +423,7 @@ int Materials_getColor(DFHackObject* mat, t_descriptor_color* (*t_descriptor_col return -1; } -int Materials_getOther(DFHackObject* mat, t_matglossOther* (*t_matglossOther_buffer_create)(int)) +int Materials_getOther(DFHackObject* mat, MatglossOtherBufferFunc callback) { if(mat != NULL) { @@ -390,7 +431,7 @@ int Materials_getOther(DFHackObject* mat, t_matglossOther* (*t_matglossOther_buf if(materials->other.size() > 0) { - t_matglossOther* buf = ((*t_matglossOther_buffer_create)(materials->other.size())); + t_matglossOther* buf = ((*callback)(materials->other.size())); if(buf != NULL) { diff --git a/dfhack/modules/World.cpp b/dfhack/modules/World.cpp index fe997da2a..fecb99712 100644 --- a/dfhack/modules/World.cpp +++ b/dfhack/modules/World.cpp @@ -22,6 +22,18 @@ must not be misrepresented as being the original software. distribution. */ +/* +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); + */ + #include "DFCommonInternal.h" #include "../private/APIPrivate.h" #include "modules/World.h"