// Copyright (c) 2001-2004 Rob Kaper , // 2001 Erik Bourget // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // version 2 as published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; see the file COPYING. If not, write to // the Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. #include #include #include #include #include #include "card.h" #include "display.h" #include "estate.h" #include "io.h" #include "game.h" #include "player.h" #include "trade.h" // move trade commands to game/engine Player::Player(Socket *socket, int id) : GameObject(id, GameObject::GPlayer) { m_socket = socket; m_game = 0; m_display = new Display(); setProperty("cookie", std::string(itoa(id)) + "/" + itoa(rand()), this); reset(); } Player::~Player() { while (!m_cards.empty()) { delete *m_cards.begin(); m_cards.erase(m_cards.begin()); } delete m_display; } void Player::reset(bool removeProperties) { while (!m_cards.empty()) { delete *m_cards.begin(); m_cards.erase(m_cards.begin()); } m_estate = m_destination = m_tokenLocation = 0; if (removeProperties) { removeProperty("money"); removeProperty("doublecount"); removeProperty("jailcount"); removeProperty("jailed"); removeProperty("bankrupt"); removeProperty("hasturn"); removeProperty("spectator"); removeProperty("can_roll"); removeProperty("canrollagain"); removeProperty("can_buyestate"); removeProperty("canauction"); removeProperty("location"); removeProperty("directmove"); removeProperty("master"); // FIXME: DEPRECATED 1.0 } m_requestedUpdate = false; m_display->setEstate(0); m_display->resetButtons(); } void Player::ioWrite(const char *fmt, ...) { int n, size = 256; char *buf = new char[size]; static std::string ioStr; va_list arg; buf[0] = 0; while (1) { va_start(arg, fmt); n = vsnprintf(buf, size, fmt, arg); va_end(arg); if (n > -1 && n < size) { ioStr = buf; delete[] buf; ioWrite(ioStr); return; } if (n > -1) size = n+1; else size *= 2; delete[] buf; buf = new char[size]; } } void Player::ioWrite(std::string data) { if (m_socket) m_socket->ioWrite(data); } void Player::ioInfo(const char *data, ...) { va_list arg; char buf[2048]; va_start(arg, data); vsnprintf(buf, sizeof(buf)-1, data, arg); va_end(arg); buf[sizeof(buf)-1] = 0; ioWrite("\n", buf); } void Player::ioInfo(const std::string data) { ioWrite("\n"); } void Player::ioError(const char *data, ...) { va_list arg; char buf[2048]; va_start(arg, data); vsnprintf(buf, sizeof(buf)-1, data, arg); va_end(arg); buf[sizeof(buf)-1] = 0; ioWrite("\n", buf); } void Player::ioError(const std::string data) { ioWrite("\n"); } void Player::ioNoSuchCmd(const std::string data) { if (data.size()) ioError("Command is unavailable at current game status: " + data); else ioError("No such command."); } void Player::setDisplay(Estate *estate) { m_display->setEstate(estate); m_display->resetButtons(); } void Player::setDisplay(Estate *estate, const char *data, ...) { va_list arg; char buf[2048]; va_start(arg, data); vsnprintf(buf, sizeof(buf)-1, data, arg); va_end(arg); buf[sizeof(buf)-1] = 0; setDisplay(estate); m_display->setText(std::string(buf)); m_display->resetButtons(); } void Player::setDisplay(Estate *estate, const std::string data) { setDisplay(estate); m_display->setText(data); } void Player::setDisplayClearText(bool clearText) { m_display->setClearText(clearText); } void Player::setDisplayClearButtons(bool clearButtons) { m_display->setClearButtons(clearButtons); } void Player::addDisplayButton(const std::string command, const std::string caption, const bool enabled) { m_display->addButton(command, caption, enabled); } void Player::sendDisplayMsg() { Estate *estate = m_display->estate(); if (estate) { std::vector buttons = m_display->buttons(); if (buttons.size() > 0) { ioWrite("", estate->id(), m_display->text().c_str(), m_display->clearText(), m_display->clearButtons()); DisplayButton *button = 0; for (std::vector::iterator it = buttons.begin() ; it != buttons.end() && (button = *it) ; ++it) ioWrite("