Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new beds behavior since 12.20.9260 #235

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions data/items/items.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50365,26 +50365,49 @@
<attribute key="containersize" value="6"/>
<attribute key="weight" value="1700"/>
</item>
<item id="35851" name="magnificent bed"/>
<item id="35852" name="magnificent bed">
<attribute key="description" value="Somebody is sleeping there"/>
<item fromid="35851" toid="35858" article="a" name="magnificent bed">
<attribute key="type" value="bed"/>
</item>
<item id="35853" name="magnificent bed"/>
<item id="35854" name="magnificent bed">
<attribute key="description" value="Somebody is sleeping there"/>
<item id="35859" article="a" name="magnificent bed">
<attribute key="type" value="bed"/>
<attribute key="bedpart" value="pillow"/>
<attribute key="bedpartof" value="35860"/>
<attribute key="partnerdirection" value="south"/>
<attribute key="femaletransformto" value="35855"/>
<attribute key="maletransformto" value="35851"/>
<attribute key="wrapableto" value="23398"/>
<attribute key="rotateTo" value="35861"/>
</item>
<item id="35855" name="magnificent bed"/>
<item id="35856" name="magnificent bed">
<attribute key="description" value="Somebody is sleeping there"/>
<item id="35860" article="a" name="magnificent bed">
<attribute key="type" value="bed"/>
<attribute key="bedpart" value="blanket"/>
<attribute key="bedpartof" value="35859"/>
<attribute key="partnerdirection" value="north"/>
<attribute key="femaletransformto" value="35856"/>
<attribute key="maletransformto" value="35852"/>
<attribute key="wrapableto" value="23398"/>
<attribute key="rotateTo" value="35862"/>
</item>
<item id="35857" name="magnificent bed"/>
<item id="35858" name="magnificent bed">
<attribute key="description" value="Somebody is sleeping there"/>
<item id="35861" article="a" name="magnificent bed">
<attribute key="type" value="bed"/>
<attribute key="bedpart" value="pillow"/>
<attribute key="bedpartof" value="35862"/>
<attribute key="partnerdirection" value="east"/>
<attribute key="femaletransformto" value="35857"/>
<attribute key="maletransformto" value="35853"/>
<attribute key="wrapableto" value="23398"/>
<attribute key="rotateTo" value="35859"/>
</item>
<item id="35862" article="a" name="magnificent bed">
<attribute key="type" value="bed"/>
<attribute key="bedpart" value="blanket"/>
<attribute key="bedpartof" value="35861"/>
<attribute key="partnerdirection" value="west"/>
<attribute key="femaletransformto" value="35858"/>
<attribute key="maletransformto" value="35854"/>
<attribute key="wrapableto" value="23398"/>
<attribute key="rotateTo" value="35860"/>
</item>
<item id="35859" name="magnificent bed"/>
<item id="35860" name="magnificent bed"/>
<item id="35861" name="magnificent bed"/>
<item id="35862" name="magnificent bed"/>
<item id="35863" name="ornate bed"/>
<item id="35864" name="ornate bed">
<attribute key="description" value="Somebody is sleeping there"/>
Expand Down
14 changes: 13 additions & 1 deletion src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3387,11 +3387,12 @@ void Game::playerWrapableItem(uint32_t playerId, const Position& pos, uint8_t st
Item* item = thing->getItem();
Tile* tile = map.getTile(item->getPosition());
HouseTile* houseTile = dynamic_cast<HouseTile*>(tile);
House* getHouse = houseTile->getHouse();
if (!tile->hasFlag(TILESTATE_PROTECTIONZONE) || !houseTile) {
player->sendCancelMessage("You may construct this only inside a house.");
return;
}
if (houseTile->getHouse() != house) {
if (getHouse != house) {
player->sendCancelMessage("Only owners can wrap/unwrap inside a house.");
return;
}
Expand Down Expand Up @@ -3446,6 +3447,9 @@ void Game::playerWrapableItem(uint32_t playerId, const Position& pos, uint8_t st
hiddenCharges = item->getSubType();
}
uint16_t oldItemID = item->getID();
if (Item::items.getItemType(item->getID()).isBed()) {
getHouse->removeBed(item->getBed());
}
addMagicEffect(item->getPosition(), CONST_ME_POFF);
Item* newItem = transformItem(item, ITEM_DECORATION_KIT);
ItemAttributes::CustomAttribute customAttribute;
Expand All @@ -3460,7 +3464,15 @@ void Game::playerWrapableItem(uint32_t playerId, const Position& pos, uint8_t st
}
else if (item->getID() == ITEM_DECORATION_KIT && unWrapId != 0) {
uint16_t hiddenCharges = item->getDate();
const ItemType& newiType = Item::items.getItemType(unWrapId);
if (newiType.isBed() && getHouse->getBedCount() + 1 > getHouse->getMaxBeds()) {
player->sendCancelMessage("You reached the maximum beds in this house");
return;
}
Item* newItem = transformItem(item, unWrapId);
if (newiType.isBed()) {
getHouse->addBed(newItem->getBed());
}
if (newItem) {
if (hiddenCharges > 0 && isCaskItem(unWrapId)) {
newItem->setSubType(hiddenCharges);
Expand Down
2 changes: 1 addition & 1 deletion src/io/iomapserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool IOMapSerialize::loadItem(PropStream& propStream, Cylinder* parent)
}

const ItemType& iType = Item::items[id];
if (iType.moveable || !tile || iType.isCarpet()) {
if (iType.moveable || !tile || iType.isCarpet() || iType.isBed()) {
//create a new item
Item* item = Item::CreateItem(id);
if (item) {
Expand Down
8 changes: 8 additions & 0 deletions src/items/bed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ bool BedItem::canUse(Player* player)
return false;
}

if (getNextBedItem() == nullptr) {
return false;
}

if (Item::items[id].bedPart != BED_PILLOW_PART) {
return false;
}

if (sleeperGUID == 0) {
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions src/items/functions/item_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,12 @@ void ItemParse::parseBeds(const std::string& tmpStrValue, pugi::xml_attribute va
itemType.transformToOnUse[PLAYERSEX_MALE] = valueFemale;
}
}

if (stringValue == "bedpart") {
itemType.bedPart = getBedPart(valueAttribute.as_string());
} else if (stringValue == "bedpartof") {
itemType.bedPartOf = pugi::cast<uint16_t>(valueAttribute.value());
}
}

void ItemParse::parseElement(const std::string& tmpStrValue, pugi::xml_attribute valueAttribute, ItemType& itemType) {
Expand Down
2 changes: 2 additions & 0 deletions src/items/functions/item_parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ const phmap::flat_hash_map<std::string, ItemParseAttributes_t> ItemParseAttribut
{"malesleeper", ITEM_PARSE_MALETRANSFORMTO},
{"femaletransformto", ITEM_PARSE_FEMALETRANSFORMTO},
{"femalesleeper", ITEM_PARSE_FEMALETRANSFORMTO},
{"bedpart", ITEM_PARSE_PARTNERDIRECTION},
{"bedpartof", ITEM_PARSE_PARTNERDIRECTION},
{"transformto", ITEM_PARSE_TRANSFORMTO},
{"destroyto", ITEM_PARSE_DESTROYTO},
{"elementice", ITEM_PARSE_ELEMENTICE},
Expand Down
2 changes: 2 additions & 0 deletions src/items/items.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,11 @@ class ItemType
uint16_t slotPosition = SLOTP_HAND;
uint16_t speed = 0;
uint16_t wareId = 0;
uint16_t bedPartOf = 0;

MagicEffectClasses magicEffect = CONST_ME_NONE;
Direction bedPartnerDir = DIRECTION_NONE;
BedItemPart_t bedPart = BED_NONE_PART;
WeaponType_t weaponType = WEAPON_NONE;
Ammo_t ammoType = AMMO_NONE;
ShootType_t shootType = CONST_ANI_NONE;
Expand Down
2 changes: 2 additions & 0 deletions src/items/items_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ enum ItemParseAttributes_t {
ITEM_PARSE_LEVELDOOR,
ITEM_PARSE_MALETRANSFORMTO,
ITEM_PARSE_FEMALETRANSFORMTO,
ITEM_PARSE_BEDPART,
ITEM_PARSE_BEDPARTOF,
ITEM_PARSE_TRANSFORMTO,
ITEM_PARSE_DESTROYTO,
ITEM_PARSE_ELEMENTICE,
Expand Down
16 changes: 4 additions & 12 deletions src/items/tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ ReturnValue Tile::queryAdd(int32_t, const Thing& thing, uint32_t, uint32_t tileF
return RETURNVALUE_NOTENOUGHROOM;
}

if (!iiType.hasHeight || iiType.isBed()) {
if (!iiType.hasHeight) {
return RETURNVALUE_NOTENOUGHROOM;
}
}
Expand All @@ -716,7 +716,7 @@ ReturnValue Tile::queryAdd(int32_t, const Thing& thing, uint32_t, uint32_t tileF
return RETURNVALUE_NOTENOUGHROOM;
}

if (!iiType.hasHeight || iiType.pickupable || iiType.isBed()) {
if (!iiType.hasHeight || iiType.pickupable) {
return RETURNVALUE_NOTENOUGHROOM;
}
}
Expand Down Expand Up @@ -1467,16 +1467,8 @@ void Tile::internalAddThing(Thing* thing)

Door* door = item->getDoor();
House* house = houseTile->getHouse();
if (door) {
if (door->getDoorId() != 0) {
house->addDoor(door);
}
}
else {
BedItem* bed = item->getBed();
if (bed) {
house->addBed(bed);
}
if (door && door->getDoorId() != 0) {
house->addDoor(door);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/map/house/house.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ void House::addBed(BedItem* bed)
bed->setHouse(this);
}

void House::removeBed(BedItem* bed)
{
bed->setHouse(nullptr);
bedsList.remove(bed);
}

Door* House::getDoorByNumber(uint32_t doorId) const
{
for (Door* door : doorList) {
Expand Down Expand Up @@ -670,6 +676,7 @@ bool Houses::loadHousesXML(const std::string& filename)

house->setRent(pugi::cast<uint32_t>(houseNode.attribute("rent").value()));
house->setTownId(pugi::cast<uint32_t>(houseNode.attribute("townid").value()));
house->setMaxBeds(pugi::cast<uint32_t>(houseNode.attribute("beds").value()));

house->setOwner(0, false);
}
Expand Down
14 changes: 13 additions & 1 deletion src/map/house/house.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,20 @@ class House
}

void addBed(BedItem* bed);
void removeBed(BedItem* bed);
const HouseBedItemList& getBeds() const {
return bedsList;
}
uint32_t getBedCount() {
return static_cast<uint32_t>(std::ceil(bedsList.size() / 2.)); //each bed takes 2 sqms of space, ceil is just for bad maps
return static_cast<uint32_t>(std::floor(bedsList.size() / 2.)); //each bed takes 2 sqms of space, ceil is just for bad maps
}

void setMaxBeds(uint32_t count) {
maxBeds = count;
}

uint32_t getMaxBeds() const {
return maxBeds;
}

private:
Expand Down Expand Up @@ -226,6 +235,9 @@ class House
uint32_t rent = 0;
uint32_t townId = 0;

uint32_t maxBeds = 4;
uint32_t bedsCount = 0;

Position posEntry = {};

bool isLoaded = false;
Expand Down
10 changes: 10 additions & 0 deletions src/utils/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,16 @@ std::time_t getTimeNow() {
return std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
}

BedItemPart_t getBedPart(std::string_view string)
{
if (string == "pillow" || string == "1") {
return BED_PILLOW_PART;
} else if (string == "blanket" || string == "2") {
return BED_BLANKET_PART;
}
return BED_NONE_PART;
}

Direction getDirection(const std::string& string)
{
Direction direction = DIRECTION_NORTH;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ int32_t uniform_random(int32_t minNumber, int32_t maxNumber);
int32_t normal_random(int32_t minNumber, int32_t maxNumber);
bool boolean_random(double probability = 0.5);

BedItemPart_t getBedPart(std::string_view string);

Direction getDirection(const std::string& string);
Position getNextPosition(Direction direction, Position pos);
Direction getDirectionTo(const Position& from, const Position& to);
Expand Down
6 changes: 6 additions & 0 deletions src/utils/utils_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,4 +742,10 @@ const phmap::flat_hash_map<Blessings_t, std::string> BlessingNames = {
{HEARTH_OF_THE_MOUNTAIN, "Heart of the Mountain"},
};

enum BedItemPart_t : uint8_t {
BED_NONE_PART,
BED_PILLOW_PART,
BED_BLANKET_PART,
};

#endif // SRC_UTILS_UTILS_DEFINITIONS_HPP_