File: fow.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 (183 lines) | stat: -rw-r--r-- 4,193 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
/* 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 "../common/net.h"
#include "server.h"
#include "fow.h"

namespace teg::server
{

TEG_STATUS fow_set_mode(bool b)
{
	if(JUEGO_EMPEZADO) {
		return TEG_STATUS_ERROR;
	}

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

bool fow_can_player_see_country(PSPLAYER pJ, PCOUNTRY pP)
{
	if(! pJ  || ! pP) {
		return false;
	}

	bool result{false};
	countries_map_int(pJ->numjug, [pP, &result](COUNTRY& country) {
		if(pP->id == country.id) {
			result = true;
			return false;
		}

		if(countries_eslimitrofe(pP->id, country.id)) {
			result = true;
			return false;
		}
		return true;
	});

	return result;
}

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 const *format, ...)
{
	va_list args;
	char buf[PROT_MAX_LEN];

	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;

	player_map([buf, country](PSPLAYER pJ) {
		if(pJ->fd > 0) {
			/* check if he has a boundry country wih 'country' */
			countries_map_int(
			pJ->numjug, [country, &buf, pJ](COUNTRY& country_to_inspect) -> bool {
				CountryId const cid{country_to_inspect.id};
				if((cid == country) || countries_eslimitrofe(cid, country))
				{
					net_print(pJ->fd, buf);
					return false;
				}
				return true;
			});
		}
	});
	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 const *format, ...)
{
	va_list args;
	char buf[PROT_MAX_LEN];

	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;

	player_map([buf, src, dst](PSPLAYER pJ) {
		if(pJ->fd > 0) {
			/* check if he has a boundry country wih src && dst */

			int src_country = -1;
			int dst_country = -1;
			countries_map_int(pJ->numjug,
			[src, dst, &src_country, &dst_country](COUNTRY& country) {

				if(country.id == src || countries_eslimitrofe(country.id, src)) {
					src_country = src;
				}

				if(country.id == dst || countries_eslimitrofe(country.id, dst)) {
					dst_country = dst;
				}

				return (dst_country == -1) && (src_country == -1);
			});

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

}