Debian Codegraph @

src/player.cpp

raw
1 // Copyright © 2001-2004 Rob Kaper <[email protected]>,
2 // 2001 Erik Bourget <[email protected]>
3 // 2010-2015 Sylvain Rochet <[email protected]>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // version 2 as published by the Free Software Foundation.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; see the file COPYING. If not, see
16 // <http://www.gnu.org/licenses/>.
17
18 #include "config.h"
19
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <string>
26
27 #include "socket.h"
28 #include "card.h"
29 #include "display.h"
30 #include "estate.h"
31 #include "io.h"
32 #include "game.h"
33 #include "player.h"
34 #include "trade.h" // move trade commands to game/engine
35
36 Player::Player(Socket *socket) : GameObject(-1, GameObject::GPlayer)
37 {
38 m_socket = socket;
39 m_game = 0;
40 m_estate = 0;
41 m_destination = 0;
42 m_tokenLocation = 0;
43 m_identified = false;
44 }
45
46 Player::~Player()
47 {
48 while (!m_cards.empty()) { delete *m_cards.begin(); m_cards.erase(m_cards.begin()); }
49 while (!m_display.empty()) { delete *m_display.begin(); m_display.erase(m_display.begin()); }
50 }
51
52 void Player::reset(bool removeProperties)
53 {
54 while (!m_cards.empty()) { delete *m_cards.begin(); m_cards.erase(m_cards.begin()); }
55 while (!m_display.empty()) { delete *m_display.begin(); m_display.erase(m_display.begin()); }
56
57 m_estate = m_destination = m_tokenLocation = 0;
58
59 if (removeProperties) {
60 removeProperty("money");
61 removeProperty("doublecount");
62 removeProperty("jailcount");
63 removeProperty("jailed");
64 removeProperty("bankrupt");
65 removeProperty("hasturn");
66 removeProperty("spectator");
67 removeProperty("can_roll");
68 removeProperty("canrollagain");
69 removeProperty("can_buyestate");
70 removeProperty("canauction");
71 removeProperty("location");
72 removeProperty("directmove");
73 removeProperty("turnorder");
74 }
75 }
76
77 void Player::identify(int id) {
78 m_id = id;
79 setProperty("cookie", std::string(itoa(m_id)) + "/" + itoa(rand()), this);
80 m_identified = true;
81 }
82
83 void Player::ioWrite(const char *fmt, ...)
84 {
85 int n, size = 256;
86 char *buf = new char[size];
87 static std::string ioStr;
88 va_list arg;
89
90 buf[0] = 0;
91
92 while (1)
93 {
94 va_start(arg, fmt);
95 n = vsnprintf(buf, size, fmt, arg);
96 va_end(arg);
97
98 if (n > -1 && n < size)
99 {
100 ioStr = buf;
101 delete[] buf;
102 ioWrite(ioStr);
103 return;
104 }
105
106 if (n > -1)
107 size = n+1;
108 else
109 size *= 2;
110
111 delete[] buf;
112 buf = new char[size];
113 }
114 }
115
116 void Player::ioWrite(std::string data)
117 {
118 if (m_socket)
119 m_socket->ioWrite(data);
120 }
121
122 void Player::ioInfo(const char *data, ...)
123 {
124 va_list arg;
125 char buf[2048];
126
127 va_start(arg, data);
128 vsnprintf(buf, sizeof(buf)-1, data, arg);
129 va_end(arg);
130 buf[sizeof(buf)-1] = 0;
131
132 ioWrite("<monopd><msg type=\"info\" value=\"%s\"/></monopd>\n", buf);
133 }
134
135 void Player::ioInfo(const std::string data)
136 {
137 ioWrite("<monopd><msg type=\"info\" value=\"" + data + "\"/></monopd>\n");
138 }
139
140 void Player::ioError(const char *data, ...)
141 {
142 va_list arg;
143 char buf[2048];
144
145 va_start(arg, data);
146 vsnprintf(buf, sizeof(buf)-1, data, arg);
147 va_end(arg);
148 buf[sizeof(buf)-1] = 0;
149
150 ioWrite("<monopd><msg type=\"error\" value=\"%s\"/></monopd>\n", buf);
151 }
152
153 void Player::ioError(const std::string data)
154 {
155 ioWrite("<monopd><msg type=\"error\" value=\"" + data + "\"/></monopd>\n");
156 }
157
158 void Player::ioNoSuchCmd(const std::string data)
159 {
160 if (data.size())
161 ioError("Command is unavailable at current game status: " + data);
162 else
163 ioError("No such command.");
164 }
165
166 void Player::sendDisplay(Display *display)
167 {
168 std::string text, estate;
169 if (display->text().size()) {
170 text = "text=\"" + display->text() + "\" ";
171 }
172
173 if (display->clearEstate()) {
174 estate = "estateid=\"-1\" ";
175 } else if (display->estate()) {
176 estate = "estateid=\"" + itoa(display->estate()->id()) + "\" ";
177 }
178
179 ioWrite("<monopd><display %s%scleartext=\"%d\" clearbuttons=\"%d\"", estate.c_str(), text.c_str(), display->clearText(), display->clearButtons());
180
181 std::vector<DisplayButton *> buttons = display->buttons();
182 if (buttons.size() > 0) {
183 ioWrite(">");
184 DisplayButton *button;
185 for (std::vector<DisplayButton *>::iterator it = buttons.begin() ; it != buttons.end() && (button = *it) ; ++it)
186 ioWrite("<button command=\"%s\" caption=\"%s\" enabled=\"%d\"/>", button->command().c_str(), button->caption().c_str(), button->enabled());
187 ioWrite("</display></monopd>\n");
188 }
189 else
190 ioWrite("/></monopd>\n");
191 }
192
193 void Player::sendDisplayMsg(Display *display)
194 {
195 sendDisplay(display);
196 if (display->clearText()) {
197 while (!m_display.empty()) { delete *m_display.begin(); m_display.erase(m_display.begin()); }
198 }
199 m_display.push_back(new Display(display));
200 }
201
202 void Player::sendDisplayHistory()
203 {
204 for(std::vector<Display *>::iterator it = m_display.begin(); it != m_display.end(); ++it) {
205 sendDisplay(*it);
206 }
207 }
208
209 void Player::sendClientMsg()
210 {
211 ioWrite(std::string("<monopd><client playerid=\"" + itoa(m_id) + "\" cookie=\"" + getStringProperty("cookie").c_str() + "\"/></monopd>\n"));
212 }
213
214 void Player::sendCardList(Player *pOut)
215 {
216 Card *card = 0;
217 for(std::vector<Card *>::iterator it = m_cards.begin() ; it != m_cards.end() && (card = *it) ; ++it)
218 pOut->ioWrite("<cardupdate cardid=\"%d\" title=\"%s\" owner=\"%d\"/>", card->id(), card->name().c_str(), m_id);
219 }
220
221 void Player::rollDice()
222 {
223 if (getBoolProperty("jailed"))
224 {
225 ioError("You cannot roll while jailed.");
226 return;
227 }
228 if (getBoolProperty("can_buyestate"))
229 {
230 ioError("You must buy or auction the current estate.");
231 return;
232 }
233
234 m_game->rollDice();
235
236 Display display;
237 display.setText("%s rolls %d and %d.", getStringProperty("name").c_str(), m_game->dice[0], m_game->dice[1]);
238 display.resetText();
239 display.resetEstate();
240 m_game->sendDisplayMsg(&display, this);
241 display.resetButtons(); /* Remove Roll button */
242 sendDisplayMsg(&display);
243
244 // Take away many privileges
245 setBoolProperty("can_roll", false);
246 setBoolProperty("canrollagain", false);
247 setBoolProperty("can_buyestate", false);
248 setBoolProperty("canauction", false);
249
250 if (m_game->dice[0] == m_game->dice[1])
251 {
252 int doubleCount = getIntProperty("doublecount");
253 setProperty("doublecount", ++doubleCount);
254 if(doubleCount < 3)
255 setBoolProperty("canrollagain", true);
256 else
257 {
258 Display display;
259 display.setText("%s throws three doubles and is thrown into jail.", getStringProperty("name").c_str());
260 m_game->sendDisplayMsg(&display);
261
262 if (Estate *eJail = m_game->findNextJailEstate(m_estate))
263 toJail(eJail);
264 else
265 m_game->ioError("This gameboard does not have a jail estate.");
266
267 m_game->updateTurn();
268 return;
269 }
270 }
271 else
272 setProperty("doublecount", 0);
273
274 // Update player board position and nothing more. Other player
275 // properties are not actually updated yet, this happens when all
276 // clients have landed the player on the destination estate. Players do
277 // get asynchronous updates, these are calculated per client.
278 advance(m_game->dice[0] + m_game->dice[1], false);
279 }
280
281 void Player::endTurn(bool userRequest)
282 {
283 if (!getBoolProperty("hasturn")) {
284 if (userRequest)
285 ioError("You cannot end your turn, this is not your turn.");
286 return;
287 }
288
289 if (m_game->pausedForDialog()) {
290 if (userRequest)
291 ioError("You cannot end your turn, you must answer the dialog first.");
292 return;
293 }
294
295 if (m_game->auction()) {
296 if (userRequest)
297 ioError("You cannot end your turn, an auction is in progress.");
298 return;
299 }
300
301 if (m_game->clientsMoving()) {
302 if (userRequest)
303 ioError("You cannot end your turn, other players are still moving.");
304 return;
305 }
306
307 if (m_game->debts()) {
308 if (userRequest)
309 ioError("You cannot end your turn, there are debts to be settled.");
310 return;
311 }
312
313 if (getBoolProperty("can_buyestate")) {
314 if (!userRequest) {
315 return;
316 }
317
318 if (m_game->getBoolConfigOption("auctionsenabled")) {
319 ioError("You cannot end your turn, you must either buy or auction the property you are on.");
320 return;
321 }
322
323 setBoolProperty("can_buyestate", false);
324 setBoolProperty("canauction", false);
325 Display display;
326 display.resetButtons(); /* Remove buy estate buttons: Buy, Auction, End Turn */
327 sendDisplayMsg(&display);
328 }
329
330 if (getBoolProperty("canrollagain")) {
331 Display display;
332 display.setText("%s may roll again.", getStringProperty("name").c_str());
333 m_game->sendDisplayMsg(&display, this);
334 display.addButton(".r", "Roll", true);
335 sendDisplayMsg(&display);
336
337 setBoolProperty("can_roll", true);
338 setBoolProperty("canrollagain", false);
339 return;
340 }
341
342 if (getBoolProperty("can_roll")) {
343 if (userRequest)
344 ioError("You cannot end your turn, you must roll first!");
345 return;
346 }
347
348 if (getBoolProperty("jailed")) {
349 if (userRequest)
350 ioError("You cannot end your turn while jailed.");
351 return;
352 }
353
354 // Turn goes to next player
355 m_game->updateTurn();
356 }
357
358 void Player::payJail()
359 {
360 int payAmount = m_estate->getIntProperty("payamount");
361 if (!payMoney(payAmount))
362 {
363 ioError("Leaving jail costs %d, you only have %d.", payAmount, getIntProperty("money"));
364 return;
365 }
366 else if (m_game->getBoolConfigOption("collectfines"))
367 {
368 Estate * const ePayTarget = m_estate->payTarget();
369 if (ePayTarget)
370 ePayTarget->addMoney(payAmount);
371 }
372
373 setBoolProperty("jailed", false);
374 setBoolProperty("can_roll", true);
375 setBoolProperty("canusecard", false);
376 setProperty("jailcount", 0);
377
378 Display display;
379 display.setText("%s paid %d and has left jail, can roll now.", getStringProperty("name").c_str(), payAmount);
380 m_game->sendDisplayMsg(&display, this);
381 display.resetButtons(); /* Remove jail buttons: Pay, Use card, Roll */
382 display.addButton(".r", "Roll", true);
383 sendDisplayMsg(&display);
384 }
385
386 void Player::rollJail()
387 {
388 m_game->rollDice();
389
390 // Leave jail for free when doubles are rolled
391 if (m_game->dice[0] == m_game->dice[1])
392 {
393 setBoolProperty("jailed", false);
394
395 Display display;
396 display.setText("Doubles, leaving jail!");
397 m_game->sendDisplayMsg(&display, this);
398 display.resetButtons(); /* Remove jail buttons: Pay, Use card, Roll */
399 sendDisplayMsg(&display);
400
401 advance(m_game->dice[0] + m_game->dice[1], false);
402 return;
403 }
404
405 // No doubles, stay in jail the first three turns
406 int jailCount = getIntProperty("jailcount");
407 if(jailCount < 3)
408 {
409 Display display;
410 display.setText("No doubles, staying in jail.");
411 m_game->sendDisplayMsg(&display, this);
412 display.resetButtons(); /* Remove jail buttons: Pay, Use card, Roll */
413 sendDisplayMsg(&display);
414
415 m_game->updateTurn();
416 setProperty("jailcount", jailCount+1);
417 return;
418 }
419
420 // Third turn in jail, no doubles after three turns, must pay and leave.
421 const int payAmount = m_estate->getIntProperty("payamount");
422 setBoolProperty("jailed", false);
423 setBoolProperty("canusecard", false);
424 setProperty("jailcount", 0);
425
426 Display display;
427 display.setText("No doubles, %s must pay %d now and leave jail.", getStringProperty("name").c_str(), payAmount);
428 m_game->sendDisplayMsg(&display, this);
429 display.resetButtons(); /* Remove jail buttons: Pay, Use card, Roll */
430 sendDisplayMsg(&display);
431
432 Estate * const ePayTarget = m_estate->payTarget();
433 if (!payMoney(payAmount))
434 {
435 Display display;
436 display.setText("Game paused, %s owes %d but is not solvent. Player needs to raise %d in cash first.", getStringProperty("name").c_str(), payAmount, (payAmount - getIntProperty("money")));
437 m_game->sendDisplayMsg(&display);
438
439 setBoolProperty("canrollagain", true); // player can roll once debt is resolved
440 m_game->newDebt(this, 0, ePayTarget, payAmount);
441 return;
442 }
443
444 if (ePayTarget && m_game->getBoolConfigOption("collectfines"))
445 ePayTarget->addMoney(payAmount);
446
447 Display display2;
448 display2.addButton(".r", "Roll", true);
449 sendDisplayMsg(&display2);
450
451 setBoolProperty("can_roll", true);
452 }
453
454 void Player::useJailCard()
455 {
456 Card *card = findOutOfJailCard();
457 if (!card)
458 {
459 ioError("You don't have any 'out of jail' cards.");
460 return;
461 }
462 m_game->transferCard(card, 0);
463
464 setBoolProperty("can_roll", true);
465 setBoolProperty("jailed", false);
466 setBoolProperty("canusecard", false);
467 setProperty("jailcount", 0);
468
469 Display display;
470 display.setText("%s used card and has left jail, can roll now.", getStringProperty("name").c_str());
471 m_game->sendDisplayMsg(&display, this);
472 display.resetButtons(); /* Remove jail buttons: Pay, Use card, Roll */
473 display.addButton(".r", "Roll", true);
474 sendDisplayMsg(&display);
475 }
476
477 void Player::buyEstate()
478 {
479 if (!getBoolProperty("can_buyestate"))
480 {
481 ioError("You cannot buy anything at the moment.");
482 return;
483 }
484
485 if (!payMoney(m_estate->price()))
486 {
487 ioError("You do not have enough money to buy this property, %s costs %d.", m_estate->name().c_str(), m_estate->price());
488 return;
489 }
490
491 m_game->transferEstate(m_estate, this);
492
493 Display display;
494 display.setText("Purchased by %s for %d.", getStringProperty("name").c_str(), m_estate->price());
495 m_game->sendDisplayMsg(&display, this);
496 display.resetButtons(); /* Remove buy estate buttons: Buy, Auction, End Turn */
497 sendDisplayMsg(&display);
498
499 setBoolProperty("can_buyestate", false);
500 setBoolProperty("canauction", false);
501 endTurn();
502 }
503
504 void Player::sellEstate(int estateId)
505 {
506 if (!m_game->getBoolConfigOption("allowestatesales"))
507 {
508 ioError("Selling estates has been disabled in the configuration.");
509 return;
510 }
511
512 Estate *estate = m_game->findEstate(estateId);
513 if (!estate)
514 {
515 ioError("No such estateId: %d.", estateId);
516 return;
517 }
518
519 if (this != estate->owner())
520 {
521 ioError("You don't own '" + estate->name() + "'.");
522 return;
523 }
524
525 if (estate->getBoolProperty("mortgaged") || estate->groupHasBuildings())
526 {
527 ioError("You can't sell this estate, there are houses on its group, or is mortgaged.");
528 return;
529 }
530
531 // Sell the estate
532 int sellPrice = estate->price() / 2;
533
534 addMoney(sellPrice);
535 m_game->transferEstate(estate, 0);
536
537 Display display;
538 display.setText("%s sold %s for %d.", getStringProperty("name").c_str(), estate->getStringProperty("name").c_str(), sellPrice);
539 m_game->sendDisplayMsg(&display);
540
541 if (getBoolProperty("hasdebt"))
542 m_game->solveDebts(this);
543 }
544
545 void Player::mortgageEstate(int estateId)
546 {
547 Estate *estate = m_game->findEstate(estateId);
548 if (!estate)
549 {
550 ioError("No such estateId: %d.", estateId);
551 return;
552 }
553
554 if (this != estate->owner())
555 {
556 ioError("You don't own '" + estate->name() + "'.");
557 return;
558 }
559
560 if (estate->getBoolProperty("mortgaged"))
561 {
562 if (!payMoney(estate->getIntProperty("unmortgageprice")))
563 {
564 ioError("'Unmortgaging %s' costs %d, you only have %d.", estate->name().c_str(), estate->getIntProperty("unmortgageprice"), getIntProperty("money"));
565 return;
566 }
567 estate->setBoolProperty("mortgaged", false);
568 m_game->sendMsgEstateUpdate(estate);
569
570 Display display;
571 display.setText("%s unmortgaged %s for %d.", getStringProperty("name").c_str(), estate->getStringProperty("name").c_str(), estate->getIntProperty("unmortgageprice"));
572 m_game->sendDisplayMsg(&display);
573 }
574 else
575 {
576 // Mortgage the property, if possible!
577
578 if (estate->groupHasBuildings())
579 {
580 ioError("You can't mortgage '" + estate->name() + "', there are still houses in the same group.");
581 return;
582 }
583
584 estate->setBoolProperty("mortgaged", true);
585 m_game->sendMsgEstateUpdate(estate);
586
587 addMoney((estate->getIntProperty("mortgageprice")));
588
589 Display display;
590 display.setText("%s mortgaged %s for %d.", getStringProperty("name").c_str(), estate->getStringProperty("name").c_str(), estate->getIntProperty("mortgageprice"));
591 m_game->sendDisplayMsg(&display);
592
593 if (getBoolProperty("hasdebt"))
594 m_game->solveDebts(this);
595 }
596 }
597
598 void Player::payTax(const bool percentage)
599 {
600 if (!getBoolProperty("hasturn"))
601 return;
602
603 m_game->setPausedForDialog(false);
604
605 int payAmount = 0;
606 if (percentage)
607 payAmount = (int)( m_estate->getIntProperty("taxpercentage") * assets() / 100 );
608 else
609 payAmount = m_estate->getIntProperty("tax");
610
611 Display display;
612 display.setText("%s chooses to pay the %s tax, which amounts to %d.", getStringProperty("name").c_str(), (percentage ? "assets based" : "static"), payAmount);
613 m_game->sendDisplayMsg(&display, this);
614 display.resetButtons(); /* Remove pay tax buttons: Pay 200, Pay 10 Percent */
615 sendDisplayMsg(&display);
616
617 Estate * const ePayTarget = m_estate->payTarget();
618 if (!payMoney(payAmount))
619 {
620 Display display;
621 display.setText("Game paused, %s owes %d but is not solvent. Player needs to raise %d in cash first.", getStringProperty("name").c_str(), payAmount, (payAmount - getIntProperty("money")));
622 m_game->sendDisplayMsg(&display);
623
624 m_game->newDebt(this, 0, ePayTarget, payAmount);
625 return;
626 }
627
628 if (ePayTarget && m_game->getBoolConfigOption("collectfines"))
629 ePayTarget->addMoney(payAmount);
630
631 endTurn();
632 }
633
634 void Player::buyHouse(int estateId)
635 {
636 Estate *estate = m_game->findEstate(estateId);
637 if (!estate)
638 {
639 ioError("No such estateId: %d.", estateId);
640 return;
641 }
642
643 if (this != estate->owner())
644 {
645 ioError("You don't own '" + estate->name() + "'.");
646 return;
647 }
648 else if (!estate->housePrice())
649 {
650 ioError("You can't build houses on '" + estate->name() + "'.");
651 return;
652 }
653 else if (!estate->estateGroupIsMonopoly())
654 {
655 ioError("You don't own all estate in the group of '" + estate->name() + "'.");
656 return;
657 }
658 else if (estate->groupHasMortgages())
659 {
660 ioError("One or more estates in the group of '" + estate->name() + "' are mortgaged, so you cannot build houses there.");
661 return;
662 }
663 else if (estate->getIntProperty("houses")==5)
664 {
665 ioError("You already have a hotel on '" + estate->name() + "'.");
666 return;
667 }
668 else if (estate->getIntProperty("houses") >= estate->maxHouses())
669 {
670 ioError("The even build rule prevents you from building more house on '" + estate->name() + "'.");
671 return;
672 }
673
674 bool unlimitedHouses = m_game->getBoolConfigOption("unlimitedhouses");
675
676 if (!unlimitedHouses)
677 {
678 if (estate->getIntProperty("houses") < 4)
679 {
680 if (!m_game->houses())
681 {
682 ioError("Sorry, there are no more houses available!");
683 return;
684 }
685 }
686 else if (!m_game->hotels())
687 {
688 ioError("Sorry, there are no more hotels available!");
689 return;
690 }
691 }
692
693 int cost = estate->housePrice();
694 if (!payMoney(cost))
695 {
696 ioError("A house for %s costs %d, you only have %d.", estate->name().c_str(), cost, getIntProperty("money"));
697 return;
698 }
699
700 // Wow, we met all the right criteria? Let's build then!
701 estate->setProperty("houses", estate->getIntProperty("houses") + 1);
702 if (estate->getIntProperty("houses") == 5)
703 {
704 if (!unlimitedHouses)
705 {
706 m_game->setHotels(m_game->hotels() - 1);
707 m_game->setHouses(m_game->houses() + 4);
708 }
709
710 Display display;
711 display.setText("%s buys a hotel for %s.", getStringProperty("name").c_str(), estate->getStringProperty("name").c_str());
712 m_game->sendDisplayMsg(&display);
713 }
714 else
715 {
716 if (!unlimitedHouses)
717 m_game->setHouses(m_game->houses() - 1);
718
719 Display display;
720 display.setText("%s buys a house for %s.", getStringProperty("name").c_str(), estate->getStringProperty("name").c_str());
721 m_game->sendDisplayMsg(&display);
722 }
723
724 m_game->sendMsgEstateUpdate(estate);
725 }
726
727 void Player::sellHouse(int estateId)
728 {
729 Estate *estate = m_game->findEstate(estateId);
730 if (!estate)
731 {
732 ioError("No such estateId: %d.", estateId);
733 return;
734 }
735
736 if (this != estate->owner())
737 {
738 ioError("You don't own '" + estate->name() + "'.");
739 return;
740 }
741 else if (estate->getIntProperty("houses")==0)
742 {
743 ioError("There are no buildings on '" + estate->name() + "' to sell.");
744 return;
745 }
746 else if (estate->getIntProperty("houses") <= estate->minHouses())
747 {
748 ioError("You must level out the number of houses across your monopoly before you sell more on '" + estate->name() + "'.");
749 return;
750 }
751
752 bool unlimitedHouses = m_game->getBoolConfigOption("unlimitedhouses");
753 if ( !unlimitedHouses && (estate->getIntProperty("houses") == 5) && (m_game->houses() < 4) )
754 {
755 ioError("Hotel cannot be sold, Bank only has %d houses left.", m_game->houses());
756 return;
757 }
758
759 // Wow, we met all the right criteria? Let's sell!
760 addMoney(estate->getIntProperty("sellhouseprice"));
761 estate->setProperty("houses", estate->getIntProperty("houses") - 1);
762 if (estate->getIntProperty("houses") == 4)
763 {
764 if (!unlimitedHouses)
765 {
766 m_game->setHotels(m_game->hotels() + 1);
767 m_game->setHouses(m_game->houses() - 4);
768 }
769
770 Display display;
771 display.setText("%s sells a hotel from %s.", getStringProperty("name").c_str(), estate->getStringProperty("name").c_str());
772 m_game->sendDisplayMsg(&display);
773 }
774 else
775 {
776 if (!unlimitedHouses)
777 m_game->setHouses(m_game->houses() + 1);
778
779 Display display;
780 display.setText("%s sells a house from %s.", getStringProperty("name").c_str(), estate->getStringProperty("name").c_str());
781 m_game->sendDisplayMsg(&display);
782 }
783
784 m_game->sendMsgEstateUpdate(estate);
785 m_game->solveDebts(this);
786 }
787
788 void Player::updateTradeObject(char *data)
789 {
790 bool useEstate = false;
791 bool useCard = false;
792 if (data[0] == 'c')
793 useCard=true;
794 else if (data[0] == 'e')
795 useEstate = true;
796 else
797 return;
798
799 data++;
800
801 // data looks like "1:10:1", tradeid, objectid, playerid
802 if (!strstr(data, ":"))
803 {
804 ioError("Invalid input, no separator after tradeId");
805 return;
806 }
807 int tradeId = atoi(strsep(&data, ":"));
808
809 if (!strstr(data, ":"))
810 {
811 ioError("Invalid input, no separator after objectId");
812 return;
813 }
814 int objectId = atoi(strsep(&data, ":"));
815 int playerId = atoi(data);
816
817 Trade *trade = m_game->findTrade(tradeId);
818 if (!trade)
819 {
820 ioError("No such tradeId: %d.", tradeId);
821 return;
822
823 }
824 if (!trade->hasPlayer(this))
825 {
826 ioError("You are not part of trade %d.", tradeId);
827 return;
828 }
829
830 GameObject *object = 0;
831 Player *pFrom = 0;
832 if (useEstate)
833 {
834 Estate *estate = m_game->findEstate(objectId);
835 if (!estate)
836 {
837 ioError("No estate with estateId %d.", objectId);
838 return;
839 }
840 if (estate->groupHasBuildings())
841 {
842 ioError("You can't trade " + estate->name() + ", there are houses in the same group.");
843 return;
844 }
845 object = estate;
846 pFrom = estate->owner();
847 if (!pFrom)
848 return;
849 }
850 else if (useCard)
851 {
852 Card *card = m_game->findCard(objectId);
853 if (!card || !card->owner())
854 {
855 ioError("No one owns a card with cardId %d.", objectId);
856 return;
857 }
858 object = card;
859 pFrom = card->owner();
860 }
861
862 // NULL player allowed, Trade will delete the component.
863 Player *playerTo = m_game->findPlayer(playerId);
864 if (playerTo) {
865 if (playerTo->getBoolProperty("bankrupt")) {
866 ioError("You cannot trade with bankrupt players.");
867 return;
868 }
869 if (playerTo->getBoolProperty("spectator")) {
870 ioError("You cannot trade with spectators.");
871 return;
872 }
873 }
874
875 trade->updateObject(object, pFrom, playerTo);
876 }
877
878 void Player::updateTradeMoney(char *data)
879 {
880 // data looks like "1:1:1:100", tradeid, playerfrom, playerto, money
881 if (!strstr(data, ":"))
882 {
883 ioError("Invalid input for .Tm, no separator after tradeId");
884 return;
885 }
886 int tradeId = atoi(strsep(&data, ":"));
887
888 if (!strstr(data, ":"))
889 {
890 ioError("Invalid input for .Tm, no separator after playerFromId");
891 return;
892 }
893 int playerFromId = atoi(strsep(&data, ":"));
894
895 if (!strstr(data, ":"))
896 {
897 ioError("Invalid input for .Tm, no separator after playerToId");
898 return;
899 }
900
901 int playerToId = atoi(strsep(&data, ":"));
902 int money = atoi(data);
903
904 Trade *trade = m_game->findTrade(tradeId);
905 if (!trade)
906 {
907 ioError("No such tradeId: %d.", tradeId);
908 return;
909 }
910
911 if (!trade->hasPlayer(this))
912 {
913 ioError("You are not part of trade %d.", tradeId);
914 return;
915 }
916
917 Player *pFrom = m_game->findPlayer(playerFromId);
918 if (!pFrom)
919 {
920 ioError("No such playerFromId: %d.", playerFromId);
921 return;
922 }
923 if (pFrom->getBoolProperty("bankrupt")) {
924 ioError("You cannot trade with bankrupt players.");
925 return;
926 }
927 if (pFrom->getBoolProperty("spectator")) {
928 ioError("You cannot trade with spectators.");
929 return;
930 }
931 Player *pTo = m_game->findPlayer(playerToId);
932 if (!pTo)
933 {
934 ioError("No such playerToId: %d.", playerToId);
935 return;
936 }
937 if (pTo->getBoolProperty("bankrupt")) {
938 ioError("You cannot trade with bankrupt players.");
939 return;
940 }
941 if (pTo->getBoolProperty("spectator")) {
942 ioError("You cannot trade with spectators.");
943 return;
944 }
945
946 // TODO: should also count money player owes in other trade components
947 if(pFrom->getIntProperty("money") < money)
948 {
949 ioError("%s doesn't have %d to offer.", pFrom->name().c_str(), money);
950 return;
951 }
952
953 GameObject *object = trade->findMoneyObject(pFrom, pTo);
954 if (object)
955 object->setId(money);
956 else
957 object = new GameObject(money, GameObject::Money);
958
959 trade->updateObject(object, pFrom, pTo);
960 }
961
962 void Player::advance(int pos, bool direct)
963 {
964 int estates = m_game->estates();
965 int location = m_estate ? m_estate->id() : 0;
966
967 location += pos;
968 if (location >= estates)
969 location %= estates;
970 else if (location < 0)
971 location += estates;
972
973 advanceTo(location, direct, (pos >= 0));
974 }
975
976 void Player::advanceTo(int pos, bool direct, bool passEstates)
977 {
978 if (passEstates)
979 m_destination = m_game->findEstate(pos);
980 else
981 m_estate = m_game->findEstate(pos);
982
983 // Send message to clients to move token. The rest is done by token confirmation or timeout.
984 setProperty("location", m_destination ? m_destination->id() : m_estate->id(), m_game);
985 setBoolProperty("directmove", direct, m_game);
986
987 if (!direct)
988 m_game->setAllClientsMoving(m_estate);
989 }
990
991 void Player::toJail(Estate *jailEstate)
992 {
993 setProperty("doublecount", 0);
994 setProperty("jailcount", 1);
995 setBoolProperty("can_roll", false);
996 setBoolProperty("jailed", true);
997
998 advanceTo(jailEstate->id(), true, false);
999 }
1000
1001 void Player::addMoney(int amount)
1002 {
1003 setProperty("money", getIntProperty("money") + amount);
1004 }
1005
1006 bool Player::payMoney(int amount)
1007 {
1008 int money = getIntProperty("money");
1009 if (money < amount)
1010 return false;
1011
1012 setProperty("money", money - amount);
1013 return true;
1014 }
1015
1016 void Player::setSocket(Socket *socket)
1017 {
1018 m_socket = socket;
1019 if (m_socket)
1020 setProperty( "host", socket->ipAddr() );
1021 else
1022 setProperty( "host", "" );
1023 }
1024
1025 void Player::setTurn(const bool &turn)
1026 {
1027 setBoolProperty("hasturn", turn);
1028 setBoolProperty("can_buyestate", false);
1029 setBoolProperty("canauction", false);
1030
1031 if (turn)
1032 {
1033 if (getBoolProperty("jailed"))
1034 {
1035 Display display1;
1036 display1.setText("%s is in jail, turn %d.", name().c_str(), getIntProperty("jailcount"));
1037 display1.setEstate(m_estate);
1038 m_game->sendDisplayMsg(&display1, this);
1039
1040 Display display2;
1041 display2.setText("You are in jail, turn %d.", getIntProperty("jailcount"));
1042 display2.setEstate(m_estate);
1043 display2.addButton(".jp", "Pay", true);
1044 display2.addButton(".jc", "Use card", findOutOfJailCard());
1045 display2.addButton(".jr", "Roll", true);
1046 sendDisplayMsg(&display2);
1047
1048 setBoolProperty("canusecard", findOutOfJailCard());
1049 }
1050 else {
1051 Display display;
1052 display.addButton(".r", "Roll", true);
1053 sendDisplayMsg(&display);
1054
1055 setBoolProperty("can_roll", true);
1056 }
1057 }
1058 else
1059 {
1060 setBoolProperty("can_roll", false);
1061 setBoolProperty("canrollagain", false);
1062 }
1063 }
1064
1065 void Player::setGame(Game *game)
1066 {
1067 if (m_game != game)
1068 {
1069 m_game = game;
1070 setProperty("game", (m_game ? m_game->id() : -1));
1071 }
1072 }
1073
1074 void Player::setEstate(Estate *estate)
1075 {
1076 if (m_estate != estate)
1077 m_estate = estate;
1078 }
1079
1080 Estate *Player::estate()
1081 {
1082 return m_estate;
1083 }
1084
1085 void Player::setDestination(Estate *estate)
1086 {
1087 if (m_destination != estate)
1088 m_destination = estate;
1089 }
1090
1091 Estate *Player::destination()
1092 {
1093 return m_destination;
1094 }
1095
1096 void Player::setTokenLocation(Estate *estate)
1097 {
1098 m_tokenLocation = estate;
1099 }
1100
1101 Estate *Player::tokenLocation()
1102 {
1103 return m_tokenLocation;
1104 }
1105
1106 void Player::addCard(Card *card)
1107 {
1108 m_cards.push_back(card);
1109 }
1110
1111 Card *Player::findFirstCard()
1112 {
1113 if (m_cards.size() > 0)
1114 return m_cards[0];
1115
1116 return 0;
1117 }
1118
1119 void Player::takeCard(Card *card)
1120 {
1121 for(std::vector<Card *>::iterator it = m_cards.begin() ; it != m_cards.end() && (*it) ; ++it)
1122 if (*it == card)
1123 {
1124 m_cards.erase(it);
1125 return;
1126 }
1127 }
1128
1129 Card *Player::findCard(int cardId)
1130 {
1131 Card *card = 0;
1132 for(std::vector<Card *>::iterator it = m_cards.begin() ; it != m_cards.end() && (card = *it) ; ++it)
1133 if (card->id() == cardId)
1134 return card;
1135 return 0;
1136 }
1137
1138 Card *Player::findOutOfJailCard()
1139 {
1140 Card *card = 0;
1141 for(std::vector<Card *>::iterator it = m_cards.begin() ; it != m_cards.end() && (card = *it) ; ++it)
1142 if (card->outOfJail())
1143 return card;
1144 return 0;
1145 }
1146
1147 int Player::assets()
1148 {
1149 if (m_game)
1150 return m_game->playerAssets(this);
1151 else
1152 return 0;
1153 }