File: ai_fichas.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 (370 lines) | stat: -rw-r--r-- 7,717 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
/* 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_fichas.c
 * AI for placing armies
 */

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

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

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

namespace teg::robot
{

namespace c = ::teg::client;

/**
 * @fn TEG_STATUS ai_fichas_calc_puntaje_conquer( int country )
 * Strategy: Try to conquer continent
 */
TEG_STATUS ai_fichas_calc_puntaje_conquer(int country)
{
	int i;
	int pc;			/* countries del continente */
	int pe=0;		/* countries enemigos */
	int ee=0;		/* ejercitos enemigos */
	int pm=0;		/* countries mios */
	int em=0;		/* ejercitos mios */
	int ple=0;		/* countries limitrofes enemigos */


	if(ai_own_continent(g_countries[country].continente)) {
		return TEG_STATUS_SUCCESS;
	}

	pc = g_conts[ g_countries[country].continente ].cant_countries;

	for(i=0; i<COUNTRIES_CANT; i++) {
		if(g_countries[country].continente == g_countries[i].continente) {
			if(g_countries[i].numjug == c::WHOAMI()) {
				pm++;
				em += g_countries[i].ejercitos;
			} else {
				pe++;
				ee += g_countries[i].ejercitos;
				if(countries_eslimitrofe(country, i)) {
					ple++;
				}
			}
		}
	}

	/* si tengo mas del xx% de los countries del cont suma puntos */
	if(pc != pm) {
		/* ASIA */
		if(pc >= 12) {
			if(pm >= 10) {
				ai_puntaje[country] += 1;
			}
			if(pm >= 11) {
				ai_puntaje[country] += 2;
			}
			if(pm >= 12) {
				ai_puntaje[country] += 7;
			}
			if(pm < 7) {
				ai_puntaje[country] -= 5;
			}
		}
		/* EUROPE, NORTH AMERICA */
		else if(pc >= 8) {
			if(pm >= 5) {
				ai_puntaje[country] += 1;
			}
			if(pm >= 7) {
				ai_puntaje[country] += 2;
			}
			if(pm >= 8) {
				ai_puntaje[country] += 7;
			}
		}
		/* SOUTH AMERICA, AFRICA */
		else if(pc >= 6) {
			if(pm >= 4) {
				ai_puntaje[country] += 2;
			}
			if(pm >= 5) {
				ai_puntaje[country] += 7;
			}
		}
		/* OCEANIA */
		if(pc >= 4) {
			if(pm >= 2) {
				ai_puntaje[country] += 2;
			}
			if(pm >= 3) {
				ai_puntaje[country] += 3;
			}
		}
		ai_puntaje[country] += ple;
		if(em > ee) {
			ai_puntaje[country] += 3;
		}

		if(ple == 0) {
			ai_puntaje[country] -= 50;
		}
	}
	return TEG_STATUS_SUCCESS;
}

/**
 * @fn TEG_STATUS ai_fichas_calc_puntaje_defense( int country )
 * Strategy: Try to defend continent
 */
TEG_STATUS ai_fichas_calc_puntaje_defense(int country)
{
	int i;
	CONTINENTE c = g_countries[country].continente;
	int suma=0;
	int f=0;		/* fronteras que tiene */

	if(!ai_own_continent(c)) {
		return TEG_STATUS_SUCCESS;
	}

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

				suma += 5;

				/* increase points if the enemy has more armies */
				if(g_countries[i].ejercitos > g_countries[country].ejercitos) {
					suma += 6;
				}
			} else {
				suma += 1;
			}

			/* increase points if I have a few armies */
			if(g_countries[country].ejercitos < 4) {
				suma += 3;
			}

			f++;
		}
	}

	/* if it is no border, dont place on him */
	if(f == 0) {
		ai_puntaje[country] -= 50;
	}

	ai_puntaje[country] += suma + (f/2);

	return TEG_STATUS_SUCCESS;
}


/**
 * @fn TEG_STATUS ai_fichas_calc_puntaje_fichas( int country )
 * Strategy: Try to conquer other countries
 */
