File: reagrupe.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 (229 lines) | stat: -rw-r--r-- 6,280 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* 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 reagrue.c
 * Contiene algunas funciones auxiliares para el manejo del estado 'ESTADO_REAGRUPE'
 */

#include <filesystem>
#include "../common/net.h"

#include "fcintl.h"
#include "client.h"
#include "protocol.h"

namespace teg::client
{

static int country_origen = -1;
static int country_destino = -1;

/* valores que guardan lo ultimo enviado, y en caso de error se borra el ejer_reagrupe */
static int last_origen = -1;
static int last_destino = -1;
static int last_cant = -1;

TEG_STATUS reagrupe_check(void)
{
	PLAYER_STATUS e = ESTADO_GET();

	if(e>=PLAYER_STATUS_ATAQUE && e<=PLAYER_STATUS_REAGRUPE) {
		ESTADO_SET(PLAYER_STATUS_REAGRUPE);
		return TEG_STATUS_SUCCESS;
	} else {
		return TEG_STATUS_ERROR;
	}
}

TEG_STATUS reagrupe_click(PCOUNTRY p)
{
	if(reagrupe_check() != TEG_STATUS_SUCCESS) {
		textmsg(M_ERR, _("Error, It's not the time to regroup"));
		return TEG_STATUS_UNEXPECTED;
	}

	if(country_origen == -1) {
		if(p->numjug == WHOAMI()) {
			if(p->ejercitos - p->ejer_reagrupe > 1) {
				p->selected &= ~COUNTRY_SELECT_REGROUP_ENTER;
				p->selected |= COUNTRY_SELECT_REGROUP;
				callbacks::gui_country_select(p->id);
				country_origen = p->id;
				textmsg(M_INF, _("Source country: '%s'. Now select the destination country"), countries_get_name(p->id));
			} else {
				textmsg(M_ERR, _("Error, '%s' doesnt have any avalaible armies to move"), countries_get_name(p->id));
				return TEG_STATUS_UNEXPECTED;
			}
		} else {
			textmsg(M_ERR, _("Error, '%s' isnt one of your countries"), countries_get_name(p->id));
			return TEG_STATUS_UNEXPECTED;
		}
	} else if(country_destino == -1) {
		if(country_origen == p->id) {
			textmsg(M_INF, _("Source country is the same as the destination. Resetting the regroup..."));
			reagrupe_reset();
			return TEG_STATUS_SUCCESS;
		}

		if(p->numjug == WHOAMI()) {
			if(countries_eslimitrofe(country_origen, p->id)) {
				p->selected &= ~COUNTRY_SELECT_REGROUP_ENTER;
				p->selected |= COUNTRY_SELECT_REGROUP;
				callbacks::gui_country_select(p->id);
				country_destino = p->id;
				textmsg(M_INF, _("Destination country: '%s'. Now select the quantity of armies to move"), countries_get_name(p->id));
				callbacks::gui_reagrupe(country_origen, country_destino, g_countries[country_origen].ejercitos - g_countries[country_origen].ejer_reagrupe - 1);
			} else {
				textmsg(M_ERR, _("Error, '%s' isnt frontier with '%s'"), countries_get_name(p->id), countries_get_name(country_origen));
				return TEG_STATUS_UNEXPECTED;
			}
		} else {
			textmsg(M_ERR, _("Error, '%s' isnt one of your countries"), countries_get_name(p->id));
			reagrupe_reset();
			return TEG_STATUS_UNEXPECTED;
		}
	} else {
		textmsg(M_ERR, _("Error, unexpected error in reagrupe_click(). Report this bug!"));
		reagrupe_reset();
		return TEG_STATUS_UNEXPECTED;
	}

	return TEG_STATUS_SUCCESS;
}

void reagrupe_reset(void)
{
	if(country_origen != -1) {
		g_countries[country_origen].selected &= ~COUNTRY_SELECT_REGROUP;
		callbacks::gui_country_select(country_origen);
		country_origen = -1;
	}

	if(country_destino != -1) {
		g_countries[country_destino].selected &= ~COUNTRY_SELECT_REGROUP;
		callbacks::gui_country_select(country_origen);
		country_destino = -1;
	}
}

void reagrupe_bigreset(void)
{
	int i;

	reagrupe_reset();

	for(i=0; i<COUNTRIES_CANT; i++) {
		g_countries[i].ejer_reagrupe = 0;
	}
	last_origen = -1;
	last_destino = -1;
	last_cant = 0;
}

TEG_STATUS reagrupe_init(void)
{
	attack_reset();

	if(reagrupe_check() != TEG_STATUS_SUCCESS) {
		textmsg(M_ERR, _("Error, you can't regroup your armies now"));
		return TEG_STATUS_ERROR;
	}
	reagrupe_reset();
	return TEG_STATUS_SUCCESS;
}

void reagrupe_set_and_save(int src, int dst, int cant)
{
	last_origen = src;
	last_destino = dst;
	last_cant = cant;
	g_countries[ dst ].ejer_reagrupe += cant;
}

TEG_STATUS reagrupe_restore_from_error(void)
{
	if(last_origen != -1 && last_destino != -1 && last_cant >= 0) {
		g_countries[ last_destino ].ejer_reagrupe -= last_cant;
		return TEG_STATUS_SUCCESS;
	} else {
		return TEG_STATUS_ERROR;
	}

}

TEG_STATUS reagrupe_out(int src, int dst, int cant)
{
	PLAYER_STATUS e;

	e = ESTADO_GET();
	if(e==PLAYER_STATUS_REAGRUPE) {
		reagrupe_reset();
		reagrupe_set_and_save(src, dst, cant);
		net_printf(g_game.fd, TOKEN_REAGRUPE"=%d,%d,%d\n", src, dst, cant);
	} else {
		textmsg(M_ERR, _("Error, you cant regroup now."));
		return TEG_STATUS_ERROR;
	}

	return TEG_STATUS_SUCCESS;
}

/**
 * @fn TEG_STATUS reagrupe_enter( PCOUNTRY p )
 * Cuando se esta reagrupando resalta los countries que se pueden reagrupar
 * @param p Pais a resaltar
 */
TEG_STATUS reagrupe_enter(PCOUNTRY p)
{
	if(reagrupe_check() != TEG_STATUS_SUCCESS) {
		return TEG_STATUS_UNEXPECTED;
	}

	if(country_origen == -1) {
		if(p->numjug == WHOAMI()) {
			if(p->ejercitos - p->ejer_reagrupe > 1) {
				p->selected |= COUNTRY_SELECT_REGROUP_ENTER;
				callbacks::gui_country_select(p->id);
			}
		}
	} else if(country_destino == -1) {
		if(p->numjug == WHOAMI()) {
			if(countries_eslimitrofe(country_origen, p->id)) {
				p->selected |= COUNTRY_SELECT_REGROUP_ENTER;
				callbacks::gui_country_select(p->id);
			}
		}
	}
	return TEG_STATUS_SUCCESS;
}

TEG_STATUS reagrupe_leave(PCOUNTRY p)
{
	if(reagrupe_check() != TEG_STATUS_SUCCESS) {
		return TEG_STATUS_UNEXPECTED;
	}
	if(p->selected & COUNTRY_SELECT_REGROUP_ENTER) {
		p->selected &= ~COUNTRY_SELECT_REGROUP_ENTER;
		callbacks::gui_country_select(p->id);
	}
	return TEG_STATUS_SUCCESS;
}

}