Commit 614f1063 authored by Max-Wilhelm Bruker's avatar Max-Wilhelm Bruker
Browse files

arrows can target players; card attachment works

parent 61b82bd6
......@@ -4,6 +4,7 @@
#include "client.h"
#include "protocol_items.h"
#include "settingscache.h"
#include "arrowitem.h"
TableZone::TableZone(Player *_p, QGraphicsItem *parent)
: CardZone(_p, "table", true, false, true, parent), active(false)
......@@ -91,17 +92,61 @@ void TableZone::handleDropEventByGrid(int cardId, CardZone *startZone, const QPo
void TableZone::reorganizeCards()
{
QList<ArrowItem *> arrowsToUpdate;
// Calculate table grid distortion so that the mapping functions work properly
gridPointWidth.clear();
for (int i = 0; i < cards.size(); ++i) {
QPoint gridPoint = cards[i]->getGridPos();
if (gridPoint.x() == -1)
continue;
gridPointWidth.insert(gridPoint.x() + gridPoint.y() * 1000, CARD_WIDTH * (1 + cards[i]->getAttachedCards().size() / 3.0));
}
for (int i = 0; i < cards.size(); ++i) {
QPointF mapPoint = mapFromGrid(cards[i]->getGridPos());
QPoint gridPoint = cards[i]->getGridPos();
if (gridPoint.x() == -1)
continue;
QPointF mapPoint = mapFromGrid(gridPoint);
qreal x = mapPoint.x();
qreal y = mapPoint.y();
if (player->getMirrored())
y = height - CARD_HEIGHT - y;
cards[i]->setPos(x, y);
cards[i]->setZValue((y + CARD_HEIGHT) * 10000000 + x + 1000);
int numberAttachedCards = cards[i]->getAttachedCards().size();
qreal actualX = x + numberAttachedCards * CARD_WIDTH / 3.0;
qreal actualY = y;
if (numberAttachedCards)
actualY += 5;
cards[i]->setPos(actualX, actualY);
cards[i]->setZValue((actualY + CARD_HEIGHT) * 10000000 + (actualX + 1) * 10000);
QListIterator<CardItem *> attachedCardIterator(cards[i]->getAttachedCards());
int j = 0;
while (attachedCardIterator.hasNext()) {
++j;
CardItem *attachedCard = attachedCardIterator.next();
qreal childX = actualX - j * CARD_WIDTH / 3.0;
qreal childY = y - 5;
attachedCard->setPos(childX, childY);
attachedCard->setZValue((childY + CARD_HEIGHT) * 10000000 + (childX + 1) * 10000);
arrowsToUpdate.append(attachedCard->getArrowsFrom());
arrowsToUpdate.append(attachedCard->getArrowsTo());
}
arrowsToUpdate.append(cards[i]->getArrowsFrom());
arrowsToUpdate.append(cards[i]->getArrowsTo());
}
QSetIterator<ArrowItem *> arrowIterator(QSet<ArrowItem *>::fromList(arrowsToUpdate));
while (arrowIterator.hasNext())
arrowIterator.next()->updatePath();
resizeToContents();
update();
}
......@@ -164,11 +209,16 @@ QPointF TableZone::mapFromGrid(const QPoint &gridPoint) const
marginX + (CARD_WIDTH * gridPoint.x() + CARD_WIDTH * (gridPoint.x() / 3)) / 2,
boxLineWidth + (CARD_HEIGHT + paddingY) * gridPoint.y() + (gridPoint.x() % 3 * CARD_HEIGHT) / 3
);
else
else {
qreal x = marginX + 0.5 * CARD_WIDTH * gridPoint.x();
for (int i = 0; i < gridPoint.x(); ++i)
x += gridPointWidth.value(gridPoint.y() * 1000 + i, CARD_WIDTH);
return QPointF(
marginX + 3 * CARD_WIDTH * gridPoint.x() / 2,
x,
boxLineWidth + (CARD_HEIGHT + paddingY) * gridPoint.y()
);
}
}
QPoint TableZone::mapToGrid(const QPointF &mapPoint) const
......@@ -184,18 +234,22 @@ QPoint TableZone::mapToGrid(const QPointF &mapPoint) const
else if (y > height - CARD_HEIGHT)
y = height - CARD_HEIGHT;
QPoint result = QPoint(
(int) (x / (1.5 * CARD_WIDTH)),
(int) (y / (CARD_HEIGHT + paddingY))
);
int resultY = (int) (y / (CARD_HEIGHT + paddingY));
if ((result.y() == 3) && (settingsCache->getEconomicGrid()))
if ((resultY == 3) && (settingsCache->getEconomicGrid()))
return QPoint(
(int) (x * 2 / CARD_WIDTH - floor(x / (2 * CARD_WIDTH))),
3
);
else
return result;
else {
int resultX = -1;
qreal tempX = 0;
do {
++resultX;
tempX += gridPointWidth.value(resultY * 1000 + resultX, CARD_WIDTH) + 0.5 * CARD_WIDTH;
} while (tempX < x + 1);
return QPoint(resultX, resultY);
}
}
QPointF TableZone::closestGridPoint(const QPointF &point)
......
......@@ -13,6 +13,7 @@ private:
static const int marginX = 20;
static const int minWidth = 20 * CARD_WIDTH / 2;
QMap<int, int> gridPointWidth;
int width, height;
int currentMinimumWidth;
QPixmap bgPixmap;
......
This diff is collapsed.
This diff is collapsed.
......@@ -39,7 +39,7 @@ ServerInfo_CardCounter::ServerInfo_CardCounter(int _id, int _value)
insertItem(new SerializableItem_Int("value", _value));
}
ServerInfo_Card::ServerInfo_Card(int _id, const QString &_name, int _x, int _y, bool _tapped, bool _attacking, const QString &_color, const QString &_pt, const QString &_annotation, bool _destroyOnZoneChange, const QList<ServerInfo_CardCounter *> &_counters)
ServerInfo_Card::ServerInfo_Card(int _id, const QString &_name, int _x, int _y, bool _tapped, bool _attacking, const QString &_color, const QString &_pt, const QString &_annotation, bool _destroyOnZoneChange, const QList<ServerInfo_CardCounter *> &_counters, int _attachPlayerId, const QString &_attachZone, int _attachCardId)
: SerializableItem_Map("card")
{
insertItem(new SerializableItem_Int("id", _id));
......@@ -52,6 +52,9 @@ ServerInfo_Card::ServerInfo_Card(int _id, const QString &_name, int _x, int _y,
insertItem(new SerializableItem_String("pt", _pt));
insertItem(new SerializableItem_String("annotation", _annotation));
insertItem(new SerializableItem_Bool("destroy_on_zone_change", _destroyOnZoneChange));
insertItem(new SerializableItem_Int("attach_player_id", _attachPlayerId));
insertItem(new SerializableItem_String("attach_zone", _attachZone));
insertItem(new SerializableItem_Int("attach_card_id", _attachCardId));
for (int i = 0; i < _counters.size(); ++i)
itemList.append(_counters[i]);
......
......@@ -62,7 +62,7 @@ public:
class ServerInfo_Card : public SerializableItem_Map {
public:
ServerInfo_Card(int _id = -1, const QString &_name = QString(), int _x = -1, int _y = -1, bool _tapped = false, bool _attacking = false, const QString &_color = QString(), const QString &_pt = QString(), const QString &_annotation = QString(), bool _destroyOnZoneChange = false, const QList<ServerInfo_CardCounter *> &_counterList = QList<ServerInfo_CardCounter *>());
ServerInfo_Card(int _id = -1, const QString &_name = QString(), int _x = -1, int _y = -1, bool _tapped = false, bool _attacking = false, const QString &_color = QString(), const QString &_pt = QString(), const QString &_annotation = QString(), bool _destroyOnZoneChange = false, const QList<ServerInfo_CardCounter *> &_counterList = QList<ServerInfo_CardCounter *>(), int attachPlayerId = -1, const QString &_attachZone = QString(), int attachCardId = -1);
static SerializableItem *newItem() { return new ServerInfo_Card; }
int getId() const { return static_cast<SerializableItem_Int *>(itemMap.value("id"))->getData(); }
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
......@@ -75,6 +75,9 @@ public:
QString getAnnotation() const { return static_cast<SerializableItem_String *>(itemMap.value("annotation"))->getData(); }
bool getDestroyOnZoneChange() const { return static_cast<SerializableItem_Bool *>(itemMap.value("destroy_on_zone_change"))->getData(); }
QList<ServerInfo_CardCounter *> getCounters() const { return typecastItemList<ServerInfo_CardCounter *>(); }
int getAttachPlayerId() const { return static_cast<SerializableItem_Int *>(itemMap.value("attach_player_id"))->getData(); }
QString getAttachZone() const { return static_cast<SerializableItem_String *>(itemMap.value("attach_zone"))->getData(); }
int getAttachCardId() const { return static_cast<SerializableItem_Int *>(itemMap.value("attach_card_id"))->getData(); }
};
class ServerInfo_Zone : public SerializableItem_Map {
......
......@@ -27,6 +27,12 @@ Server_Card::Server_Card(QString _name, int _id, int _coord_x, int _coord_y)
Server_Card::~Server_Card()
{
// setParentCard(0) leads to the item being removed from our list, so we can't iterate properly
while (!attachedCards.isEmpty())
attachedCards.first()->setParentCard(0);
if (parentCard)
parentCard->removeAttachedCard(this);
}
void Server_Card::resetState()
......@@ -74,3 +80,12 @@ void Server_Card::setCounter(int id, int value)
else
counters.remove(id);
}
void Server_Card::setParentCard(Server_Card *_parentCard)
{
if (parentCard)
parentCard->removeAttachedCard(this);
parentCard = _parentCard;
if (parentCard)
parentCard->addAttachedCard(this);
}
......@@ -44,6 +44,7 @@ private:
bool doesntUntap;
Server_Card *parentCard;
QList<Server_Card *> attachedCards;
public:
Server_Card(QString _name, int _id, int _coord_x, int _coord_y);
~Server_Card();
......@@ -66,6 +67,7 @@ public:
bool getDoesntUntap() const { return doesntUntap; }
bool getDestroyOnZoneChange() const { return destroyOnZoneChange; }
Server_Card *getParentCard() const { return parentCard; }
const QList<Server_Card *> &getAttachedCards() const { return attachedCards; }
void setId(int _id) { id = _id; }
void setCoords(int x, int y) { coord_x = x; coord_y = y; }
......@@ -79,7 +81,9 @@ public:
void setAnnotation(const QString &_annotation) { annotation = _annotation; }
void setDestroyOnZoneChange(bool _destroy) { destroyOnZoneChange = _destroy; }
void setDoesntUntap(bool _doesntUntap) { doesntUntap = _doesntUntap; }
void setParentCard(Server_Card *_parentCard) { parentCard = _parentCard; }
void setParentCard(Server_Card *_parentCard);
void addAttachedCard(Server_Card *card) { attachedCards.append(card); }
void removeAttachedCard(Server_Card *card) { attachedCards.removeAt(attachedCards.indexOf(card)); }
void resetState();
bool setAttribute(const QString &aname, const QString &avalue, bool allCards);
......
......@@ -318,7 +318,15 @@ QList<ServerInfo_Player *> Server_Game::getGameState(Server_Player *playerWhosAs
cardCounterList.append(new ServerInfo_CardCounter(cardCounterIterator.key(), cardCounterIterator.value()));
}
cardList.append(new ServerInfo_Card(card->getId(), displayedName, card->getX(), card->getY(), card->getTapped(), card->getAttacking(), card->getColor(), card->getPT(), card->getAnnotation(), card->getDestroyOnZoneChange(), cardCounterList));
int attachPlayerId = -1;
QString attachZone;
int attachCardId = -1;
if (card->getParentCard()) {
attachPlayerId = card->getParentCard()->getZone()->getPlayer()->getPlayerId();
attachZone = card->getParentCard()->getZone()->getName();
attachCardId = card->getParentCard()->getId();
}
cardList.append(new ServerInfo_Card(card->getId(), displayedName, card->getX(), card->getY(), card->getTapped(), card->getAttacking(), card->getColor(), card->getPT(), card->getAnnotation(), card->getDestroyOnZoneChange(), cardCounterList, attachPlayerId, attachZone, attachCardId));
}
}
zoneList.append(new ServerInfo_Zone(zone->getName(), zone->getType(), zone->hasCoords(), zone->cards.size(), cardList));
......
......@@ -517,6 +517,12 @@ ResponseCode Server_ProtocolHandler::moveCard(Server_Game *game, Server_Player *
if (!card)
return RespNameNotFound;
if (startzone != targetzone) {
const QList<Server_Card *> &attachedCards = card->getAttachedCards();
for (int i = 0; i < attachedCards.size(); ++i)
unattachCard(game, attachedCards[i]->getZone()->getPlayer(), cont, attachedCards[i]);
}
if (card->getDestroyOnZoneChange() && (startzone != targetzone)) {
cont->enqueueGameEventPrivate(new Event_DestroyCard(player->getPlayerId(), startzone->getName(), card->getId()), game->getGameId());
cont->enqueueGameEventPublic(new Event_DestroyCard(player->getPlayerId(), startzone->getName(), card->getId()), game->getGameId());
......@@ -609,6 +615,17 @@ ResponseCode Server_ProtocolHandler::cmdMoveCard(Command_MoveCard *cmd, CommandC
return moveCard(game, player, cont, cmd->getStartZone(), cmd->getCardId(), cmd->getTargetZone(), cmd->getX(), cmd->getY(), cmd->getFaceDown(), cmd->getTapped());
}
void Server_ProtocolHandler::unattachCard(Server_Game *game, Server_Player *player, CommandContainer *cont, Server_Card *card)
{
Server_CardZone *zone = card->getZone();
card->setParentCard(0);
cont->enqueueGameEventPrivate(new Event_AttachCard(player->getPlayerId(), zone->getName(), card->getId(), -1, QString(), -1), game->getGameId());
cont->enqueueGameEventPublic(new Event_AttachCard(player->getPlayerId(), zone->getName(), card->getId(), -1, QString(), -1), game->getGameId());
moveCard(game, player, cont, zone->getName(), card->getId(), zone->getName(), -1, card->getY(), card->getFaceDown(), card->getTapped());
}
ResponseCode Server_ProtocolHandler::cmdAttachCard(Command_AttachCard *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player)
{
if (player->getSpectator())
......@@ -625,21 +642,49 @@ ResponseCode Server_ProtocolHandler::cmdAttachCard(Command_AttachCard *cmd, Comm
if (!card)
return RespNameNotFound;
Server_Player *targetPlayer = game->getPlayer(cmd->getTargetPlayerId());
if (!targetPlayer)
return RespNameNotFound;
int playerId = cmd->getTargetPlayerId();
Server_Player *targetPlayer = 0;
Server_CardZone *targetzone = 0;
Server_Card *targetCard = 0;
Server_CardZone *targetzone = targetPlayer->getZones().value(cmd->getTargetZone());
if (!targetzone)
return RespNameNotFound;
if (playerId != -1) {
targetPlayer = game->getPlayer(cmd->getTargetPlayerId());
if (!targetPlayer)
return RespNameNotFound;
} else if (!card->getParentCard())
return RespContextError;
if (targetPlayer)
targetzone = targetPlayer->getZones().value(cmd->getTargetZone());
if (targetzone)
targetCard = targetzone->getCard(cmd->getTargetCardId(), false);
// Get all arrows pointing to or originating from the card being attached and delete them.
QMapIterator<int, Server_Player *> playerIterator(game->getPlayers());
while (playerIterator.hasNext()) {
Server_Player *p = playerIterator.next().value();
QList<Server_Arrow *> arrows = p->getArrows().values();
QList<Server_Arrow *> toDelete;
for (int i = 0; i < arrows.size(); ++i) {
Server_Arrow *a = arrows[i];
Server_Card *tCard = qobject_cast<Server_Card *>(a->getTargetItem());
if ((tCard == card) || (a->getStartCard() == card))
toDelete.append(a);
}
for (int i = 0; i < toDelete.size(); ++i) {
cont->enqueueGameEventPrivate(new Event_DeleteArrow(p->getPlayerId(), toDelete[i]->getId()), game->getGameId());
cont->enqueueGameEventPublic(new Event_DeleteArrow(p->getPlayerId(), toDelete[i]->getId()), game->getGameId());
p->deleteArrow(toDelete[i]->getId());
}
}
Server_Card *targetCard = targetzone->getCard(cmd->getTargetCardId(), false);
if (!targetCard)
return RespNameNotFound;
if (targetCard) {
card->setParentCard(targetCard);
card->setCoords(-1, card->getY());
cont->enqueueGameEventPrivate(new Event_AttachCard(player->getPlayerId(), startzone->getName(), card->getId(), targetPlayer->getPlayerId(), targetzone->getName(), targetCard->getId()), game->getGameId());
cont->enqueueGameEventPublic(new Event_AttachCard(player->getPlayerId(), startzone->getName(), card->getId(), targetPlayer->getPlayerId(), targetzone->getName(), targetCard->getId()), game->getGameId());
} else
unattachCard(game, player, cont, card);
card->setParentCard(targetCard);
cont->enqueueGameEventPrivate(new Event_AttachCard(player->getPlayerId(), startzone->getName(), card->getId(), targetPlayer->getPlayerId(), targetzone->getName(), targetCard->getId()), game->getGameId());
cont->enqueueGameEventPublic(new Event_AttachCard(player->getPlayerId(), startzone->getName(), card->getId(), targetPlayer->getPlayerId(), targetzone->getName(), targetCard->getId()), game->getGameId());
return RespOk;
}
......@@ -956,7 +1001,17 @@ ResponseCode Server_ProtocolHandler::cmdDumpZone(Command_DumpZone *cmd, CommandC
cardCounterIterator.next();
cardCounterList.append(new ServerInfo_CardCounter(cardCounterIterator.key(), cardCounterIterator.value()));
}
respCardList.append(new ServerInfo_Card(card->getId(), displayedName, card->getX(), card->getY(), card->getTapped(), card->getAttacking(), card->getColor(), card->getPT(), card->getAnnotation(), card->getDestroyOnZoneChange(), cardCounterList));
int attachPlayerId = -1;
QString attachZone;
int attachCardId = -1;
if (card->getParentCard()) {
attachPlayerId = card->getParentCard()->getZone()->getPlayer()->getPlayerId();
attachZone = card->getParentCard()->getZone()->getName();
attachCardId = card->getParentCard()->getId();
}
respCardList.append(new ServerInfo_Card(card->getId(), displayedName, card->getX(), card->getY(), card->getTapped(), card->getAttacking(), card->getColor(), card->getPT(), card->getAnnotation(), card->getDestroyOnZoneChange(), cardCounterList, attachPlayerId, attachZone, attachCardId));
}
}
if (zone->getType() == HiddenZone) {
......
......@@ -8,6 +8,7 @@
#include "protocol_items.h"
class Server_Player;
class Server_Card;
class QTimer;
class Server_ProtocolHandler : public QObject {
......@@ -61,6 +62,7 @@ private:
ResponseCode cmdDrawCards(Command_DrawCards *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
ResponseCode moveCard(Server_Game *game, Server_Player *player, CommandContainer *cont, const QString &_startZone, int _cardId, const QString &_targetZone, int _x, int _y, bool _faceDown, bool _tapped);
ResponseCode cmdMoveCard(Command_MoveCard *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
void unattachCard(Server_Game *game, Server_Player *player, CommandContainer *cont, Server_Card *card);
ResponseCode cmdAttachCard(Command_AttachCard *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
ResponseCode cmdCreateToken(Command_CreateToken *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
ResponseCode cmdCreateArrow(Command_CreateArrow *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);
......
......@@ -130,6 +130,10 @@
<name>M10</name>
<longname>Magic 2010</longname>
</set>
<set import="1">
<name>M11</name>
<longname>Magic 2011</longname>
</set>
<set import="1">
<name>MM</name>
<longname>Mercadian Masques</longname>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment