File: attack.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 (293 lines) | stat: -rw-r--r-- 7,375 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* 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.
 */
/*
 * manages the client side of the attack
 */

#include <filesystem>
#include "attack.h"

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

namespace teg::client
{

/// \internal The origin of the ongoing attack
static int country_origen = -1;

//// \internal The target of the current attack
static int country_destino = -1;

/// \internal Copy of the origin country, used for attack replay (Ctrl-R)
static int country_origen_bak = -1;

/// \internal Copy of the target country, used for attack replay
static int country_destino_bak = -1;

/// \internal Used in the gnome gui only as a helper to show ongoing attacks
static int show_src = -1;

/// \internal Used in the gnome gui only as a helper to show ongoing attacks
static int show_dst = -1;

/* are the necesary conditions met for the attack ? */
static TEG_STATUS attack_check()
{
	PLAYER_STATUS e;

	e = ESTADO_GET();

	if(e==PLAYER_STATUS_ATAQUE || e==PLAYER_STATUS_TROPAS) {
		ESTADO_SET(PLAYER_STATUS_ATAQUE);
		return TEG_STATUS_SUCCESS;
	} else {
		return TEG_STATUS_ERROR;
	}
}

/* reset the status of the attack */
void attack_reset()
{
	if(country_origen != -1) {
		g_countries[country_origen].selected &= ~COUNTRY_SELECT_ATTACK;
		callbacks::gui_country_select(country_origen);
		country_origen = -1;
	}

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

void attack_restore()
{
	country_origen = country_origen_bak;
	country_destino = country_destino_bak;
}

void attack_backup()
{
	country_origen_bak = country_origen;
	country_destino_bak = country_destino;
}

TEG_STATUS attack_init()
{
	if(attack_check() != TEG_STATUS_SUCCESS) {
		return TEG_STATUS_UNEXPECTED;
	}

	attack_backup(); /// \todo This looks wrong here.
	attack_reset();

	return TEG_STATUS_SUCCESS;
}

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

	if(country_origen == -1) {
		if(p->numjug == WHOAMI()) {
			if(p->ejercitos >1) {
				p->selected &= ~COUNTRY_SELECT_ATTACK_ENTER;
				p->selected |= COUNTRY_SELECT_ATTACK;
				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' must have at least 2 armies"), 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 attack..."));
			attack_reset();
			return TEG_STATUS_SUCCESS;
		}

		if(p->numjug != WHOAMI()) {
			if(countries_eslimitrofe(country_origen, p->id)) {
				p->selected &= ~COUNTRY_SELECT_ATTACK_ENTER;
				p->selected |= COUNTRY_SELECT_ATTACK;
				callbacks::gui_country_select(p->id);
				country_destino = p->id;
				textmsg(M_INF, _("Destination country: '%s'. Attacking..."), countries_get_name(p->id));
				attack_out();
			} else {
				textmsg(M_ERR, _("Error, '%s' isnt frontier with '%s'"), countries_get_name(p->id), countries_get_name(country_origen));
				attack_reset();
				return TEG_STATUS_UNEXPECTED;
			}
		} else {
			textmsg(M_ERR, _("Error, you cant attack your own countries ('%s')"), countries_get_name(p->id));
			attack_reset();
			return TEG_STATUS_UNEXPECTED;
		}
	} else {
		attack_reset();
		textmsg(M_ERR, _("Error, unexpected error in attack_click(). Report this bug!"));
		return TEG_STATUS_UNEXPECTED;
	}

	return TEG_STATUS_SUCCESS;
}

TEG_STATUS attack_finish(int *ori, int *dst)
{
	if(attack_check() != TEG_STATUS_SUCCESS) {
		return TEG_STATUS_UNEXPECTED;
	}

	if(country_destino == -1 || country_origen == -1) {
		return TEG_STATUS_ERROR;
	}

	*ori = country_origen;
	*dst = country_destino;

	return TEG_STATUS_SUCCESS;
}


TEG_STATUS attack_pre_reset()
{
	if(attack_check() != TEG_STATUS_SUCCESS) {
		return TEG_STATUS_ERROR;
	} else {
		attack_reset();
		return TEG_STATUS_SUCCESS;
	}
}


/* Sends to server the attack message */
TEG_STATUS attack_out()
{
	int src;
	int dst;

	if(attack_finish(&src, &dst) != TEG_STATUS_SUCCESS)  {
		textmsg(M_ERR, _("Error, make sure to select the countries first."));
		attack_init();
		return TEG_STATUS_ERROR;
	}

	net_printf(g_game.fd, TOKEN_ATAQUE"=%d,%d\n", src, dst);
	attack_backup();
	return TEG_STATUS_SUCCESS;
}

/* Sends to server the attack message */
TEG_STATUS attackre_out()
{
	attack_restore();
	return attack_out();
}

/* the mouse is over a country */
TEG_STATUS attack_enter(PCOUNTRY p)
{
	if(attack_check() != TEG_STATUS_SUCCESS) {
		return TEG_STATUS_UNEXPECTED;
	}

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

TEG_STATUS attack_leave(PCOUNTRY p)
{
	if(attack_check() != TEG_STATUS_SUCCESS) {
		return TEG_STATUS_UNEXPECTED;
	}
	if(p->selected & COUNTRY_SELECT_ATTACK_ENTER) {
		p->selected &= ~COUNTRY_SELECT_ATTACK_ENTER;
		callbacks::gui_country_select(p->id);
	}
	return TEG_STATUS_SUCCESS;
}

/* unshow the attack shown with attack_show() */
void attack_unshow()
{

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

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

/* shows attack an attack from srt to dst */
void attack_show(int src, int dst)
{
	attack_unshow();

	if(src >= 0 && src < COUNTRIES_CANT) {
		show_src = src;
		g_countries[src].selected |= COUNTRY_SELECT_ATTACK_SRC;
		callbacks::gui_country_select(src);
	}

	if(dst >= 0 && dst < COUNTRIES_CANT) {
		show_dst = dst;
		g_countries[dst].selected |= COUNTRY_SELECT_ATTACK_DST;
		callbacks::gui_country_select(dst);
	}
}

}