Ejemplo n.º 1
0
/* take a point from an other player */
void player_take_point(gint player_num, gint id, gint old_owner)
{
	Player *player = player_get(player_num);
	Player *victim = player_get(old_owner);
	Points *point;
	GList *list;
	/* look up the point in the list */
	for (list = victim->points; list != NULL; list = g_list_next(list)) {
		point = list->data;
		if (point->id == id)
			break;
	}
	/* communication error: the point doesn't exist */
	if (list == NULL) {
		log_message(MSG_ERROR,
			    _("server asks to move invalid point.\n"));
		return;
	}
	/* move the point in memory */
	victim->points = g_list_remove(victim->points, point);
	player->points = g_list_append(player->points, point);
	/* tell the user someone (1) lost something (2) to someone else (3) */
	log_message(MSG_INFO, _("%s lost %s to %s.\n"), victim->name,
		    point->name, player->name);
}
Ejemplo n.º 2
0
/* lose a point: noone gets it */
void player_lose_point(gint player_num, gint id)
{
	Player *player = player_get(player_num);
	Points *point;
	GList *list;
	/* look up the point in the list */
	for (list = player->points; list != NULL; list = g_list_next(list)) {
		point = list->data;
		if (point->id == id)
			break;
	}
	/* communication error: the point doesn't exist */
	if (list == NULL) {
		log_message(MSG_ERROR,
			    _("server asks to lose invalid point.\n"));
		return;
	}
	player->points = g_list_remove(player->points, point);
	player_modify_points(player_num, point, FALSE);
	/* tell the user the point is lost */
	log_message(MSG_INFO, _("%s lost %s.\n"), player->name,
		    point->name);
	/* free the memory */
	points_free(point);
	g_free(point);
}
Ejemplo n.º 3
0
const gchar *player_name(gint player_num, gboolean word_caps)
{
	static gchar buff[256];
	if (player_num >= num_total_players) {
		/* this is about a viewer */
		Viewer *viewer = viewer_get(player_num);
		if (viewer != NULL)
			return viewer->name;
		else {
			if (word_caps)
				sprintf(buff, _("Viewer %d"), player_num);
			else
				sprintf(buff, _("viewer %d"), player_num);
			return buff;
		}
	} else if (player_num >= 0) {
		Player *player = player_get(player_num);
		return player->name;
	}
	if (word_caps)
		sprintf(buff, _("Player %d"), player_num);
	else
		sprintf(buff, _("player %d"), player_num);
	return buff;
}
Ejemplo n.º 4
0
void player_has_quit(gint player_num)
{
	Player *player;
	Viewer *viewer;

	if (player_num < 0)
		return;

	/* usually callbacks are called after the event has been handled.
	 * Here it is called before, so the frontend can access the
	 * information about the quitting player/viewer */

	if (player_num >= num_total_players) {
		/* a viewer has quit */
		callbacks.viewer_quit(player_num);
		viewer = viewer_get(player_num);
		g_free(viewer->name);
		g_free(viewer->style);
		viewers = g_list_remove(viewers, viewer);
		return;
	}
	callbacks.player_quit(player_num);
	player = player_get(player_num);
	log_message(MSG_INFO, _("%s has quit\n"), player_name(player_num,
							      TRUE));
}
Ejemplo n.º 5
0
void player_modify_points(gint player_num, Points * points, gboolean added)
{
	Player *player = player_get(player_num);
	/* if !added -> is already removed */
	if (added)
		player->points = g_list_append(player->points, points);
	callbacks.new_points(player_num, points, added);
}
Ejemplo n.º 6
0
/* get a new point */
void player_get_point(gint player_num, gint id, const gchar * str,
		      gint num)
{
	Player *player = player_get(player_num);
	/* create the point */
	Points *point = points_new(id, str, num);
	player_modify_points(player_num, point, TRUE);
	/* tell the user that someone got something */
	log_message(MSG_INFO, _("%s received %s.\n"), player->name, str);
}
Ejemplo n.º 7
0
gboolean quote_view_has_reject(QuoteView * qv, gint player_num)
{
	Player *player = player_get(player_num);

	if (qv->quote_list == NULL)
		return FALSE;

	quote_found_flag = FALSE;
	gtk_tree_model_foreach(GTK_TREE_MODEL(qv->store),
			       trade_locate_reject, player);
	return quote_found_flag;
}
Ejemplo n.º 8
0
const gchar *player_get_style(gint player_num)
{
	const gchar *style = NULL;
	if (player_num >= num_total_players) {
		Viewer *viewer = viewer_get(player_num);
		style = viewer->style;
	} else if (player_num >= 0) {
		Player *player = player_get(player_num);
		style = player->style;
	}
	return style == NULL ? "square" : style;
}
Ejemplo n.º 9
0
void quote_view_remove_rejected_quotes(QuoteView * qv)
{
	gint idx;

	for (idx = 0; idx < num_players(); idx++) {
		Player *player = player_get(idx);
		quote_found_flag = FALSE;
		gtk_tree_model_foreach(GTK_TREE_MODEL(qv->store),
				       trade_locate_reject, player);
		if (quote_found_flag)
			gtk_list_store_remove(qv->store,
					      &quote_found_iter);
	}
}
Ejemplo n.º 10
0
void player_set_style(gint player_num, const gchar * style)
{
	if (player_num >= num_total_players) {
		Viewer *viewer = viewer_get(player_num);
		if (viewer->style)
			g_free(viewer->style);
		viewer->style = g_strdup(style);
	} else if (player_num >= 0) {
		Player *player = player_get(player_num);
		if (player->style)
			g_free(player->style);
		player->style = g_strdup(style);
	}
}
Ejemplo n.º 11
0
/** Player <I>player_num</I> has rejected trade */
void quote_view_reject(QuoteView * qv, gint player_num)
{
	Player *player = player_get(player_num);
	QuoteInfo *quote;
	GtkTreeIter iter;
	enum TFindResult found;
	GdkPixbuf *pixbuf;

	if (qv->quote_list == NULL)
		return;

	while ((quote =
		quotelist_find_domestic(qv->quote_list, player_num,
					-1)) != NULL) {
		remove_quote(qv, quote);
	}

	quote_found_flag = FALSE;
	gtk_tree_model_foreach(GTK_TREE_MODEL(qv->store),
			       trade_locate_reject, player);
	if (quote_found_flag)	/* Already removed */
		return;

	/* work out where to put the reject row
	 */
	for (quote = quotelist_first(qv->quote_list);
	     quote != NULL; quote = quotelist_next(quote))
		if (!quote->is_domestic)
			continue;
		else if (quote->var.d.player_num >= player_num)
			break;

	found =
	    find_integer_in_tree(GTK_TREE_MODEL(qv->store), &iter,
				 TRADE_COLUMN_PLAYER_NUM, player_num);
	if (found != FIND_NO_MATCH)
		gtk_list_store_insert_before(qv->store, &iter, &iter);
	else
		gtk_list_store_append(qv->store, &iter);
	pixbuf = player_create_icon(GTK_WIDGET(qv), player_num, TRUE);
	gtk_list_store_set(qv->store, &iter, TRADE_COLUMN_PLAYER, pixbuf,
			   TRADE_COLUMN_POSSIBLE, cross_pixbuf,
			   /* Trade: a player has rejected trade */
			   TRADE_COLUMN_DESCRIPTION, _("Rejected trade"),
			   TRADE_COLUMN_QUOTE, NULL,
			   TRADE_COLUMN_REJECT, player,
			   TRADE_COLUMN_PLAYER_NUM, player_num, -1);
	g_object_unref(pixbuf);
}
Ejemplo n.º 12
0
void player_change_name(gint player_num, const gchar * name)
{
	Player *player;
	gchar *old_name;

	if (player_num < 0)
		return;
	if (player_num >= num_total_players) {
		/* this is about a viewer */
		Viewer *viewer = viewer_get(player_num);
		if (viewer == NULL) {
			/* there is a new viewer */
			viewer = g_malloc0(sizeof(*viewer));
			viewers = g_list_prepend(viewers, viewer);
			viewer->num = player_num;
			viewer->name = NULL;
			viewer->style = g_strdup("square");
			old_name = NULL;
		} else {
			old_name = viewer->name;
		}
		if (old_name == NULL)
			log_message(MSG_INFO, _("New viewer: %s.\n"),
				    name);
		else
			log_message(MSG_INFO, _("%s is now %s.\n"),
				    old_name, name);
		viewer->name = g_strdup(name);
		if (old_name != NULL)
			g_free(old_name);
		callbacks.viewer_name(player_num, name);
		return;
	}

	player = player_get(player_num);
	old_name = player->name;
	player->name = g_strdup(name);
	if (old_name == NULL)
		log_message(MSG_INFO, _("Player %d is now %s.\n"),
			    player_num, name);
	else if (strcmp(old_name, name))
		log_message(MSG_INFO, _("%s is now %s.\n"), old_name,
			    name);
	if (old_name != NULL)
		g_free(old_name);
	callbacks.player_name(player_num, name);
}
Ejemplo n.º 13
0
gint find_player_by_name(const gchar * name)
{
	gint i;
	GList *list;

	for (i = 0; i < num_total_players; i++) {
		Player *player = player_get(i);
		if (player->name && !strcmp(player->name, name))
			return i;
	}

	for (list = viewers; list != NULL; list = g_list_next(list)) {
		Viewer *viewer = list->data;
		if (!strcmp(viewer->name, name))
			return viewer->num;
	}
	return -1;
}
Ejemplo n.º 14
0
gint player_get_score(gint player_num)
{
	Player *player = player_get(player_num);
	GList *list;
	gint i, score;

	for (i = 0, score = 0; i < G_N_ELEMENTS(player->statistics); i++) {
		score += stat_get_vp_value(i) * player->statistics[i];
	}

	list = player->points;
	while (list) {
		Points *points = list->data;
		score += points->points;
		list = g_list_next(list);
	}
	return score;
}
Ejemplo n.º 15
0
/** Returns TRUE if the edge can be robbed. */
static gboolean can_edge_be_robbed(MapElement edge,
				   G_GNUC_UNUSED int owner,
				   MapElement pirate)
{
	gint idx;

	/* Can only steal from ships that are not owned by me */
	if (edge.edge->type != BUILD_SHIP
	    || edge.edge->owner == my_player_num())
		return FALSE;

	/* Can only steal if the owner has some resources */
	if (player_get(edge.edge->owner)->statistics[STAT_RESOURCES] == 0)
		return FALSE;

	/* Can only steal from edges adjacent to hex with pirate */
	for (idx = 0; idx < G_N_ELEMENTS(edge.edge->hexes); idx++)
		if (edge.edge->hexes[idx] == pirate.hex)
			return TRUE;
	return FALSE;
}
Ejemplo n.º 16
0
/* Return TRUE if the node can be robbed. */
static gboolean can_building_be_robbed(MapElement node,
				       G_GNUC_UNUSED int owner,
				       MapElement robber)
{
	gint idx;

	/* Can only steal from buildings that are not owned by me */
	if (node.node->type == BUILD_NONE
	    || node.node->owner == my_player_num())
		return FALSE;

	/* Can only steal if the owner has some resources */
	if (player_get(node.node->owner)->statistics[STAT_RESOURCES] == 0)
		return FALSE;

	/* Can only steal from buildings adjacent to hex with robber */
	for (idx = 0; idx < G_N_ELEMENTS(node.node->hexes); idx++)
		if (node.node->hexes[idx] == robber.hex)
			return TRUE;
	return FALSE;
}
Ejemplo n.º 17
0
void player_largest_army(gint player_num)
{
	gint idx;
	if (player_num < 0)
		log_message(MSG_LARGESTARMY,
			    _("There is no largest army.\n"));
	else
		log_message(MSG_LARGESTARMY,
			    _("%s has the largest army.\n"),
			    player_name(player_num, TRUE));
	for (idx = 0; idx < num_total_players; idx++) {
		Player *player = player_get(idx);
		if (player->statistics[STAT_LARGEST_ARMY] != 0
		    && idx != player_num)
			player_modify_statistic(idx, STAT_LARGEST_ARMY,
						-1);
		if (player->statistics[STAT_LARGEST_ARMY] == 0
		    && idx == player_num)
			player_modify_statistic(idx, STAT_LARGEST_ARMY, 1);
	}
}
Ejemplo n.º 18
0
void player_longest_road(gint player_num)
{
	gint idx;

	if (player_num < 0)
		log_message(MSG_LONGESTROAD,
			    _("There is no longest road.\n"));
	else
		log_message(MSG_LONGESTROAD,
			    _("%s has the longest road.\n"),
			    player_name(player_num, TRUE));

	for (idx = 0; idx < num_total_players; idx++) {
		Player *player = player_get(idx);

		if (player->statistics[STAT_LONGEST_ROAD] != 0
		    && idx != player_num)
			player_modify_statistic(idx, STAT_LONGEST_ROAD,
						-1);
		if (player->statistics[STAT_LONGEST_ROAD] == 0
		    && idx == player_num)
			player_modify_statistic(idx, STAT_LONGEST_ROAD, 1);
	}
}
Ejemplo n.º 19
0
void player_modify_statistic(gint player_num, StatisticType type, gint num)
{
	Player *player = player_get(player_num);
	player->statistics[type] += num;
	callbacks.new_statistics(player_num, type, num);
}