File: ai.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 (410 lines) | stat: -rw-r--r-- 8,526 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* Tenes Empanadas Graciela
 *
 * Copyright (C) 2000 Ricardo Quesada
 *
 * Author: Ricardo Calixto Quesada <riq@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 ai.c
 * Inteligecia artificial del robot
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>

#include "client.h"
#include "ai.h"
#include "ai_fichas.h"

namespace teg::robot
{

namespace c = ::teg::client;

/**
 * Risk of buffer overflow: When cards are traded for armies, there is NO upper
 * limit for how many armies you can place! When set to 50 this was a cause of
 * overflows when games lasted longer. But 500 armies should be enough for everyone.
*/
int ai_puntaje[COUNTRIES_CANT*10];
int ai_sorted[COUNTRIES_CANT*10];
static int attack_dst[COUNTRIES_CANT];

TEG_STATUS ai_puntaje_sort(int cant)
{
	int i, j;
	int max;

	//check if the buffer is big enough
	if(cant > COUNTRIES_CANT * 10) {
		return TEG_STATUS_ERROR;
	}

	for(j=0; j<cant; j++) {
		max = 0;
		for(i=0; i<COUNTRIES_CANT; i++) {
			if(ai_puntaje[i] > ai_puntaje[max]) {
				max = i;
			}
		}
		ai_sorted[j] = max;
		ai_puntaje[max] -= 2;
	}
	return TEG_STATUS_SUCCESS;
}

/**
 * @fn TEG_STATUS ai_puntaje_sort_for_attack( int cant)
 * Algoritmo para ordernar como se va a hacer el attack
 */
TEG_STATUS ai_puntaje_sort_for_attack()
{
	int i;
	int max=0;

	for(i=0; i<COUNTRIES_CANT; i++) {
		if(ai_puntaje[i] > ai_puntaje[max]) {
			max = i;
		}
	}
	ai_sorted[0] = max;
	return TEG_STATUS_SUCCESS;
}

void ai_puntaje_clean()
{
	int i;
	for(i=0; i<COUNTRIES_CANT; i++) {
		ai_puntaje[i] = -10000;
	}
}

/**
 * @fn int ai_many_country_enemigo( int p )
 * Dice la cantidad de countries enemigos que tiene un country
 */
int ai_many_country_enemigo(int p)
{
	int i;
	int c=0;

	for(i=0; i<COUNTRIES_CANT; i++) {
		if(g_countries[i].numjug != c::WHOAMI() && countries_eslimitrofe(p, i)) {
			c++;
		}
	}

	return c;
}

/**
 * @fn BOOLEAN ai_is_country_border( int p )
 * Tells if a country border of its continent
 */
bool ai_is_country_border(int p)
{
	int i;
	for(i=0; i<COUNTRIES_CANT; i++) {
		if(countries_eslimitrofe(i, p) && g_countries[i].continente != g_countries[p].continente) {
			return true;
		}
	}

	return false;
}

bool ai_own_continent(CONTINENTE c)
{
	int i;
	int t=0;

	for(i=0; i<COUNTRIES_CANT; i++) {
		if(g_countries[i].numjug == c::WHOAMI()
		        && g_countries[i].continente == c) {
			t++;
		}
	}

	return (t == g_conts[c].cant_countries);

}

TEG_STATUS ai_init()
{
	ai_puntaje_clean();
	return TEG_STATUS_SUCCESS;
}

/**
 * This function decides if it is dangerous to attack country dst by src. It is
 * taken into consideration if the the player of the country to be attacked has
 * already done with the turn.
 */
bool ai_is_country_peligroso(int src, int dst)
{
	/// \todo rename and scope limit variables
	bool aparecio_empezo = false;
	bool dst_jugo = false;
	int tmp;

	c::players_map_int(
	[&aparecio_empezo, dst, &dst_jugo](c::Player& player) {

		if(player.empezo_turno) {
			aparecio_empezo = true;
		}

		if(player.numjug == g_countries[dst].numjug) {
			dst_jugo = aparecio_empezo;
			return false;
		}
		return true;
	});

	if(dst_jugo) {
		return false;
	}

	tmp = (g_countries[src].ejercitos > 3) ? 3: g_countries[src].ejercitos-1;
	if(g_countries[dst].ejercitos > (g_countries[src].ejercitos-tmp)) {
		return true;
	}
	return false;
}

/**
 * @fn int ai_puntaje_atacar_warning( int src )
 * Resta puntos si existe un country limitrofe a 'src' que puede atacarlo antes
 * de que se acabe el turno
 */
int ai_puntaje_atacar_warning(int src)
{
	int p=0;
	int i;

	for(i=0; i<COUNTRIES_CANT; i++) {
		if(countries_eslimitrofe(src, i) &&
		        g_countries[src].numjug != g_countries[i].numjug &&
		        ai_is_country_peligroso(src, i)) {

			p += (g_countries[i].ejercitos - g_countries[src].ejercitos);
		}
	}

	return -(5+2*p);
}


/**
 * @fn int ai_puntaje_atacar_country( int src, int dst )
 * Dice los puntos que tiene de atacar src a dst
 */
int ai_puntaje_atacar_country(int src, int dst)
{
	int p=0;

	if(g_countries[src].ejercitos > g_countries[dst].ejercitos) {

		if(g_countries[src].continente == g_countries[dst].continente) {
			p++;
		}

		p += 10 + g_countries[src].ejercitos - g_countries[dst].ejercitos;

	} else {

		int r = random_between(1, 10);

		if((r>7) && (g_countries[src].ejercitos > 7)) {
			p++;
		}
	}

	return p;
}

/**
 * @fn int ai_puntaje_attack( int p )
 * Evalua los puntos que tiene el country para atacar
 * y dice al country que convendria atacar
 */
TEG_STATUS ai_puntaje_atacar(int country)
{
	int i;
	int p=0;
	int p_tmp;
	int dst=-1;

	for(i=0; i<COUNTRIES_CANT; i++) {
		if(countries_eslimitrofe(country, i) && g_countries[i].numjug != c::WHOAMI()) {
			p_tmp = ai_puntaje_atacar_country(country, i);

			/* son negativos estos valores, por eso sumo */
			p_tmp += ai_puntaje_atacar_warning(country);

			if(p_tmp > p) {
				p = p_tmp;
				dst = i;
			}
		}
	}

	/* return the best candidate */
	if(dst >=0) {
		ai_puntaje[ country ] = p;
		attack_dst[ country ] = dst;
	}

	return TEG_STATUS_SUCCESS;
}

TEG_STATUS ai_turno()
{
	int i;
	int j;

	ai_puntaje_clean();
	for(i=0; i<COUNTRIES_CANT; i++) {
		if(g_countries[i].numjug == c::WHOAMI() && g_countries[i].ejercitos > 1) {
			ai_puntaje_atacar(i);
		}
	}
	ai_puntaje_sort_for_attack();

	i = ai_sorted[0];
	j = attack_dst[i];
	if(ai_puntaje[i] > 0) {

		c::attack_init();
		c::attack_click(&g_countries[i]);
		c::attack_click(&g_countries[j]);
		return TEG_STATUS_SUCCESS;
	}
	return TEG_STATUS_ERROR;
}

TEG_STATUS __ai_reagrupe(int  p, int cant)
{
	int i;
	int own = ai_own_continent(g_countries[p].continente);

	if(own) {
		int new_cant = cant;

		/* try to defend, undefended frontiers */
		for(i=0; i<COUNTRIES_CANT; i++) {
			if(g_countries[i].numjug != c::WHOAMI() &&
			        countries_eslimitrofe(i, p) &&
			        g_countries[i].ejercitos > g_countries[p].ejercitos) {

				c::reagrupe_out(p, i, cant);
				return TEG_STATUS_SUCCESS;
			}
		}

		/* I will leave 3 armies in border country of the continent */
		if(g_countries[p].ejercitos - g_countries[p].ejer_reagrupe - cant < 3) {
			new_cant -= 2;
		}

		for(i=0; i<COUNTRIES_CANT && (new_cant>0); i++) {
			if(g_countries[i].numjug == c::WHOAMI() && countries_eslimitrofe(p, i) &&
			        g_countries[i].continente != g_countries[p].continente) {

				c::reagrupe_out(p, i, new_cant);
				return TEG_STATUS_SUCCESS;
			}
		}
	}

	/*... si falla, los muevo a una frontera */
	for(i=0; i<COUNTRIES_CANT; i++) {
		if(g_countries[i].numjug == c::WHOAMI() && countries_eslimitrofe(p, i) &&
		        g_countries[i].continente == g_countries[p].continente) {

			if(ai_is_country_border(i)) {
				c::reagrupe_out(p, i, cant);
				return TEG_STATUS_SUCCESS;
			}
		}
	}

	/*... y si no hay frontera lo mando al 1ro que tenga a mano */
	for(i=0; i<COUNTRIES_CANT; i++) {
		if(g_countries[i].numjug == c::WHOAMI() && countries_eslimitrofe(p, i) &&
		        g_countries[i].continente == g_countries[p].continente) {

			c::reagrupe_out(p, i, cant);
			return TEG_STATUS_SUCCESS;
		}
	}

	return TEG_STATUS_ERROR;
}

TEG_STATUS ai_reagrupe()
{
	int i, p;
	c::reagrupe_init();

	for(i=0; i < COUNTRIES_CANT ; i++) {
		if(g_countries[i].numjug == c::WHOAMI()) {
			if(ai_many_country_enemigo(i) == 0) {
				p = g_countries[i].ejercitos - g_countries[i].ejer_reagrupe;

				if(p < 2) {
					continue;
				}

				__ai_reagrupe(i, p-1);
			}
		}
	}
	return TEG_STATUS_SUCCESS;
}

TEG_STATUS ai_tropas(int src, int dst, int cant)
{
	int i;
	int c=0;

	ai_puntaje_clean();
	ai_fichas_calc_puntaje(src);
	ai_fichas_calc_puntaje(dst);

	for(i=0; i<cant; i++) {
		if(ai_puntaje[dst] > ai_puntaje[src] + i) {
			c++;
		}
	}

	if(c > 0) {
		c::out_tropas(src, dst, c);
	}

	return TEG_STATUS_SUCCESS;
}

TEG_STATUS ai_puedocanje(int *p1, int *p2, int *p3)
{
	return c::canje_puedo(p1, p2, p3);
}

}