File: fow.c

package info (click to toggle)
teg 0.11.2%2Bdebian-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,340 kB
  • sloc: ansic: 17,600; sh: 8,546; python: 1,442; xml: 1,310; makefile: 357
file content (198 lines) | stat: -rw-r--r-- 4,769 bytes parent folder | download | duplicates (6)
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
/*	$Id: fow.c,v 1.8 2002/08/31 17:46:00 riq Exp $	*/
/* Tenes Empanadas Graciela
 *
 * Copyright (C) 2002 Ricardo Quesada
 *
 * Author: Ricardo Calixto Quesada <riq@corest.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.
 */
/* Fog of War */

#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#include "server.h"

#include "fow.h"

TEG_STATUS fow_set_mode( BOOLEAN b )
{
	if( JUEGO_EMPEZADO )
		return TEG_STATUS_ERROR;

	if( b!=0)
		b=1;
	g_game.fog_of_war = b;
	return TEG_STATUS_SUCCESS;
}

BOOLEAN fow_can_player_see_country( PSPLAYER pJ, PCOUNTRY pP )
{
	PLIST_ENTRY pL;
	PCOUNTRY pcountry;

	if( ! pJ  || ! pP )
		return FALSE;

	pL = pJ->countries.Flink;
	while( !IsListEmpty( &pJ->countries ) && (pL != &pJ->countries) ) {
		pcountry = (PCOUNTRY) pL;

		if( countries_eslimitrofe(pP->id, pcountry->id) )
			return TRUE;

		if(  pP->id == pcountry->id )
			return TRUE;

		pL = LIST_NEXT(pL);
	}

	return FALSE;
}

TEG_STATUS fow_fill_with_boundaries( int country, char *buffer, int buf_len )
{
	int i;
	int first_time=1;

	if( ! g_game.player_fow )
		return TEG_STATUS_ERROR;

	for( i=0; i < COUNTRIES_CANT; i++ )
	{
		if( i != country && g_countries[i].numjug != g_game.player_fow->numjug && countries_eslimitrofe( country, i ) )
		{
			char buf[1024];

			if( first_time ) {
				snprintf( buf, sizeof(buf)-1,"%s=%d,%d,%d", TOKEN_COUNTRY, i, g_countries[i].numjug, g_countries[i].ejercitos );
				first_time = 0;
			} else
				snprintf( buf, sizeof(buf)-1,";%s=%d,%d,%d", TOKEN_COUNTRY, i, g_countries[i].numjug, g_countries[i].ejercitos );

			strncat( buffer, buf, buf_len );
		}
	}


	/* dont send, since I didnt modify the buffer */
	if( first_time )
		return TEG_STATUS_ERROR;

	return TEG_STATUS_SUCCESS;
}

int fow_netall_printf( int country, char *format, ...)
{
        va_list args;
	char buf[PROT_MAX_LEN];
	PLIST_ENTRY pLplayer = g_list_player.Flink;
	PLIST_ENTRY pLcountry;
	PSPLAYER pJ;
	PCOUNTRY pP;

	if( country < 0 || country >= COUNTRIES_CANT )
		return -1;

	va_start(args, format);
	vsnprintf(buf, sizeof(buf) -1, format, args);
	va_end(args);

	buf[ sizeof(buf) -1 ] = 0;

	while( !IsListEmpty( &g_list_player ) && (pLplayer != &g_list_player) ) {
		pJ = (PSPLAYER) pLplayer;
		if( pJ->fd > 0 && pJ->is_player )
		{
			pLcountry = pJ->countries.Flink;
			/* check if he has a boundry country wih 'country' */
			while( !IsListEmpty( &pJ->countries ) && (pLcountry != &pJ->countries) ) {
				pP = (PCOUNTRY) pLcountry;

				if( countries_eslimitrofe(pP->id, country) || pP->id == country )
				{
					net_print(pJ->fd, buf);
					break;
				}

				pLcountry = LIST_NEXT(pLcountry);
			}

		}

		pLplayer = LIST_NEXT( pLplayer );
	}
	return 0;
}

/*
 * IMPORTANT: it is very important the user can't control the format
 * in this function to prevent a format string vulnerability
 *
 * After format is parsed, 2 "%d" should be present, because they will
 * be filled with src & dst
 */
int fow_2_netall_printf( int src, int dst, char *format, ...)
{
        va_list args;
	char buf[PROT_MAX_LEN];
	PLIST_ENTRY pLplayer = g_list_player.Flink;
	PLIST_ENTRY pLcountry;
	PSPLAYER pJ;
	PCOUNTRY pP;
	int src_country;
	int dst_country;

	if( src<0 || src>=COUNTRIES_CANT || dst<0 || dst>=COUNTRIES_CANT)
		return -1;

	va_start(args, format);
	vsnprintf(buf, sizeof(buf) -1, format, args);
	va_end(args);

	buf[ sizeof(buf) -1 ] = 0;

	while( !IsListEmpty( &g_list_player ) && (pLplayer != &g_list_player) ) {
		pJ = (PSPLAYER) pLplayer;
		if( pJ->fd > 0 && pJ->is_player )
		{
			pLcountry = pJ->countries.Flink;
			/* check if he has a boundry country wih src && dst */

			src_country = -1;
			dst_country = -1;
			while( !IsListEmpty( &pJ->countries ) && (pLcountry != &pJ->countries) ) {
				pP = (PCOUNTRY) pLcountry;

				if(countries_eslimitrofe(pP->id, src) || pP->id == src)
					src_country = src;

				if(countries_eslimitrofe(pP->id, dst) || pP->id == dst)
					dst_country = dst;

				if( dst_country >= 0 && src_country >=0 )
					break;

				pLcountry = LIST_NEXT(pLcountry);
			}

			net_printf(pJ->fd, buf, src_country, dst_country);
		}

		pLplayer = LIST_NEXT( pLplayer );
	}
	return 0;
}