File: network.c

package info (click to toggle)
liquidwar 5.6.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 15,912 kB
  • ctags: 2,624
  • sloc: ansic: 25,422; xml: 3,985; sh: 3,355; makefile: 1,378; asm: 1,344; python: 537; php: 486; sql: 22
file content (249 lines) | stat: -rw-r--r-- 8,251 bytes parent folder | download | duplicates (4)
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
/********************************************************************/
/*                                                                  */
/*            L   I  QQ  U U I DD    W   W  A  RR    555            */
/*            L   I Q  Q U U I D D   W   W A A R R   5              */
/*            L   I Q  Q U U I D D   W W W AAA RR    55             */
/*            L   I Q Q  U U I D D   WW WW A A R R     5            */
/*            LLL I  Q Q  U  I DD    W   W A A R R   55             */
/*                                                                  */
/*                             b                                    */
/*                             bb  y y                              */
/*                             b b yyy                              */
/*                             bb    y                              */
/*                                 yy                               */
/*                                                                  */
/*                     U U       FFF  O   O  TTT                    */
/*                     U U       F   O O O O  T                     */
/*                     U U TIRET FF  O O O O  T                     */
/*                     U U       F   O O O O  T                     */
/*                      U        F    O   O   T                     */
/*                                                                  */
/********************************************************************/

/*****************************************************************************/
/* Liquid War is a multiplayer wargame                                       */
/* Copyright (C) 1998-2007 Christian Mauduit                                 */
/*                                                                           */
/* 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; either version 2 of the License, or         */
/* (at your option) any later version.                                       */
/*                                                                           */
/* 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
/*                                                                           */
/* Liquid War homepage : http://www.ufoot.org/liquidwar/v5                   */
/* Contact author      : ufoot@ufoot.org                                     */
/*****************************************************************************/

/********************************************************************/
/* name          : network.c                                        */
/* content       : various network related stuff                    */
/* last update   : may 8th 2001                                     */
/********************************************************************/

/*==================================================================*/
/* includes                                                         */
/*==================================================================*/

#include <stdlib.h>

#include <allegro.h>

#include "network.h"
#include "config.h"

/*==================================================================*/
/* constants                                                        */
/*==================================================================*/

#define LW_NETWORK_COLOR_NB               12
#define LW_NETWORK_COLOR_DIST_MAXINT 1000000
#define LW_NETWORK_COLOR_DIST_SCALE     1000

/*==================================================================*/
/* globals                                                          */
/*==================================================================*/

int LW_NETWORK_ON = 0;
LW_WHO LW_NETWORK_INFO[NB_TEAMS];
int LW_NETWORK_ERROR_DETECTED = 0;
void *LW_NETWORK_RAW_MAP = NULL;

/*==================================================================*/
/* static funcs                                                     */
/*==================================================================*/


/*==================================================================*/
/* functions                                                        */
/*==================================================================*/

/*------------------------------------------------------------------*/
/*
 * Returns the "distance" of 2 colors. For 2 adjacent which look
 * about the sane, it is 1. For radicaly different colors, it is
 * 6. If the colors are the same, it is 0.
 */
int
distance_between_colors (int col1, int col2)
{
  int dist;

  if (col1 < 0 || col2 < 0)
    {
      /*
       * Now if one of the member was -1, we
       * consider the colors are "far"
       * from each other.
       */
      dist = 6;
    }
  else
    {
      dist = abs (col1 - col2);
    }

  if (dist > LW_NETWORK_COLOR_NB)
    {
      /*
       * Normally we should never get here...
       */
      dist = LW_NETWORK_COLOR_NB / 2;
    }

  if (dist > LW_NETWORK_COLOR_NB / 2)
    {
      dist = LW_NETWORK_COLOR_NB - dist;
    }

  return dist;
}

/*------------------------------------------------------------------*/
/*
 * Calculates the "most different color", considering the colors 
 * already chosen in LW_NETWORK_INFO.
 */
int
choose_different_color ()
{
  int color;
  int closeness[LW_NETWORK_COLOR_NB];
  int closeness_min;
  int dist;
  int i, j;

  /*
   * We calculate the "closeness" of each color
   */
  for (i = 0; i < LW_NETWORK_COLOR_NB; ++i)
    {
      closeness[i] = 0;
      for (j = 0; j < NB_TEAMS; ++j)
	{
	  if (LW_NETWORK_INFO[j].active)
	    {
	      dist = distance_between_colors (LW_NETWORK_INFO[j].color, i);
	      if (dist > 0)
		{
		  if (LW_NETWORK_INFO[j].network)
		    {
		      /*
		       * We consider network color conflicts
		       * to be less important than conflicts with
		       * local colors, therefore we increase the
		       * distance for network teams.
		       */
		      dist++;
		    }
		  closeness[i] += LW_NETWORK_COLOR_DIST_SCALE / (dist * dist);
		}
	      else
		{
		  closeness[i] = LW_NETWORK_COLOR_DIST_MAXINT;
		}
	    }
	}
    }

  /*
   * And now we pick up the one that has the less common points
   * with existing colors.
   */
  closeness_min = LW_NETWORK_COLOR_DIST_MAXINT;
  color = 0;
  for (i = 0; i < LW_NETWORK_COLOR_NB; ++i)
    {
      if (closeness[i] < closeness_min)
	{
	  color = i;
	  closeness_min = closeness[i];
	}
    }

  return color;
}

/*------------------------------------------------------------------*/
/*
 * Associates a color to each team 
 */
void
lw_network_attribute_colors ()
{
  int i, color;

  for (i = 0; i < NB_TEAMS; ++i)
    {
      if (LW_NETWORK_INFO[i].active)
	{
	  if (LW_NETWORK_INFO[i].network)
	    {
	      /*
	       * -1 is a temporary value which will be overwritten
	       * by the _real_ value later
	       */
	      LW_NETWORK_INFO[i].color = -1;
	    }
	  else
	    {
	      LW_NETWORK_INFO[i].color = CONFIG_TEAM_COLOR[i];
	    }
	}
    }

  for (i = 0; i < NB_TEAMS; ++i)
    {
      if (LW_NETWORK_INFO[i].active && LW_NETWORK_INFO[i].network)
	{
	  color = choose_different_color ();
	  LW_NETWORK_INFO[i].color = color;
	}
    }
}

/*------------------------------------------------------------------*/
/*
 * Associates a part to each team 
 */
void
lw_network_attribute_parts ()
{
  int i;
  static int order[NB_TEAMS] = { 0, 5, 2, 3, 1, 4 };

  for (i = 0; i < NB_TEAMS; ++i)
    {
      if (LW_NETWORK_INFO[i].active)
	{
	  LW_NETWORK_INFO[i].part = order[LW_NETWORK_INFO[i].server_id];
	}
    }
}