1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | typedef struct |
---|
6 | { |
---|
7 | int alliance; |
---|
8 | double inf; |
---|
9 | struct influence *next; |
---|
10 | } influence; |
---|
11 | |
---|
12 | typedef struct |
---|
13 | { |
---|
14 | int x; |
---|
15 | struct influence *inf; |
---|
16 | struct System *next; |
---|
17 | } System; |
---|
18 | |
---|
19 | typedef struct |
---|
20 | { |
---|
21 | signed int y; |
---|
22 | struct System *fields; |
---|
23 | struct row *next; |
---|
24 | } row; |
---|
25 | |
---|
26 | typedef System *SystemPointer; |
---|
27 | typedef influence *influencePointer; |
---|
28 | typedef row *rowPointer; |
---|
29 | |
---|
30 | System *sCur; |
---|
31 | System *sTop; |
---|
32 | System *sFirst; |
---|
33 | row *bFirst; |
---|
34 | row *rFirst; |
---|
35 | row *rCur; |
---|
36 | |
---|
37 | influencePointer iLast; |
---|
38 | influencePointer iCur; |
---|
39 | |
---|
40 | int pow_10(int base, int exp) |
---|
41 | { |
---|
42 | int result, i; |
---|
43 | result = base; |
---|
44 | exp--; |
---|
45 | for (i=0; i<exp; i++) |
---|
46 | { |
---|
47 | result *= base; |
---|
48 | } |
---|
49 | return result; |
---|
50 | } |
---|
51 | |
---|
52 | double charToFloat(char *string) |
---|
53 | { |
---|
54 | float result; |
---|
55 | int i, fCount, fVar, sLen, fract; |
---|
56 | |
---|
57 | result = atoi(string); |
---|
58 | |
---|
59 | fVar = 0; |
---|
60 | fCount = 0; |
---|
61 | sLen = strlen(string); |
---|
62 | for (i=0; i<sLen; i++) |
---|
63 | { |
---|
64 | if (string[i] == '.') |
---|
65 | break; |
---|
66 | } |
---|
67 | i++; |
---|
68 | fract = atoi(&string[i]); |
---|
69 | |
---|
70 | if (fract != 0) |
---|
71 | { |
---|
72 | fCount = strlen(&string[i]); |
---|
73 | result += (double)fract/pow_10(10, fCount); |
---|
74 | //printf("[%d^%d -> %d]\n", fract, fCount, pow(10, fCount)); |
---|
75 | } |
---|
76 | //printf("<> %f %d %s\n", result, fVar, string); |
---|
77 | |
---|
78 | return result; |
---|
79 | } |
---|
80 | |
---|
81 | int main() |
---|
82 | { |
---|
83 | FILE *f; |
---|
84 | char s[1000]; |
---|
85 | char *pch; |
---|
86 | SystemPointer sp; |
---|
87 | influencePointer ip; |
---|
88 | rowPointer rp; |
---|
89 | int sH, sW, sS, sA, pC; |
---|
90 | int tempy, sR, i; |
---|
91 | double sV; |
---|
92 | |
---|
93 | pC = 0; |
---|
94 | |
---|
95 | f = fopen("evemap_systems.data", "r"); |
---|
96 | if (!f) |
---|
97 | return 1; |
---|
98 | |
---|
99 | // fwrite($fp, 'c '.$opt['height'].' '.$opt['width'].' '.$opt['maxrange'].' '.$opt['sensitivity'].' '.$opt['validInf']."\r\n"); |
---|
100 | fgets(s, 1000, f); |
---|
101 | |
---|
102 | pch = (char*)strtok (s, " "); |
---|
103 | pch = (char*)strtok (NULL, " "); |
---|
104 | sH = atoi(pch); |
---|
105 | pch = (char*)strtok (NULL, " "); |
---|
106 | sW = atoi(pch); |
---|
107 | pch = (char*)strtok (NULL, " "); |
---|
108 | sR = atoi(pch); |
---|
109 | pch = (char*)strtok (NULL, " "); |
---|
110 | sS = atoi(pch); |
---|
111 | pch = (char*)strtok (NULL, " "); |
---|
112 | sV = charToFloat(pch); |
---|
113 | pch = (char*)strtok (NULL, " "); |
---|
114 | sA = atoi(pch); |
---|
115 | |
---|
116 | while (fgets(s, 1000, f) != NULL) |
---|
117 | { |
---|
118 | pch = (char*)strtok (s, " "); |
---|
119 | while (pch != NULL) |
---|
120 | { |
---|
121 | if (strcmp("s", pch) == 0) |
---|
122 | { |
---|
123 | sp = (System *)malloc(sizeof(System)); pC++; |
---|
124 | pch = (char*)strtok (NULL, " "); |
---|
125 | sp->x = atoi(pch); |
---|
126 | pch = (char*)strtok (NULL, " "); |
---|
127 | tempy = atoi(pch); |
---|
128 | |
---|
129 | // create the first row |
---|
130 | if (rCur == NULL) |
---|
131 | { |
---|
132 | rp = (row *)malloc(sizeof(row)); pC++; |
---|
133 | rp->y = tempy; |
---|
134 | rCur = rp; |
---|
135 | rFirst = rp; |
---|
136 | rCur->fields = (struct System *)sp; |
---|
137 | sFirst = sp; |
---|
138 | } |
---|
139 | // create another row, we got more y! |
---|
140 | if (rCur->y != tempy) |
---|
141 | { |
---|
142 | rp = (row *)malloc(sizeof(row)); pC++; |
---|
143 | rp->y = tempy; |
---|
144 | rCur->next = (struct row*)rp; |
---|
145 | rCur = (row*)rCur->next; |
---|
146 | rCur->fields = (struct System *)sp; |
---|
147 | sFirst = sp; |
---|
148 | sTop = NULL; |
---|
149 | } |
---|
150 | //printf ("%d %d\n", sp->x, sp->y); |
---|
151 | /*if (sFirst == NULL) |
---|
152 | { |
---|
153 | sFirst = sp; |
---|
154 | } */ |
---|
155 | if (sTop != NULL) |
---|
156 | { |
---|
157 | sTop->next = (struct System*)sp; |
---|
158 | } |
---|
159 | sTop = sp; |
---|
160 | iLast = NULL; |
---|
161 | } |
---|
162 | if (strcmp("i", pch) == 0) |
---|
163 | { |
---|
164 | ip = (influencePointer)malloc(sizeof(influence)); pC++; |
---|
165 | pch = (char*)strtok (NULL, " "); |
---|
166 | ip->alliance = atoi(pch); |
---|
167 | pch = (char*)strtok (NULL, " "); |
---|
168 | ip->inf = charToFloat(pch); |
---|
169 | //printf ("%d %.2lf", ip->alliance, ip->inf); |
---|
170 | if (iLast != NULL) |
---|
171 | { |
---|
172 | iLast->next = (struct influence*)ip; |
---|
173 | iLast = ip; |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | sTop->inf = (struct influence*)ip; |
---|
178 | iLast = (influence*)sTop->inf; |
---|
179 | } |
---|
180 | } |
---|
181 | pch = (char*)strtok (NULL, " "); |
---|
182 | } |
---|
183 | } |
---|
184 | fclose(f); |
---|
185 | |
---|
186 | int range, dist; |
---|
187 | int dx, dy, bestA, y, x; |
---|
188 | range = sR*sR; |
---|
189 | |
---|
190 | float influence[sA], bestI; |
---|
191 | for (i = 0; i < sA; i++) |
---|
192 | { |
---|
193 | influence[i] = 0; |
---|
194 | } |
---|
195 | bFirst = rFirst; |
---|
196 | |
---|
197 | for (y = 0; y < sH; y++) |
---|
198 | { |
---|
199 | // iterate to the first needed item |
---|
200 | rCur = bFirst; |
---|
201 | while (rCur != NULL) |
---|
202 | { |
---|
203 | if (y - rCur->y <= sR) |
---|
204 | { |
---|
205 | bFirst = rCur; |
---|
206 | break; |
---|
207 | } |
---|
208 | rCur = (row*)rCur->next; |
---|
209 | } |
---|
210 | //printf("line: %d, first y: %d\n", y, rFirst->y); |
---|
211 | |
---|
212 | for (x = 0; x < sW; x++) |
---|
213 | { |
---|
214 | //printf("pixel: %d,%d\n", x, y); |
---|
215 | // iterate through all rows |
---|
216 | rCur = bFirst; |
---|
217 | while (rCur != NULL) |
---|
218 | { |
---|
219 | if (rCur->y - y > sR) |
---|
220 | { |
---|
221 | break; |
---|
222 | } |
---|
223 | |
---|
224 | sCur = (System*)rCur->fields; |
---|
225 | while (sCur != NULL) |
---|
226 | { |
---|
227 | if (x - sCur->x > sR) |
---|
228 | { |
---|
229 | //printf("skipping: %d,%d - %d,%d\n", x, y, sCur->x, rCur->y); |
---|
230 | sCur = (System*)sCur->next; |
---|
231 | continue; |
---|
232 | } |
---|
233 | //printf("noskip: %d,%d - %d,%d\n", x, y, sCur->x, rCur->y); |
---|
234 | if (sCur->x - x > sR) |
---|
235 | { |
---|
236 | break; |
---|
237 | } |
---|
238 | dy = y - rCur->y; |
---|
239 | dx = x - sCur->x; |
---|
240 | dist = dx*dx + dy*dy; |
---|
241 | |
---|
242 | if (dist > range) |
---|
243 | { |
---|
244 | sCur = (System*)sCur->next; |
---|
245 | continue; |
---|
246 | } |
---|
247 | |
---|
248 | iCur = (influencePointer)sCur->inf; |
---|
249 | while (iCur != NULL) |
---|
250 | { |
---|
251 | influence[iCur->alliance] += iCur->inf/(sS+dist); |
---|
252 | iCur = (influencePointer)iCur->next; |
---|
253 | } |
---|
254 | sCur = (System*)sCur->next; |
---|
255 | } |
---|
256 | rCur = (row*)rCur->next; |
---|
257 | } |
---|
258 | |
---|
259 | bestI = 0; |
---|
260 | bestA = -1; |
---|
261 | for (i = 0; i < sA; i++) |
---|
262 | { |
---|
263 | if (influence[i] > bestI) |
---|
264 | { |
---|
265 | bestI = influence[i]; |
---|
266 | bestA = i; |
---|
267 | } |
---|
268 | influence[i] = 0; |
---|
269 | } |
---|
270 | if (bestI > sV) |
---|
271 | { |
---|
272 | printf("%d,%d,%d,%f\n", x, y, bestA, bestI); |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | // free up all used memory |
---|
278 | // iterate through the linked lists and free the element we processed |
---|
279 | |
---|
280 | int pointer; |
---|
281 | rCur = rFirst; |
---|
282 | while (rCur != NULL) |
---|
283 | { |
---|
284 | sCur = (System*)rCur->fields; |
---|
285 | while (sCur != NULL) |
---|
286 | { |
---|
287 | iCur = (influencePointer)sCur->inf; |
---|
288 | while (iCur != NULL) |
---|
289 | { |
---|
290 | pointer = (int)iCur->next; |
---|
291 | free(iCur); |
---|
292 | iCur = (influencePointer)pointer; |
---|
293 | } |
---|
294 | pointer = (int)sCur->next; |
---|
295 | free(sCur); |
---|
296 | sCur = (System*)pointer; |
---|
297 | } |
---|
298 | pointer = (int)rCur->next; |
---|
299 | free(rCur); |
---|
300 | rCur = (row*)pointer; |
---|
301 | } |
---|
302 | |
---|
303 | return 0; |
---|
304 | } |
---|