File: turno.cpp

package info (click to toggle)
teg 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,036 kB
  • sloc: cpp: 16,819; xml: 1,313; makefile: 268; sh: 195; ansic: 112
file content (191 lines) | stat: -rw-r--r-- 4,374 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Tenes Empanadas Graciela
 *
 * Copyright (C) 2000 Ricardo Quesada
 *
 * Author: Ricardo Calixto Quesada <rquesada@core-sdi.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; only version 2 of the License
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/**
 * @file turno.c
 * Manejo de turnos
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "../common/missions.h"
#include "server.h"
#include "tegdebug.h"
#include "missions.h"

#define TURNO_DEBUG PDEBUG

namespace teg::server
{

/* Gives turn to the next player */
TEG_STATUS turno_2nextplayer(PSPLAYER *ppJ)
{
	PSPLAYER next = find_next_player(*ppJ,
	[](SPLAYER &p) {
		return player_is_playing(&p);
	});
	if(!next) {
		con_text_out_wop(M_ERR, "Abnormal error in turno_2nextplayer\n");
		return TEG_STATUS_PLAYERNOTFOUND;
	}

	TURNO_DEBUG("Old turn: '%s'\n", (*ppJ)->name);
	g_game.old_turn = *ppJ;

	TURNO_DEBUG("New turn: '%s'\n", next->name);
	*ppJ = next;
	return TEG_STATUS_SUCCESS;
}

/* Ends the player turn */
TEG_STATUS turno_end(PSPLAYER pJ)
{
	assert(pJ);

	/* FIXME: Creo que igual este chequeo no sirve, porque se chequea cuando
	 * se conquista un country. Solo sirve si uno gana sin conquistar algun country
	 * Puede pasar ???
	 */
	if(mission_chequear(pJ) == TEG_STATUS_GAMEOVER) {
		game_end(pJ);
		return TEG_STATUS_GAMEOVER;
	}

	player_clear_turn(pJ);
	return TEG_STATUS_SUCCESS;
}

/* Gives turn to the next player */
TEG_STATUS turno_next(void)
{
	assert(g_game.turno);

	/* give turn to the next one */
	if(turno_2nextplayer(&g_game.turno) == TEG_STATUS_SUCCESS) {

		/* the one who has the turn is the one that started ? */
		if(turno_is_round_complete()) {

			turno_initialize_new_round();

			aux_token_fichasc(g_game.turno);

		} else {
			g_game.turno->estado = PLAYER_STATUS_ATAQUE;
			netall_printf(TOKEN_TURNO"=%d\n", g_game.turno->numjug);
		}
		return TEG_STATUS_SUCCESS;
	}

	return TEG_STATUS_ERROR;
}

/* initializes the round. Called before the game starts */
TEG_STATUS turno_init(void)
{
	int i;
	int real_i;
	PSPLAYER j;

	i = random_between(0, g_game.playing-1);

	player_from_indice(i, &real_i);
	if(player_whois(real_i, &j) != TEG_STATUS_SUCCESS) {
		return TEG_STATUS_ERROR;
	}

	g_game.old_turn = NULL;
	g_game.turno = j;
	g_game.empieza_turno = j;

	return TEG_STATUS_SUCCESS;
}

/* return true if the round is complete */
bool turno_is_round_complete()
{
	/* I want to know if the round is over. It is not enought to know
	 * if newturn == started because if a player with the turn exit the game
	 * he will never receive the turn again, but the started turn will point
	 * to him
	 */

	if(g_game.old_turn == NULL) {
		return false;
	}

	if(g_game.empieza_turno == g_game.turno) {
		return true;
	}

	/*
	 * if the previous playing player of 'empieza turn' is 'old turn' then
	 * the round is over
	 */
	auto *next = find_next_player(g_game.old_turn,
	[](SPLAYER& player) {
		return player.is_player;
	});
	if(next) {
		if(next == g_game.empieza_turno) {
			return true;
		} else if(next == g_game.turno) {
			return false;
		}
	}

	/* abnormal error */
	fprintf(stderr, "Abnormal error in turno_is_round_complete()\n");
	return false;
}

/* initialize variables for the new round */
void turno_initialize_new_round(void)
{
	/* So, a new round is started */
	turno_2nextplayer(&g_game.empieza_turno);
	g_game.turno = g_game.empieza_turno;

	g_game.round_number++;

	/* add the continents that it defend */
	player_map(
	[](PSPLAYER pJ) {
		unsigned long conts;
		player_listar_conts(pJ, &conts);
		int i;
		for(i=0; i<CONT_CANT; i++) {
			if(conts & 1) {
				pJ->player_stats.continents_turn[i]++;
			}
			conts >>= 1;
		}

		/* update the score */
		stats_score(&pJ->player_stats, g_conts);
	});

	netall_printf(TOKEN_NEW_ROUND"=%d,%d\n", g_game.turno->numjug, g_game.round_number);

}

}