bool Item::serializeAttr(PropWriteStream& propWriteStream) const { if (isStackable() || isFluidContainer() || isSplash()) { uint8_t _count = getSubType(); propWriteStream.ADD_UCHAR(ATTR_COUNT); propWriteStream.ADD_UCHAR(_count); } if (hasCharges()) { uint16_t _count = getCharges(); propWriteStream.ADD_UCHAR(ATTR_CHARGES); propWriteStream.ADD_USHORT(_count); } if (!isNotMoveable()) { uint16_t _actionId = getActionId(); if (_actionId) { propWriteStream.ADD_UCHAR(ATTR_ACTION_ID); propWriteStream.ADD_USHORT(_actionId); } } const std::string& _text = getText(); if (!_text.empty()) { propWriteStream.ADD_UCHAR(ATTR_TEXT); propWriteStream.ADD_STRING(_text); } const time_t _writtenDate = getDate(); if (_writtenDate > 0) { propWriteStream.ADD_UCHAR(ATTR_WRITTENDATE); propWriteStream.ADD_ULONG(_writtenDate); } const std::string& _writer = getWriter(); if (!_writer.empty()) { propWriteStream.ADD_UCHAR(ATTR_WRITTENBY); propWriteStream.ADD_STRING(_writer); } const std::string& _specialDesc = getSpecialDescription(); if (!_specialDesc.empty()) { propWriteStream.ADD_UCHAR(ATTR_DESC); propWriteStream.ADD_STRING(_specialDesc); } if (hasAttribute(ATTR_ITEM_DURATION)) { uint32_t duration = getDuration(); propWriteStream.ADD_UCHAR(ATTR_DURATION); propWriteStream.ADD_ULONG(duration); } ItemDecayState_t decayState = getDecaying(); if (decayState == DECAYING_TRUE || decayState == DECAYING_PENDING) { propWriteStream.ADD_UCHAR(ATTR_DECAYING_STATE); propWriteStream.ADD_UCHAR(decayState); } return true; }
std::string Item::getXRayDescription() const { std::stringstream ret; ret << "ID: " << getID() << std::endl; uint16_t actionId = getActionId(); uint16_t uniqueId = getUniqueId(); if (actionId > 0) { ret << "Action ID: " << actionId << std::endl; } if (uniqueId > 0) { ret << "Unique ID: " << uniqueId << std::endl; } #ifdef __DEBUG__ if (getContainer()) { ret << "There are " << getContainer()->getTotalAmountOfItemsInside() - 1 << " things inside of this." << std::endl; } #endif ret << Thing::getXRayDescription(); return ret.str(); }
bool Item::canDecay() { if(isRemoved()) return false; if(loadedFromMap && (getUniqueId() || (getActionId() && getContainer()))) return false; const ItemType& it = Item::items[id]; return it.decayTo >= 0 && it.decayTime; }
void Item::resetActionId(bool callEvent/* = true*/) { if(!getActionId()) return; Tile* tile = NULL; if(callEvent) tile = getTile(); eraseAttribute("aid"); if(tile) g_moveEvents->onAddTileItem(tile, this); }
void Item::setActionId(int32_t aid, bool callEvent/* = true*/) { Tile* tile = NULL; if(callEvent) tile = getTile(); if(tile && getActionId()) g_moveEvents->onRemoveTileItem(tile, this); setAttribute("aid", aid); if(tile) g_moveEvents->onAddTileItem(tile, this); }
bool Item::canDecay() { if(isRemoved()){ return false; } if(getUniqueId() != 0 || getActionId() != 0){ return false; } const ItemType& it = Item::items[id]; if(it.decayTo == -1 || it.decayTime == 0){ return false; } return true; }
bool Item::serializeAttr(PropWriteStream& propWriteStream) { if(isStackable() || isSplash() || isFluidContainer()){ unsigned char _count = getItemCountOrSubtype(); propWriteStream.ADD_UCHAR(ATTR_COUNT); propWriteStream.ADD_UCHAR(_count); } if(isRune()){ unsigned char _count = getItemCharge(); propWriteStream.ADD_UCHAR(ATTR_RUNE_CHARGES); propWriteStream.ADD_UCHAR(_count); } if(!isNotMoveable() /*moveable*/){ if(actionId){ unsigned short _actionId = getActionId(); propWriteStream.ADD_UCHAR(ATTR_ACTION_ID); propWriteStream.ADD_USHORT(_actionId); } } /*we are not saving unique ids if(uniqueId){ unsigned short _uniqueId = getUniqueId(); propWriteStream.ADD_UCHAR(ATTR_UNIQUE_ID); propWriteStream.ADD_USHORT(_uniqueId); } */ const std::string& _text = getText(); if(_text.length() > 0){ propWriteStream.ADD_UCHAR(ATTR_TEXT); propWriteStream.ADD_STRING(_text); } const std::string& _specialDesc = getSpecialDescription(); if(_specialDesc.length() > 0){ propWriteStream.ADD_UCHAR(ATTR_DESC); propWriteStream.ADD_STRING(_specialDesc); } return true; }
int main(int argc, char* argv[]) { double discountFactor = 0.9; FILE* initFileFd = NULL; FILE** combinedFd = NULL; FILE* optimalFd = NULL; optimistic_instance* optimistic = NULL; random_search_instance* random_search = NULL; uct_instance* uct = NULL; uniform_instance* uniform = NULL; unsigned int maxDepth = 0; unsigned int i = 0; unsigned int n = 0; state** initialStates = NULL; unsigned int timestamp = time(NULL); double* optimalValues = NULL; int readFscanf = -1; struct arg_file* initFile = arg_file1(NULL, "init", "<file>", "File containing the inital state"); struct arg_int* d = arg_int1("d", NULL, "<n>", "Maximum depth of an uniform tree which the number of call per step"); struct arg_int* k = arg_int1("k", NULL, "<n>", "Branching factor of the problem"); struct arg_file* where = arg_file1(NULL, "where", "<file>", "Directory where we save the outputs"); struct arg_file* optimal = arg_file1(NULL, "optimal", "<file>", "File containing the optimal values"); struct arg_end* end = arg_end(6); void* argtable[6]; int nerrors = 0; argtable[0] = initFile; argtable[1] = where; argtable[2] = d; argtable[3] = k; argtable[4] = optimal; argtable[5] = end; if(arg_nullcheck(argtable) != 0) { printf("error: insufficient memory\n"); arg_freetable(argtable, 6); return EXIT_FAILURE; } nerrors = arg_parse(argc, argv, argtable); if(nerrors > 0) { printf("%s:", argv[0]); arg_print_syntax(stdout, argtable, "\n"); arg_print_errors(stdout, end, argv[0]); arg_freetable(argtable, 6); return EXIT_FAILURE; } initGenerativeModelParameters(); K = k->ival[0]; initGenerativeModel(); optimalFd = fopen(optimal->filename[0], "r"); initFileFd = fopen(initFile->filename[0], "r"); readFscanf = fscanf(initFileFd, "%u\n", &n); initialStates = (state**)malloc(sizeof(state*) * n); for(i = 0; i < n; i++) { char str[1024]; readFscanf = fscanf(initFileFd, "%s\n", str); initialStates[i] = makeState(str); } maxDepth = d->ival[0]; combinedFd = (FILE**)malloc(sizeof(FILE*) * maxDepth); for(i = 1; i <= maxDepth; i++) { char str[1024]; sprintf(str, "%s/%u_combined_%u_%u.csv", where->filename[0], timestamp, K, i); combinedFd[i - 1] = fopen(str, "w"); fprintf(combinedFd[i - 1], "n,optimistic,random search,uct,uniform\n"); } arg_freetable(argtable, 6); optimalValues = (double*)malloc(sizeof(double) * K); optimistic = optimistic_initInstance(initialStates[0], discountFactor); random_search = random_search_initInstance(initialStates[0], discountFactor); uct = uct_initInstance(initialStates[0], discountFactor); uniform = uniform_initInstance(initialStates[0], discountFactor); for(i = 0; i < n; i++) { unsigned int j = 1; unsigned int maxNbIterations = K; unsigned int optimalAction = 0; char str[1024]; readFscanf = fscanf(optimalFd, "%s\n", str); optimalValues[0] = strtod(strtok(str, ","), NULL); printf("%.15f,",optimalValues[0]); for(; j < K; j++) { optimalValues[j] = strtod(strtok(NULL, ","), NULL); printf("%.15f,",optimalValues[j]); } optimalAction = atol(strtok(NULL, ",")); printf("%u\n",optimalAction); for(j = 1; j <= maxDepth; j++) { unsigned int crtOptimalAction = getActionId(optimistic_planning(optimistic, maxNbIterations)); fprintf(combinedFd[j - 1], "%u,", maxNbIterations); fprintf(combinedFd[j - 1], "%.15f,", crtOptimalAction == optimalAction ? 0.0 : optimalValues[optimalAction] - optimalValues[crtOptimalAction]); maxNbIterations += pow(K, j+1); } if(i < (n - 1)) optimistic_resetInstance(optimistic, initialStates[i+1]); printf("optimistic: %uth initial state processed\n", i+1); fflush(NULL); maxNbIterations = K; for(j = 1; j <= maxDepth; j++) { unsigned int crtOptimalAction = getActionId(random_search_planning(random_search, maxNbIterations)); fprintf(combinedFd[j - 1], "%.15f,", crtOptimalAction == optimalAction ? 0.0 : optimalValues[optimalAction] - optimalValues[crtOptimalAction]); maxNbIterations += pow(K, j+1); } if(i < (n - 1)) random_search_resetInstance(random_search, initialStates[i + 1]); printf("random_search: %uth initial state processed\n", i+1); fflush(NULL); maxNbIterations = K; for(j = 1; j <= maxDepth; j++) { unsigned int crtOptimalAction = getActionId(uct_planning(uct, maxNbIterations)); fprintf(combinedFd[j - 1], "%.15f,", crtOptimalAction == optimalAction ? 0.0 : optimalValues[optimalAction] - optimalValues[crtOptimalAction]); maxNbIterations += pow(K, j+1); } if(i < (n - 1)) uct_resetInstance(uct, initialStates[i + 1]); printf("uct: %uth initial state processed\n", i+1); fflush(NULL); maxNbIterations = K; for(j = 1; j <= maxDepth; j++) { unsigned int crtOptimalAction = getActionId(uniform_planning(uniform, maxNbIterations)); fprintf(combinedFd[j - 1], "%.15f\n", crtOptimalAction == optimalAction ? 0.0 : optimalValues[optimalAction] - optimalValues[crtOptimalAction]); maxNbIterations += pow(K, j+1); } if(i < (n - 1)) uniform_resetInstance(uniform, initialStates[i + 1]); printf("uniform: %uth initial state processed\n", i+1); printf("%uth initial state processed\n", i+1); fflush(NULL); } for(i = 0; i < maxDepth; i++) { fclose(combinedFd[i]); } for(i = 0; i < n; i++) freeState(initialStates[i]); free(initialStates); free(combinedFd); optimistic_uninitInstance(&optimistic); random_search_uninitInstance(&random_search); uct_uninitInstance(&uct); uniform_uninitInstance(&uniform); freeGenerativeModel(); freeGenerativeModelParameters(); return EXIT_SUCCESS; }
bool Item::hasProperty(enum ITEMPROPERTY prop) const { const ItemType& it = items[id]; switch(prop) { case BLOCKSOLID: if(it.blockSolid) return true; break; case MOVABLE: if(it.movable && (!loadedFromMap || (!getUniqueId() && (!getActionId() || !getContainer())))) return true; break; case HASHEIGHT: if(it.hasHeight) return true; break; case BLOCKPROJECTILE: if(it.blockProjectile) return true; break; case BLOCKPATH: if(it.blockPathFind) return true; break; case ISVERTICAL: if(it.isVertical) return true; break; case ISHORIZONTAL: if(it.isHorizontal) return true; break; case IMMOVABLEBLOCKSOLID: if(it.blockSolid && (!it.movable || (loadedFromMap && (getUniqueId() || (getActionId() && getContainer()))))) return true; break; case IMMOVABLEBLOCKPATH: if(it.blockPathFind && (!it.movable || (loadedFromMap && (getUniqueId() || (getActionId() && getContainer()))))) return true; break; case SUPPORTHANGABLE: if(it.isHorizontal || it.isVertical) return true; break; case IMMOVABLENOFIELDBLOCKPATH: if(!it.isMagicField() && it.blockPathFind && (!it.movable || (loadedFromMap && (getUniqueId() || (getActionId() && getContainer()))))) return true; break; case NOFIELDBLOCKPATH: if(!it.isMagicField() && it.blockPathFind) return true; break; case FLOORCHANGEDOWN: if(it.floorChange[CHANGE_DOWN]) return true; break; case FLOORCHANGEUP: for(uint16_t i = CHANGE_FIRST; i <= CHANGE_PRE_LAST; ++i) { if(it.floorChange[i]) return true; } break; default: break; } return false; }
bool Item::hasProperty(enum ITEMPROPERTY prop) const { const ItemType& it = items[id]; switch(prop) { case BLOCKSOLID: if(it.blockSolid) return true; break; case MOVEABLE: if(it.moveable && (!loadedFromMap || (!getUniqueId() && (!getActionId() || getContainer())))) return true; break; case HASHEIGHT: if(it.hasHeight) return true; break; case BLOCKPROJECTILE: if(it.blockProjectile) return true; break; case BLOCKPATH: if(it.blockPathFind) return true; break; case ISVERTICAL: if(it.isVertical) return true; break; case ISHORIZONTAL: if(it.isHorizontal) return true; break; case IMMOVABLEBLOCKSOLID: if(it.blockSolid && (!it.moveable || (loadedFromMap && (getUniqueId() || (getActionId() && getContainer()))))) return true; break; case IMMOVABLEBLOCKPATH: if(it.blockPathFind && (!it.moveable || (loadedFromMap && (getUniqueId() || (getActionId() && getContainer()))))) return true; break; case SUPPORTHANGABLE: if(it.isHorizontal || it.isVertical) return true; break; case IMMOVABLENOFIELDBLOCKPATH: if(!it.isMagicField() && it.blockPathFind && (!it.moveable || (loadedFromMap && (getUniqueId() || (getActionId() && getContainer()))))) return true; break; case NOFIELDBLOCKPATH: if(!it.isMagicField() && it.blockPathFind) return true; break; default: break; } return false; }
xmlNodePtr Item::serialize() { xmlNodePtr nodeItem = xmlNewNode(NULL,(const xmlChar*)"item"); std::stringstream ss; ss.str(""); ss << getID(); xmlSetProp(nodeItem, (const xmlChar*)"id", (const xmlChar*)ss.str().c_str()); if(hasSubType()){ ss.str(""); ss << (int32_t)getSubType(); xmlSetProp(nodeItem, (const xmlChar*)"count", (const xmlChar*)ss.str().c_str()); } if(getSpecialDescription() != ""){ ss.str(""); ss << getSpecialDescription(); xmlSetProp(nodeItem, (const xmlChar*)"special_description", (const xmlChar*)ss.str().c_str()); } if(getText() != ""){ ss.str(""); ss << getText(); xmlSetProp(nodeItem, (const xmlChar*)"text", (const xmlChar*)ss.str().c_str()); } if(getWrittenDate() != 0){ ss.str(""); ss << getWrittenDate(); xmlSetProp(nodeItem, (const xmlChar*)"written_date", (const xmlChar*)ss.str().c_str()); } if(getWriter() != ""){ ss.str(""); ss << getWriter(); xmlSetProp(nodeItem, (const xmlChar*)"writer", (const xmlChar*)ss.str().c_str()); } if(!isNotMoveable() /*moveable*/){ if(getActionId() != 0){ ss.str(""); ss << getActionId(); xmlSetProp(nodeItem, (const xmlChar*)"actionId", (const xmlChar*)ss.str().c_str()); } } if(hasAttribute(ATTR_ITEM_DURATION)){ uint32_t duration = getDuration(); ss.str(""); ss << duration; xmlSetProp(nodeItem, (const xmlChar*)"duration", (const xmlChar*)ss.str().c_str()); } uint32_t decayState = getDecaying(); if(decayState == DECAYING_TRUE || decayState == DECAYING_PENDING){ ss.str(""); ss << decayState; xmlSetProp(nodeItem, (const xmlChar*)"decayState", (const xmlChar*)ss.str().c_str()); } return nodeItem; }