TEG_STATUS ai_fichas_calc_puntaje_fichas(int country)
{
	int i;
	int p1=0;
	int p2=0;
	int suma=0;

	int cant_country = g_conts[g_countries[country].continente].cant_countries;

	/* suma las fichas de los countries limitrofes enemigos */
	for(i=0; i< COUNTRIES_CANT; i++) {
		if(g_countries[i].numjug != c::WHOAMI() && countries_eslimitrofe(country, i)) {

			/* si es un enemigo ya tiene punto */
			suma += 3;

			if(g_countries[country].ejercitos >= g_countries[i].ejercitos) {
				p2 += 2 + g_countries[country].ejercitos - g_countries[i].ejercitos;
			} else {
				p1 +=g_countries[i].ejercitos - g_countries[country].ejercitos;
			}
		}
	}

	if(suma == 0) {
		return TEG_STATUS_SUCCESS;
	}

	suma += p1/2 + p2/2;

	/* ASIA */
	if(cant_country >= 12) {
		suma -= 1;
	}

	/* EUROPE, NORTH AMERICA */
	else if(cant_country >= 8) {
		suma += 1;
	}

	/* SOUTH AMERICA, AFRICA */
	else if(cant_country >= 6) {
		suma += 3;
	}

	/* OCENIA */
	if(cant_country >= 4) {
		suma += 3;
	}

	ai_puntaje[country] += suma;

	return TEG_STATUS_SUCCESS;
}

TEG_STATUS ai_fichas_calc_puntaje(int p)
{
	ai_fichas_calc_puntaje_conquer(p);
	ai_fichas_calc_puntaje_fichas(p);
	ai_fichas_calc_puntaje_defense(p);

	return TEG_STATUS_SUCCESS;
}


/**
 * @fn TEG_STATUS __ai_fichas( int cant )
 * Pone fichas en los countries.
 */
TEG_STATUS ai_fichas_helper(int cant)
{
	int i;

	/* all unavailble countries are -10000 */
	ai_puntaje_clean();

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

	if(ai_puntaje_sort(cant) != TEG_STATUS_SUCCESS) {
		c::textmsg(M_ERR, _("Error in ai_puntaje_sort, terminating the robot. Variable cant was %d"), cant);
		return TEG_STATUS_ERROR;
	}

	for(i=0; i<cant; i++) {
		if(c::fichas_add(&g_countries[ ai_sorted[i] ]) != TEG_STATUS_SUCCESS) {
			c::textmsg(M_ERR, _("Failed to fichas_add(%s)"), g_countries[ai_sorted[i]].name);
			return TEG_STATUS_ERROR;
		}
	}
	return TEG_STATUS_SUCCESS;
}

TEG_STATUS ai_fichas(int cant)
{
	ai_puntaje_clean();

	if(ai_fichas_helper(cant) != TEG_STATUS_SUCCESS) {
		return TEG_STATUS_ERROR;
	}

	if(c::fichas_out() != TEG_STATUS_SUCCESS) {
		return TEG_STATUS_ERROR;
	}

	return TEG_STATUS_SUCCESS;
}


/**
 * @fn TEG_STATUS ai_fichas_en_cont( int cont )
 * Place armies in the continents
 */
TEG_STATUS ai_fichas_en_cont(int cont)
{
	int cant = g_conts[cont].fichas_x_cont;
	int i;

	/* all unavailble countries are -10000 */
	ai_puntaje_clean();

	for(i=0; i<COUNTRIES_CANT; i++) {
		if(g_countries[i].continente == g_conts[cont].id) {
			ai_puntaje[i] = 0;
			ai_fichas_calc_puntaje_defense(i);
		}
	}

	ai_puntaje_sort(cant);

	for(i=0; i<cant; i++) {
		if(c::fichas_add(&g_countries[ai_sorted[i]]) != TEG_STATUS_SUCCESS) {
			c::textmsg(M_ERR, _("Failed to fichas_add(%s)"), g_countries[ai_sorted[i]].name);
			return TEG_STATUS_ERROR;
		}
	}

	return TEG_STATUS_SUCCESS;
}

/**
 * @fn TEG_STATUS ai_fichasc( int cant, int conts)
 * pone fichas de continente y de eso.
 */
TEG_STATUS ai_fichasc(int cant, int conts)
{
	int i;
	int conts_tmp;

	ai_puntaje_clean();

	/* pongo las fichas del continente */
	conts_tmp = conts;
	for(i=0; i<CONT_CANT; i++) {
		if(conts_tmp & 1) {
			ai_fichas_en_cont(i);
		}
		conts_tmp >>= 1;
	}

	ai_puntaje_clean();

	/* y ahora las fichas donde creo conveniente */
	if(ai_fichas_helper(cant) != TEG_STATUS_SUCCESS) {
		c::textmsg(M_ERR, _("Failed to __ai_fichas( %d )"), cant);
		return TEG_STATUS_ERROR;
	}

	if(c::fichas_out() != TEG_STATUS_SUCCESS) {
		c::textmsg(M_ERR, _("Failed to fichas_out()"));
		return TEG_STATUS_ERROR;
	}

	return TEG_STATUS_SUCCESS;
}

}