1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 22:49:55 +00:00
openmw-tes3mp/apps/openmw/mwgui/travelwindow.cpp

265 lines
10 KiB
C++
Raw Normal View History

2012-09-26 16:30:47 +00:00
#include "travelwindow.hpp"
2015-01-10 01:50:43 +00:00
#include <MyGUI_Button.h>
#include <MyGUI_ScrollView.h>
#include <MyGUI_Gui.h>
#include <components/settings/settings.hpp>
/*
Start of tes3mp addition
Include additional headers for multiplayer purposes
*/
#include "../mwmp/Main.hpp"
#include "../mwmp/Networking.hpp"
#include "../mwmp/Worldstate.hpp"
/*
End of tes3mp addition
*/
2012-09-26 16:30:47 +00:00
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwbase/windowmanager.hpp"
2012-10-16 17:34:29 +00:00
#include "../mwbase/mechanicsmanager.hpp"
2012-09-26 16:30:47 +00:00
#include "../mwmechanics/creaturestats.hpp"
2015-08-21 09:12:39 +00:00
#include "../mwmechanics/actorutil.hpp"
2013-05-11 16:38:27 +00:00
#include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp"
#include "../mwworld/actionteleport.hpp"
2014-02-23 19:11:05 +00:00
#include "../mwworld/esmstore.hpp"
#include "../mwworld/cellstore.hpp"
2012-09-26 16:30:47 +00:00
namespace MWGui
{
TravelWindow::TravelWindow() :
WindowBase("openmw_travel_window.layout")
2012-09-26 16:30:47 +00:00
, mCurrentY(0)
{
getWidget(mCancelButton, "CancelButton");
getWidget(mPlayerGold, "PlayerGold");
getWidget(mSelect, "Select");
2012-10-06 15:52:46 +00:00
getWidget(mDestinations, "Travel");
getWidget(mDestinationsView, "DestinationsView");
2012-09-26 16:30:47 +00:00
mCancelButton->eventMouseButtonClick += MyGUI::newDelegate(this, &TravelWindow::onCancelButtonClicked);
mDestinations->setCoord(450/2-mDestinations->getTextSize().width/2,
mDestinations->getTop(),
mDestinations->getTextSize().width,
mDestinations->getHeight());
mSelect->setCoord(8,
mSelect->getTop(),
mSelect->getTextSize().width,
mSelect->getHeight());
}
void TravelWindow::addDestination(const std::string& name, ESM::Position pos, bool interior)
2012-09-26 16:30:47 +00:00
{
int price;
const MWWorld::Store<ESM::GameSetting> &gmst =
MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>();
MWWorld::Ptr player = MWBase::Environment::get().getWorld ()->getPlayerPtr();
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
if (!mPtr.getCell()->isExterior())
2012-10-08 13:51:36 +00:00
{
2018-08-29 15:38:12 +00:00
price = gmst.find("fMagesGuildTravel")->mValue.getInteger();
2012-10-08 13:51:36 +00:00
}
else
{
ESM::Position PlayerPos = player.getRefData().getPosition();
float d = sqrt(pow(pos.pos[0] - PlayerPos.pos[0], 2) + pow(pos.pos[1] - PlayerPos.pos[1], 2) + pow(pos.pos[2] - PlayerPos.pos[2], 2));
float fTravelMult = gmst.find("fTravelMult")->mValue.getFloat();
if (fTravelMult != 0)
price = static_cast<int>(d / fTravelMult);
else
price = static_cast<int>(d);
2012-10-08 13:51:36 +00:00
}
price = std::max(1, price);
price = MWBase::Environment::get().getMechanicsManager()->getBarterOffer(mPtr, price, true);
// Add price for the travelling followers
std::set<MWWorld::Ptr> followers;
MWWorld::ActionTeleport::getFollowers(player, followers);
// Apply followers cost, unlike vanilla the first follower doesn't travel for free
price *= 1 + static_cast<int>(followers.size());
2018-06-18 09:43:39 +00:00
int lineHeight = MWBase::Environment::get().getWindowManager()->getFontHeight() + 2;
MyGUI::Button* toAdd = mDestinationsView->createWidget<MyGUI::Button>("SandTextButton", 0, mCurrentY, 200, lineHeight, MyGUI::Align::Default);
toAdd->setEnabled(price <= playerGold);
2018-06-18 09:43:39 +00:00
mCurrentY += lineHeight;
if(interior)
toAdd->setUserString("interior","y");
else
toAdd->setUserString("interior","n");
2019-01-07 13:47:39 +00:00
toAdd->setUserString("price", std::to_string(price));
toAdd->setCaptionWithReplacing("#{sCell=" + name + "} - " + MyGUI::utility::toString(price)+"#{sgp}");
2018-06-18 09:43:39 +00:00
toAdd->setSize(mDestinationsView->getWidth(),lineHeight);
2012-09-26 16:30:47 +00:00
toAdd->eventMouseWheel += MyGUI::newDelegate(this, &TravelWindow::onMouseWheel);
toAdd->setUserString("Destination", name);
2012-10-08 09:14:22 +00:00
toAdd->setUserData(pos);
2012-09-26 16:30:47 +00:00
toAdd->eventMouseButtonClick += MyGUI::newDelegate(this, &TravelWindow::onTravelButtonClick);
}
void TravelWindow::clearDestinations()
{
mDestinationsView->setViewOffset(MyGUI::IntPoint(0,0));
mCurrentY = 0;
while (mDestinationsView->getChildCount())
MyGUI::Gui::getInstance().destroyWidget(mDestinationsView->getChildAt(0));
}
void TravelWindow::setPtr(const MWWorld::Ptr& actor)
2012-09-26 16:30:47 +00:00
{
center();
2012-09-28 15:02:27 +00:00
mPtr = actor;
2012-09-26 16:30:47 +00:00
clearDestinations();
std::vector<ESM::Transport::Dest> transport;
if (mPtr.getClass().isNpc())
transport = mPtr.get<ESM::NPC>()->mBase->getTransport();
else if (mPtr.getTypeName() == typeid(ESM::Creature).name())
transport = mPtr.get<ESM::Creature>()->mBase->getTransport();
for(unsigned int i = 0;i<transport.size();i++)
2012-09-26 16:30:47 +00:00
{
std::string cellname = transport[i].mCellName;
2012-10-08 09:14:22 +00:00
bool interior = true;
int x,y;
MWBase::Environment::get().getWorld()->positionToIndex(transport[i].mPos.pos[0],
transport[i].mPos.pos[1],x,y);
if (cellname == "")
{
MWWorld::CellStore* cell = MWBase::Environment::get().getWorld()->getExterior(x,y);
cellname = MWBase::Environment::get().getWorld()->getCellName(cell);
interior = false;
}
addDestination(cellname,transport[i].mPos,interior);
2012-09-26 16:30:47 +00:00
}
updateLabels();
// Canvas size must be expressed with VScroll disabled, otherwise MyGUI would expand the scroll area when the scrollbar is hidden
mDestinationsView->setVisibleVScroll(false);
2012-09-26 16:30:47 +00:00
mDestinationsView->setCanvasSize (MyGUI::IntSize(mDestinationsView->getWidth(), std::max(mDestinationsView->getHeight(), mCurrentY)));
mDestinationsView->setVisibleVScroll(true);
2012-09-26 16:30:47 +00:00
}
void TravelWindow::onTravelButtonClick(MyGUI::Widget* _sender)
{
2012-10-08 09:14:22 +00:00
std::istringstream iss(_sender->getUserString("price"));
int price;
iss >> price;
2012-09-26 16:30:47 +00:00
2015-08-21 09:12:39 +00:00
MWWorld::Ptr player = MWMechanics::getPlayer();
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
if (playerGold<price)
return;
// Set "traveling" flag, so GetPCTraveling can detect teleportation.
// We will reset this flag during next world update.
MWBase::Environment::get().getWorld()->setPlayerTraveling(true);
if (!mPtr.getCell()->isExterior())
// Interior cell -> mages guild transport
MWBase::Environment::get().getWindowManager()->playSound("mysticism cast");
player.getClass().getContainerStore(player).remove(MWWorld::ContainerStore::sGoldId, price, player);
// add gold to NPC trading gold pool
MWMechanics::CreatureStats& npcStats = mPtr.getClass().getCreatureStats(mPtr);
/*
Start of tes3mp change (major)
Don't unilaterally change the merchant's gold pool on our client and instead let the server do it
*/
//npcStats.setGoldPool(npcStats.getGoldPool() + price);
mwmp::ObjectList* objectList = mwmp::Main::get().getNetworking()->getObjectList();
objectList->reset();
objectList->packetOrigin = mwmp::CLIENT_GAMEPLAY;
objectList->addObjectMiscellaneous(mPtr, npcStats.getGoldPool() + price, npcStats.getLastRestockTime().getHour(),
npcStats.getLastRestockTime().getDay());
objectList->sendObjectMiscellaneous();
/*
End of tes3mp change (major)
*/
MWBase::Environment::get().getWindowManager()->fadeScreenOut(1);
ESM::Position pos = *_sender->getUserData<ESM::Position>();
std::string cellname = _sender->getUserString("Destination");
bool interior = _sender->getUserString("interior") == "y";
if (mPtr.getCell()->isExterior())
2012-09-26 16:30:47 +00:00
{
ESM::Position playerPos = player.getRefData().getPosition();
2015-06-03 17:41:19 +00:00
float d = (osg::Vec3f(pos.pos[0], pos.pos[1], 0) - osg::Vec3f(playerPos.pos[0], playerPos.pos[1], 0)).length();
2018-08-29 15:38:12 +00:00
int hours = static_cast<int>(d /MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("fTravelTimeMult")->mValue.getFloat());
MWBase::Environment::get().getMechanicsManager()->rest(hours, true);
/*
Start of tes3mp change (major)
Multiplayer requires that time not get advanced here
*/
//MWBase::Environment::get().getWorld()->advanceTime(hours);
/*
End of tes3mp change (major)
*/
2012-10-08 09:14:22 +00:00
}
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Travel);
MWBase::Environment::get().getWindowManager()->exitCurrentGuiMode();
MWBase::Environment::get().getWindowManager()->fadeScreenOut(1);
// Teleports any followers, too.
MWWorld::ActionTeleport action(interior ? cellname : "", pos, true);
action.execute(player);
MWBase::Environment::get().getWindowManager()->fadeScreenOut(0);
MWBase::Environment::get().getWindowManager()->fadeScreenIn(1);
2012-09-26 16:30:47 +00:00
}
void TravelWindow::onCancelButtonClicked(MyGUI::Widget* _sender)
{
2017-09-23 10:18:39 +00:00
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Travel);
2012-09-26 16:30:47 +00:00
}
void TravelWindow::updateLabels()
{
MWWorld::Ptr player = MWBase::Environment::get().getWorld ()->getPlayerPtr();
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
mPlayerGold->setCaptionWithReplacing("#{sGold}: " + MyGUI::utility::toString(playerGold));
2012-09-26 16:30:47 +00:00
mPlayerGold->setCoord(8,
mPlayerGold->getTop(),
mPlayerGold->getTextSize().width,
mPlayerGold->getHeight());
}
void TravelWindow::onReferenceUnavailable()
{
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Travel);
MWBase::Environment::get().getWindowManager()->exitCurrentGuiMode();
2012-09-26 16:30:47 +00:00
}
void TravelWindow::onMouseWheel(MyGUI::Widget* _sender, int _rel)
{
if (mDestinationsView->getViewOffset().top + _rel*0.3f > 0)
2012-09-26 16:30:47 +00:00
mDestinationsView->setViewOffset(MyGUI::IntPoint(0, 0));
else
mDestinationsView->setViewOffset(MyGUI::IntPoint(0, static_cast<int>(mDestinationsView->getViewOffset().top + _rel*0.3f)));
2012-09-26 16:30:47 +00:00
}
}