bool BTMonsterEditor::updateActive(ObjectSerializer &serial, BitField &active, int modField) { BTFactory<BTMonster> &monsterList = BTCore::getCore()->getMonsterList(); bool refresh = false; unsigned int newXp = monsterList[current].calcXp(); if (newXp != prevXp) { XMLAction *xpField = serial.find("xp", NULL); unsigned int *xp = (reinterpret_cast<unsigned int*>(xpField->object)); *xp += newXp; *xp -= prevXp; prevXp = newXp; refresh = true; } if (modField == MONSTERLOC_RANGEDTYPE) { BitField old = active; XMLAction *curField = serial.find("rangedType", NULL); int type = *(reinterpret_cast<int*>(curField->object)); if (type == BTRANGEDTYPE_MAGIC) { if (!active.isSet(MONSTERLOC_RANGEDSPELL)) { active.set(MONSTERLOC_RANGEDSPELL); XMLAction *extraField = serial.find("rangedSpell", NULL); *(reinterpret_cast<int*>(extraField->object)) = 0; } } else active.clear(MONSTERLOC_RANGEDSPELL); return (refresh || (!(active == old))); } return refresh; }
int BTSerializedEditor::setup(ObjectSerializer &serial, BitField &active, std::vector<BTDisplay::selectItem> &items) { size_t current = 0; for (int i = 0; i < entries; ++i) { if (!active.isSet(i)) continue; XMLAction *curField = serial.find(field[i], NULL); if (current >= entries) items.push_back(BTDisplay::selectItem()); if (curField->getType() == XMLTYPE_OBJECT) { XMLObject *obj = reinterpret_cast<XMLObject*>(curField->object); items[current].name = std::string(description[i]) + ": " + obj->createString(); items[current].value = i; ++current; } else if (curField->getType() == XMLTYPE_CREATE) { XMLArray *obj = reinterpret_cast<XMLArray*>(curField->object); for (size_t k = 0; k < obj->size(); k++) { if (current >= items.size()) items.push_back(BTDisplay::selectItem()); items[current].name = std::string(description[i]) + ": " + obj->get(k)->createString(); items[current].value = i; ++current; } if (current >= items.size()) items.push_back(BTDisplay::selectItem()); items[current].name = std::string(description[i]) + ": <New>"; items[current].value = i; ++current; } else if (curField->getType() == XMLTYPE_VECTORSTRING) { std::vector<std::string> *obj = reinterpret_cast<std::vector<std::string>*>(curField->object); for (size_t k = 0; k < obj->size(); k++) { if (current >= items.size()) items.push_back(BTDisplay::selectItem()); items[current].name = std::string(description[i]) + ": " + (*obj)[k]; items[current].value = i; ++current; } if (current >= items.size()) items.push_back(BTDisplay::selectItem()); items[current].name = std::string(description[i]) + ": <New>"; items[current].value = i; ++current; } else { items[current].name = std::string(description[i]) + ": " + curField->createString(); items[current].value = i; ++current; } } return current; }
void BTSerializedEditor::editField(BTDisplay &d, ObjectSerializer &serial, const char *text, XMLAction *curField, int modField, int where) { int key; switch (curField->getType()) { case XMLTYPE_STDSTRING: { std::string val = curField->createString(); d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) *(reinterpret_cast<std::string*>(curField->object)) = val; break; } case XMLTYPE_STRING: { std::string val = curField->createString(); d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) { char *str = *(reinterpret_cast<char**>(curField->object)); if (str) { delete [] str; } int len = val.length(); str = new char[len + 1]; strncpy(str, val.c_str(), len); str[len] = 0; *(reinterpret_cast<char**>(curField->object)) = str; } break; } case XMLTYPE_BOOL: { BTDisplay::selectItem vals[2]; vals[0].name = "false"; vals[1].name = "true"; int lookupStart(0); int lookupCurrent((*(reinterpret_cast<bool*>(curField->object)) ? 1 : 0)); d.addSelection(vals, 2, lookupStart, lookupCurrent); if (27 != d.process()) { *(reinterpret_cast<bool*>(curField->object)) = lookupCurrent; } break; } case XMLTYPE_BITFIELD: { ValueLookup *lookup = reinterpret_cast<ValueLookup*>(curField->data); BitField *bits = reinterpret_cast<BitField*>(curField->object); BTDisplay::selectItem lookupItem[lookup->size()]; for (int i = 0; i < lookup->size(); ++i) { lookupItem[i].name = lookup->getName(i); if (bits->isSet(i)) lookupItem[i].first = '*'; } int lookupStart(0); int lookupCurrent(0); d.addSelection(lookupItem, lookup->size(), lookupStart, lookupCurrent); int key; while (27 != (key = d.process())) { if (bits->toggle(lookupCurrent)) lookupItem[lookupCurrent].first = '*'; else lookupItem[lookupCurrent].first = 0; } break; } case XMLTYPE_INT: { if (curField->data) { ValueLookup *lookup = reinterpret_cast<ValueLookup*>(curField->data); bool extra = ((curField->extra == EXTRA_NONE) ? false : true); BTDisplay::selectItem lookupItem[lookup->size() + (extra ? 1 : 0)]; int i = 0; if (extra) { lookupItem[0].name = curField->extraText; lookupItem[0].value = -1; ++i; } int endIndex = lookup->getEndIndex(); int lookupCurrent(0); int valIndex((*(reinterpret_cast<int*>(curField->object))) + (extra ? 1 : 0)); for (int curIndex = lookup->getFirstIndex(); curIndex != endIndex; curIndex = lookup->getNextIndex(curIndex)) { lookupItem[i].name = lookup->getName(curIndex); lookupItem[i].value = curIndex; if (curIndex == valIndex) lookupCurrent = i; ++i; } int lookupStart(0); d.addSelection(lookupItem, lookup->size() + (extra ? 1 : 0), lookupStart, lookupCurrent); if (27 != d.process()) { *(reinterpret_cast<int*>(curField->object)) = lookupItem[lookupCurrent].value; } } else { std::string val = curField->createString(); d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) *(reinterpret_cast<int*>(curField->object)) = atol(val.c_str()); } break; } case XMLTYPE_UINT: { std::string val = curField->createString(); d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) *(reinterpret_cast<unsigned int*>(curField->object)) = atol(val.c_str()); break; } case XMLTYPE_INT16: { std::string val = curField->createString(); d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) *(reinterpret_cast<int16_t*>(curField->object)) = atol(val.c_str()); break; } case XMLTYPE_VECTORUINT: { std::vector<unsigned int> *vec = reinterpret_cast<std::vector<unsigned int> *>(curField->object); std::string val; for (size_t i = 0; i < vec->size(); ++i) { if (i != 0) val += ","; char convert[30]; sprintf(convert, "%u", (*vec)[i]); val += convert; } d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) { size_t i = 0; const char *start = val.c_str(); for (const char *comma = strchr(val.c_str(), ','); (start) && (*start); ++i) { if (i < vec->size()) (*vec)[i] = atol(start); else vec->push_back(atol(start)); start = comma; if (start) { if ((*start) == ',') ++start; comma = strchr(start, ','); } } if (i < vec->size()) vec->resize(i); } break; } case XMLTYPE_OBJECT: { XMLObject *obj = reinterpret_cast<XMLObject*>(curField->object); BTDice *dice = dynamic_cast<BTDice*>(obj); if (dice) { std::ostringstream stream; stream << dice->getNumber(); std::string val = stream.str(); d.addReadString(std::string(text) + "- Number of Dice: ", 100, val); key = d.process(); if ('\r' == key) dice->setNumber(atol(val.c_str())); d.clearText(); stream.str(""); stream << dice->getType(); val = stream.str(); d.addReadString(std::string(text) + "- Type of Dice: ", 100, val); key = d.process(); if ('\r' == key) dice->setType(atol(val.c_str())); d.clearText(); stream.str(""); stream << dice->getModifier(); val = stream.str(); d.addReadString(std::string(text) + "- Modifier to Roll: ", 100, val); key = d.process(); if ('\r' == key) dice->setModifier(atol(val.c_str())); } else printf("Unsuppported type: %d\n", curField->getType()); break; } case XMLTYPE_CREATE: { XMLArray *obj = reinterpret_cast<XMLArray*>(curField->object); handleObject(d, obj->get(where), modField); break; } case XMLTYPE_PICTURE: { BTDisplayConfig *oldConfig = d.getConfig(); BTDisplayConfig config; XMLSerializer parser; config.serialize(&parser); parser.parse(BTDisplay::applyDisplayDir("data/pictureselect.xml").c_str(), true); d.setConfig(&config); d.clearText(); d.addText(text); int val(reinterpret_cast<PictureIndex*>(curField->object)->value); d.addSelectImage(val); key = d.process(); if ('\r' == key) reinterpret_cast<PictureIndex*>(curField->object)->value = val; d.clearImage(); d.setConfig(oldConfig); break; } case XMLTYPE_VECTORSTRING: { std::vector<std::string> *obj = reinterpret_cast<std::vector<std::string>*>(curField->object); std::string val = (*obj)[where]; d.addReadString(std::string(text) + ": ", 100, val); key = d.process(); if ('\r' == key) (*obj)[where] = val; break; } default: printf("Unsuppported type: %d\n", curField->getType()); break; } d.clearText(); }
void BTEditor::editSpecial(BTDisplay &d, BTSpecial *special) { if (NULL == special) { special = new BTSpecial; levelMap->addSpecial(special); } BTDisplayConfig *oldConfig = d.getConfig(); BTDisplayConfig config; XMLSerializer parser; config.serialize(&parser); parser.parse(BTDisplay::applyDisplayDir("data/specialedit.xml").c_str(), true); d.setConfig(&config); int start(0); int current(0); BTSpecialBody *body = special->getBody(); std::vector<operationList> ops; std::vector<BTDisplay::selectItem> list(2); list[0].name = std::string("Name: ") + special->getName(); list[1].name = "Flags: " + special->printFlags(false); buildOperationList(d, body, list, ops); d.addSelection(list.data(), list.size(), start, current); int key; char extra[6] = {BTKEY_INS, BTKEY_DEL, BTKEY_CTRL_C, BTKEY_CTRL_V, BTKEY_CTRL_X, 0}; while (27 != (key = d.process(extra))) { d.clearText(); if (current == 0) { std::string name = special->getName(); d.addReadString("Name: ", 25, name); key = d.process(); if ('\r' == key) special->setName(name); d.clearText(); list[0].name = std::string("Name: ") + special->getName(); } else if (current == 1) { BTSpecialFlagList &lookup = getSpecialFlagList(); BitField bits = special->getFlag(); BTDisplay::selectItem lookupItem[lookup.size()]; for (size_t i = 0; i < lookup.size(); ++i) { lookupItem[i].name = lookup.getName(i); if (bits.isSet(i)) lookupItem[i].first = '*'; } int lookupStart(0); int lookupCurrent(0); d.addSelection(lookupItem, lookup.size(), lookupStart, lookupCurrent); int key; while (27 != (key = d.process())) { if (bits.toggle(lookupCurrent)) lookupItem[lookupCurrent].first = '*'; else lookupItem[lookupCurrent].first = 0; } special->setFlag(bits); d.clearText(); list[1].name = "Flags: " + special->printFlags(false); } else { if (BTKEY_INS == key) { if ((ops[list[current].value].op != NULL) && (ops[list[current].value].parent != NULL)) { ops[list[current].value].parent->insertOperation(ops[list[current].value].op, new BTSpecialCommand(BTSPECIALCOMMAND_NOTHING)); } } else if (BTKEY_DEL == key) { if ((ops[list[current].value].op != NULL) && (ops[list[current].value].parent != NULL)) { ops[list[current].value].parent->eraseOperation(ops[list[current].value].op); } } else if (BTKEY_CTRL_X == key) { if ((ops[list[current].value].op != NULL) && (ops[list[current].value].parent != NULL)) { if (clipboard) delete clipboard; clipboard = ops[list[current].value].op->clone(); ops[list[current].value].parent->eraseOperation(ops[list[current].value].op); } } else if (BTKEY_CTRL_C == key) { if ((ops[list[current].value].op != NULL) && (ops[list[current].value].parent != NULL)) { if (clipboard) delete clipboard; clipboard = ops[list[current].value].op->clone(); } } else if (BTKEY_CTRL_V == key) { if ((ops[list[current].value].parent != NULL) && (clipboard)) { if (ops[list[current].value].op) ops[list[current].value].parent->insertOperation(ops[list[current].value].op, clipboard->clone()); else ops[list[current].value].parent->addOperation(clipboard->clone()); } } else if ('\r' == key) { BTSpecialOperation *op = editSpecialOperation(d, ops[list[current].value].op); if (op) { if (ops[list[current].value].op) ops[list[current].value].parent->replaceOperation(ops[list[current].value].op, op); else ops[list[current].value].parent->addOperation(op); } } } ops.clear(); list.resize(2); buildOperationList(d, body, list, ops); d.addSelection(list.data(), list.size(), start, current); } d.clearText(); d.setConfig(oldConfig); }
std::string BTCombatant::attack(BTCombatant *defender, bool melee, const std::string &cause, const std::string &effect, const BTDice &damageDice, IShort chanceXSpecial, IShort xSpecial, int &numAttacksLeft, int &activeNum, int toHitBonus /*= 0*/) { int hits = 0; int totalDamage = 0; bool dead = false; bool totalDrain = false; BitField finalSpecial; while ((defender->isAlive()) && (numAttacksLeft > 0)) { int roll = BTDice(1, 20).roll(); if ((1 != roll) && ((20 == roll) || (roll + toHit + toHitBonus >= defender->ac))) { ++hits; int damage = 0; BitField special; damage = damageDice.roll(); for (int i = 0; i < dmgBonus.size(); i++) damage += dmgBonus[i]->apply(melee); if ((BTEXTRADAMAGE_NONE != xSpecial) && (BTDice(1, 100).roll() <= chanceXSpecial)) special.set(xSpecial); if (damage < 0) damage = 0; totalDamage += damage; if (defender->takeHP(damage)) { dead = true; defender->deactivate(activeNum); } else { useAutoCombatSkill(melee, special); int maxSpecial = special.getMaxSet(); if (maxSpecial > -1) { std::string specialText; for (int i = 0; i <= maxSpecial; ++i) { if (!special.isSet(i)) continue; if (defender->savingThrow(BTSAVE_DIFFICULTY)) { special.clear(i); continue; } switch(i) { case BTEXTRADAMAGE_POISON: defender->status.set(BTSTATUS_POISONED); break; case BTEXTRADAMAGE_LEVELDRAIN: if (defender->drainLevel()) { totalDrain = true; defender->deactivate(activeNum); } break; case BTEXTRADAMAGE_INSANITY: defender->status.set(BTSTATUS_INSANE); break; case BTEXTRADAMAGE_AGED: if (defender->age()) { defender->deactivate(activeNum); } break; case BTEXTRADAMAGE_POSSESSION: defender->status.set(BTSTATUS_POSSESSED); break; case BTEXTRADAMAGE_PARALYSIS: defender->status.set(BTSTATUS_PARALYZED); break; case BTEXTRADAMAGE_STONED: defender->status.set(BTSTATUS_STONED); defender->deactivate(activeNum); break; case BTEXTRADAMAGE_CRITICALHIT: defender->status.set(BTSTATUS_DEAD); defender->deactivate(activeNum); break; case BTEXTRADAMAGE_ITEMZOT: if (!defender->drainItem(5)) { special.clear(BTEXTRADAMAGE_ITEMZOT); } break; case BTEXTRADAMAGE_POINTPHAZE: if (!defender->takeSP(5)) { special.clear(BTEXTRADAMAGE_POINTPHAZE); } break; default: break; } } finalSpecial |= special; } } } --numAttacksLeft; } std::string text = getName(); text += " "; text += cause; text += " "; std::string defenderName; text += defenderName = defender->getName(); if (0 < hits) { text += " "; text += effect; text += " "; char tmp[20]; if (hits > 1) { sprintf(tmp, "%d", hits); text += tmp; text += " times "; } text += "for "; sprintf(tmp, "%d", totalDamage); text += tmp; text += " points of damage"; if (dead) { text += ", killing "; text += genderPronouns[defender->getGender()]; } else { int maxSpecial = finalSpecial.getMaxSet(); if (maxSpecial > -1) { std::string specialText; for (int i = 0; i <= maxSpecial; ++i) { if (!finalSpecial.isSet(i)) continue; if ((specialText == "") || (maxSpecial == i)) specialText += " and"; else specialText += ","; switch(i) { case BTEXTRADAMAGE_POISON: specialText += " poisons"; break; case BTEXTRADAMAGE_LEVELDRAIN: specialText += " drains a level from "; specialText += defenderName; if (totalDrain) { specialText += " totally draining "; specialText += genderPronouns[defender->getGender()]; } break; case BTEXTRADAMAGE_INSANITY: specialText += " inflicts insanity"; break; case BTEXTRADAMAGE_AGED: specialText += " withers "; specialText += genderPronouns[defender->getGender()]; break; case BTEXTRADAMAGE_POSSESSION: specialText += " possesses"; break; case BTEXTRADAMAGE_PARALYSIS: specialText += " paralyzes"; break; case BTEXTRADAMAGE_STONED: specialText += " stones"; break; case BTEXTRADAMAGE_CRITICALHIT: specialText += " critically hits"; break; case BTEXTRADAMAGE_ITEMZOT: specialText += " drains an item"; break; case BTEXTRADAMAGE_POINTPHAZE: specialText += " absorbs spell points"; break; default: break; } } text += specialText; } } if (defender->isAlive()) text += "."; else text += "!"; } else text += ", but misses!"; return text; }
std::string BTCombatant::specialAttack(BTCombatant *defender, const BTDice &damageDice, IShort xSpecial, bool farRange, int &activeNum, bool *saved /*= NULL*/) { int totalDamage = 0; bool dead = false; bool totalDrain = false; BitField special; totalDamage = damageDice.roll(); if (farRange) totalDamage = totalDamage / 2; bool saveResult = false; if (saved) saveResult = *saved; else saveResult = defender->savingThrow(BTSAVE_DIFFICULTY); if (saveResult) { totalDamage = totalDamage / 2; } else if (BTEXTRADAMAGE_NONE != xSpecial) { special.set(xSpecial); } if (defender->takeHP(totalDamage)) { dead = true; defender->deactivate(activeNum); } else { int maxSpecial = special.getMaxSet(); if (maxSpecial > -1) { for (int i = 0; i <= maxSpecial; ++i) { if (!special.isSet(i)) continue; switch(i) { case BTEXTRADAMAGE_POISON: defender->status.set(BTSTATUS_POISONED); break; case BTEXTRADAMAGE_LEVELDRAIN: if (defender->drainLevel()) { totalDrain = true; defender->deactivate(activeNum); } break; case BTEXTRADAMAGE_INSANITY: defender->status.set(BTSTATUS_INSANE); break; case BTEXTRADAMAGE_AGED: if (defender->age()) { defender->deactivate(activeNum); } break; case BTEXTRADAMAGE_POSSESSION: defender->status.set(BTSTATUS_POSSESSED); break; case BTEXTRADAMAGE_PARALYSIS: defender->status.set(BTSTATUS_PARALYZED); break; case BTEXTRADAMAGE_STONED: defender->status.set(BTSTATUS_STONED); defender->deactivate(activeNum); break; case BTEXTRADAMAGE_CRITICALHIT: defender->status.set(BTSTATUS_DEAD); defender->deactivate(activeNum); break; case BTEXTRADAMAGE_ITEMZOT: if (!defender->drainItem(5)) { special.clear(BTEXTRADAMAGE_ITEMZOT); } break; case BTEXTRADAMAGE_POINTPHAZE: if (!defender->takeSP(5)) { special.clear(BTEXTRADAMAGE_POINTPHAZE); } break; default: break; } } } } std::string text; std::string defenderName; text = defenderName = defender->getName(); if (saveResult == false) { if (totalDamage > 0) { text += " takes "; char tmp[20]; sprintf(tmp, "%d", totalDamage); text += tmp; text += " points of damage"; } if (dead) { text += ", killing "; text += genderPronouns[defender->getGender()]; } else { int maxSpecial = special.getMaxSet(); if (maxSpecial > -1) { std::string specialText; for (int i = 0; i <= maxSpecial; ++i) { if (!special.isSet(i)) continue; if ((totalDamage > 0) || (specialText != "")) { if ((specialText == "") || (maxSpecial == i)) specialText += " and"; else specialText += ","; } switch(i) { case BTEXTRADAMAGE_POISON: specialText += " is poisoned"; break; case BTEXTRADAMAGE_LEVELDRAIN: specialText += " is drained of a level"; if (totalDrain) { specialText += " totally draining "; specialText += genderPronouns[defender->getGender()]; } break; case BTEXTRADAMAGE_INSANITY: specialText += " has gone insane"; break; case BTEXTRADAMAGE_AGED: specialText += " withers"; break; case BTEXTRADAMAGE_POSSESSION: specialText += " is possessed"; break; case BTEXTRADAMAGE_PARALYSIS: specialText += " is paralyzed"; break; case BTEXTRADAMAGE_STONED: specialText += " is stoned"; break; case BTEXTRADAMAGE_CRITICALHIT: specialText += " is killed"; break; case BTEXTRADAMAGE_ITEMZOT: specialText += " has an item drained"; break; case BTEXTRADAMAGE_POINTPHAZE: specialText += " is drained of spell points"; break; default: break; } } text += specialText; } } if (defender->isAlive()) text += "."; else text += "!"; } else { if (totalDamage > 0) { text += " saves and takes "; char tmp[20]; sprintf(tmp, "%d", totalDamage); text += tmp; text += " points of damage"; if (dead) { text += ", killing "; text += genderPronouns[defender->getGender()]; } text += "!"; } else text += " saves!"; } return text; }