1 /* Pioneers - Implementation of the excellent Settlers of Catan board game.
2  *   Go buy a copy.
3  *
4  * Copyright (C) 1999 Dave Cole
5  * Copyright (C) 2003 Bas Wijnen <shevek@fmf.nl>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "config.h"
23 #include <string.h>
24 #include "cost.h"
25 #include "server.h"
26 
resource_available(Player * player,gint * resources,gint * num_in_bank)27 gboolean resource_available(Player * player, gint * resources,
28 			    gint * num_in_bank)
29 {
30 	Game *game = player->game;
31 	gint idx;
32 
33 	if (num_in_bank != NULL)
34 		*num_in_bank = 0;
35 	for (idx = 0; idx < NO_RESOURCE; idx++) {
36 		if (resources[idx] > game->bank_deck[idx]) {
37 			player_send(player, FIRST_VERSION, LATEST_VERSION,
38 				    "ERR no-cards %r\n", idx);
39 			return FALSE;
40 		}
41 		if (num_in_bank != NULL)
42 			*num_in_bank += game->bank_deck[idx];
43 	}
44 
45 	return TRUE;
46 }
47 
resource_start(Game * game)48 void resource_start(Game * game)
49 {
50 	GList *list;
51 
52 	for (list = player_first_real(game);
53 	     list != NULL; list = player_next_real(list)) {
54 		Player *player = list->data;
55 
56 		memcpy(player->prev_assets,
57 		       player->assets, sizeof(player->assets));
58 		player->gold = 0;
59 	}
60 }
61 
resource_end(Game * game,const gchar * action,gint mult)62 void resource_end(Game * game, const gchar * action, gint mult)
63 {
64 	GList *list;
65 
66 	for (list = player_first_real(game);
67 	     list != NULL; list = player_next_real(list)) {
68 		Player *player = list->data;
69 		gint resource[NO_RESOURCE];
70 		guint idx;
71 		gboolean send_message = FALSE;
72 
73 		for (idx = 0; idx < G_N_ELEMENTS(player->assets); idx++) {
74 			gint num;
75 
76 			num =
77 			    player->assets[idx] - player->prev_assets[idx];
78 			if (game->bank_deck[idx] - num < 0) {
79 				num = game->bank_deck[idx];
80 				player->assets[idx]
81 				    = player->prev_assets[idx] + num;
82 			}
83 
84 			resource[idx] = num;
85 			if (num != 0)
86 				send_message = TRUE;
87 
88 			game->bank_deck[idx] -= num;
89 		}
90 
91 		if (send_message) {
92 			for (idx = 0; idx < NO_RESOURCE; idx++)
93 				resource[idx] *= mult;
94 			player_broadcast(player, PB_ALL, FIRST_VERSION,
95 					 LATEST_VERSION, "%s %R\n", action,
96 					 resource);
97 		}
98 	}
99 }
100 
resource_spend(Player * player,const gint * cost)101 void resource_spend(Player * player, const gint * cost)
102 {
103 	Game *game = player->game;
104 
105 	resource_start(game);
106 	cost_buy(cost, player->assets);
107 	resource_end(game, "spent", -1);
108 }
109 
resource_refund(Player * player,const gint * cost)110 void resource_refund(Player * player, const gint * cost)
111 {
112 	Game *game = player->game;
113 
114 	resource_start(game);
115 	cost_refund(cost, player->assets);
116 	resource_end(game, "refund", 1);
117 }
118