Debian Codegraph @

src/structure/pathstructure.cpp

raw symbols SCIP
1 /***************************************************************************
2 pathstructure.cpp - description
3 -------------------
4 begin : june 17th, 2003
5 copyright : (C) 2003-2007 by Duong Khang NGUYEN
6 email : neoneurone @ gmail com
7
8 $Id: pathstructure.cpp 450 2010-11-21 19:11:43Z neoneurone $
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * any later version. *
17 * *
18 ***************************************************************************/
19
20 // Useful enumerations
21 #include "opencity_direction.h"
22 #include "opencity_structure_type.h"
23
24 // OpenCity header
25 #include "pathstructure.h"
26
27 // Global settings
28 #include "globalvar.h"
29 extern GlobalVar gVars;
30
31
32 /*======================================================================*/
33 PathStructure::PathStructure():
34 Structure(),
35 ubNumberNeighbour( 0 ),
36 ubTraffic( 0 )
37 {
38 OPENCITY_DEBUG( "ctor" );
39 }
40
41
42 /*======================================================================*/
43 PathStructure::PathStructure
44 (
45 const OPENCITY_STRUCTURE_CODE & enumStructCode
46 ):
47 Structure( enumStructCode ),
48 ubNumberNeighbour( 0 ),
49 ubTraffic( 0 )
50 {
51 OPENCITY_DEBUG( "ctor param" );
52
53 _eGC = gVars.gpPropertyMgr->GetGC( enumStructCode );
54 _eType = gVars.gpPropertyMgr->GetST( enumStructCode );
55
56 //debug
57 //cout << "struct: " << (int)enumStructCode
58 // << "/ graphic: " << (int)_eGC << endl;
59
60 }
61
62
63 /*======================================================================*/
64 PathStructure::~PathStructure()
65 {
66 OPENCITY_DEBUG( "dtor" );
67 }
68
69
70 /*======================================================================*/
71 void
72 PathStructure::SaveTo( std::fstream& rfs )
73 {
74 Structure::SaveTo( rfs );
75
76 rfs << (uint)ubNumberNeighbour << std::ends;
77 rfs << (uint)ubTraffic << std::ends;
78 }
79
80
81 /*======================================================================*/
82 void
83 PathStructure::LoadFrom( std::fstream& rfs )
84 {
85 Structure::LoadFrom( rfs );
86 uint temp = 0;
87
88 rfs >> temp; rfs.ignore(); ubNumberNeighbour = (OC_BYTE)temp;
89 rfs >> temp; rfs.ignore(); ubTraffic = (OC_BYTE)temp;
90 }
91
92
93 /*======================================================================*/
94 void
95 PathStructure::AddNeighbour(
96 const PathStructure & rcNeighbour,
97 const OPENCITY_DIRECTION & enumDir )
98 {
99 OPENCITY_GRAPHIC_CODE baseGC = OC_EMPTY;
100 uint newGC;
101
102 //debug cout << "adding 1 neighbour" << endl;
103
104 switch (_eSC) {
105 case OC_STRUCTURE_ROAD:
106 baseGC = OC_ROAD_O_N;
107 break;
108 case OC_STRUCTURE_ELINE:
109 baseGC = OC_ELINE_O_N;
110 break;
111 default:
112 OPENCITY_DEBUG( "Game design ERROR" );
113 break;
114 }
115
116 // We will update the _eGC with the newGC value
117 // when we return, so we keep with this
118 newGC = _eGC;
119
120 if (ubNumberNeighbour == 0) {
121 // The current _eGC is already OC_ROAD_O_N we don't process further in such case
122 if (enumDir == OC_DIR_O_E)
123 newGC = baseGC + 1;
124 else if (enumDir == OC_DIR_O_S)
125 newGC = baseGC + 2;
126 else if (enumDir == OC_DIR_O_W)
127 newGC = baseGC + 3;
128 } else
129
130 /*-----------------------------------------*/
131 if (ubNumberNeighbour == 1) {
132 //debug cout << "number neighbour == 1" << endl;
133 if (enumDir == OC_DIR_O_N) {
134 // We don't process the case OC_ROAD_O_N
135 if (_eGC == baseGC + 1)
136 newGC = baseGC + 6;
137 else if (_eGC == baseGC + 2)
138 newGC = baseGC + 4;
139 else if (_eGC == baseGC + 3)
140 newGC = baseGC + 7;
141 else
142 return;
143 } else
144
145 if (enumDir == OC_DIR_O_E) {
146 // we don't process the case OC_ROAD_O_E
147 if (_eGC == baseGC)
148 newGC = baseGC + 6;
149 else if (_eGC == baseGC + 2)
150 newGC = baseGC + 8;
151 else if (_eGC == baseGC + 3)
152 newGC = baseGC + 5;
153 else
154 return;
155 } else
156
157 if (enumDir == OC_DIR_O_S) {
158 // we don't process the case OC_ROAD_O_S
159 if (_eGC == baseGC)
160 newGC = baseGC + 4;
161 else if (_eGC == baseGC + 1)
162 newGC = baseGC + 8;
163 else if (_eGC == baseGC + 3)
164 newGC = baseGC + 9;
165 else
166 return;
167 } else
168
169 if (enumDir == OC_DIR_O_W) {
170 // we don't process the case OC_ROAD_O_W
171 if (_eGC == baseGC)
172 newGC = baseGC + 7;
173 else if (_eGC == baseGC + 1)
174 newGC = baseGC + 5;
175 else if (_eGC == baseGC + 2)
176 newGC = baseGC + 9;
177 else
178 return;
179 }
180 } else
181
182 //-----------------------------------------
183 if (ubNumberNeighbour == 2) {
184 if (enumDir == OC_DIR_O_N) {
185 // We don't process the case OC_ROAD_S_N, N_E, N_W
186 if (_eGC == baseGC + 8) // S_E
187 newGC = baseGC + 10; // S_N_E
188 else if (_eGC == baseGC + 9) // S_W
189 newGC = baseGC + 12; // S_N_W
190 else if (_eGC == baseGC + 5) // W_E
191 newGC = baseGC + 13; // N_W_E
192 else
193 return;
194 } else
195
196 if (enumDir == OC_DIR_O_E) {
197 // We don't process the case OC_ROAD_W_E, N_E, S_E
198 if (_eGC == baseGC + 9)
199 newGC = baseGC + 11;
200 else if (_eGC == baseGC + 7)
201 newGC = baseGC + 13;
202 else if (_eGC == baseGC + 4)
203 newGC = baseGC + 10;
204 else
205 return;
206 } else
207
208 if (enumDir == OC_DIR_O_S) {
209 // We don't process the case OC_ROAD_S_N, S_E, S_W
210 if (_eGC == baseGC + 6)
211 newGC = baseGC + 10;
212 else if (_eGC == baseGC + 7)
213 newGC = baseGC + 12;
214 else if (_eGC == baseGC + 5)
215 newGC = baseGC + 11;
216 else
217 return;
218 } else
219
220 if (enumDir == OC_DIR_O_W) {
221 // We don't process the case OC_ROAD_W_E, N_W, S_W
222 if (_eGC == baseGC + 8)
223 newGC = baseGC + 11;
224 else if (_eGC == baseGC + 6)
225 newGC = baseGC + 13;
226 else if (_eGC == baseGC + 4)
227 newGC = baseGC + 12;
228 else
229 return;
230 }
231 } else
232
233 //-----------------------------------------
234 if (ubNumberNeighbour == 3) {
235 if (enumDir == OC_DIR_O_N) {
236 if (_eGC == baseGC + 11)
237 newGC = baseGC + 14;
238 else
239 return;
240 } else
241 if (enumDir == OC_DIR_O_E) {
242 if (_eGC == baseGC + 12)
243 newGC = baseGC + 14;
244 else
245 return;
246 } else
247 if (enumDir == OC_DIR_O_S) {
248 if (_eGC == baseGC + 13)
249 newGC = baseGC + 14;
250 else
251 return;
252 } else
253 if (enumDir == OC_DIR_O_W) {
254 if (_eGC == baseGC + 10)
255 newGC = baseGC + 14;
256 else
257 return;
258 }
259 }
260 else {
261 // impossible case !
262 OPENCITY_FATAL( "Internal game design error !" );
263 exit(-1);
264 return;
265 }
266
267 _eGC = (OPENCITY_GRAPHIC_CODE)newGC;
268 ubNumberNeighbour++;
269
270 //debug
271 //cout << "newGC: " << (int) newGC
272 // << " / nNeighbours: " << (int)ubNumberNeighbour << endl;
273 //debug end
274 }
275
276
277 /*=====================================================================*/
278 void
279 PathStructure::RemoveNeighbour(
280 const PathStructure & rcNeighbour,
281 const OPENCITY_DIRECTION & enumDir )
282 {
283 OPENCITY_GRAPHIC_CODE baseGC = OC_EMPTY;
284 uint newGC;
285
286 // note: we do nothing when number of neighbours is 0
287 if (ubNumberNeighbour == 0) {
288 return;
289 }
290
291 switch (_eSC) {
292 case OC_STRUCTURE_ROAD:
293 baseGC = OC_ROAD_O_N;
294 break;
295 case OC_STRUCTURE_ELINE:
296 baseGC = OC_ELINE_O_N;
297 break;
298 default:
299 cout << "WARNING: game design error" << endl;
300 break;
301 }
302
303 // we will update the _eGC with the newGC value
304 // when we return, so we keep with this
305 newGC = _eGC;
306
307 if (ubNumberNeighbour == 1) {
308 // we have no neighbour so the default path structure
309 // look to the NORTH
310 newGC = baseGC;
311 } else
312
313 //-----------------------------------------
314
315 if (ubNumberNeighbour == 2) {
316 //debug cout << "number neighbour == 1" << endl;
317 if (enumDir == OC_DIR_O_N) {
318 if (_eGC == baseGC + 4)
319 newGC = baseGC + 2;
320 else if (_eGC == baseGC + 6)
321 newGC = baseGC + 1;
322 else if (_eGC == baseGC + 7)
323 newGC = baseGC + 3;
324 else
325 return;
326 } else
327
328 if (enumDir == OC_DIR_O_E) {
329 if (_eGC == baseGC + 6)
330 newGC = baseGC;
331 else if (_eGC == baseGC + 8)
332 newGC = baseGC + 2;
333 else if (_eGC == baseGC + 5)
334 newGC = baseGC + 3;
335 else
336 return;
337 } else
338
339 if (enumDir == OC_DIR_O_S) {
340 if (_eGC == baseGC + 4)
341 newGC = baseGC;
342 else if (_eGC == baseGC + 8)
343 newGC = baseGC + 1;
344 else if (_eGC == baseGC + 9)
345 newGC = baseGC + 3;
346 else
347 return;
348 } else
349
350 if (enumDir == OC_DIR_O_W) {
351 if (_eGC == baseGC + 7)
352 newGC = baseGC;
353 else if (_eGC == baseGC + 5)
354 newGC = baseGC + 1;
355 else if (_eGC == baseGC + 9)
356 newGC = baseGC + 2;
357 else
358 return;
359 }
360 } else
361
362 //-----------------------------------------
363 if (ubNumberNeighbour == 3) {
364 if (enumDir == OC_DIR_O_N) {
365 if (_eGC == baseGC + 10)
366 newGC = baseGC + 8;
367 else if (_eGC == baseGC + 12)
368 newGC = baseGC + 9;
369 else if (_eGC == baseGC + 13)
370 newGC = baseGC + 5;
371 else
372 return;
373 } else
374
375 if (enumDir == OC_DIR_O_E) {
376 if (_eGC == baseGC + 11)
377 newGC = baseGC + 9;
378 else if (_eGC == baseGC + 13)
379 newGC = baseGC + 7;
380 else if (_eGC == baseGC + 10)
381 newGC = baseGC + 4;
382 else
383 return;
384 } else
385
386 if (enumDir == OC_DIR_O_S) {
387 if (_eGC == baseGC + 10)
388 newGC = baseGC + 6;
389 else if (_eGC == baseGC + 12)
390 newGC = baseGC + 7;
391 else if (_eGC == baseGC + 11)
392 newGC = baseGC + 5;
393 else
394 return;
395 } else
396
397 if (enumDir == OC_DIR_O_W) {
398 if (_eGC == baseGC + 11)
399 newGC = baseGC + 8;
400 else if (_eGC == baseGC + 13)
401 newGC = baseGC + 6;
402 else if (_eGC == baseGC + 12)
403 newGC = baseGC + 4;
404 else
405 return;
406 }
407 } else
408
409 //-----------------------------------------
410 if (ubNumberNeighbour == 4) {
411 if (enumDir == OC_DIR_O_N) {
412 newGC = baseGC + 11;
413 } else
414 if (enumDir == OC_DIR_O_E) {
415 newGC = baseGC + 12;
416 } else
417 if (enumDir == OC_DIR_O_S) {
418 newGC = baseGC + 13;
419 } else
420 if (enumDir == OC_DIR_O_W) {
421 newGC = baseGC + 10;
422 }
423 }
424
425 _eGC = (OPENCITY_GRAPHIC_CODE)newGC;
426 ubNumberNeighbour--;
427 }
428
429
430 /*======================================================================*/
431 // Old code, kept for future reference
432 /*
433 void
434 pathstructureAddNeighbour2(
435 const PathStructure & rcNeighbour,
436 const OPENCITY_DIRECTION & enumDir )
437 {
438 //debug
439 cout << "adding 1 neighbour" << endl;
440
441 if (ubNumberNeighbour == 0) {
442 // the current _eGC is already OC_ROAD_O_N
443 // we don't process further in such case
444 if (enumDir == OC_DIR_O_E)
445 _eGC = OC_ROAD_O_E;
446 else if (enumDir == OC_DIR_O_S)
447 _eGC = OC_ROAD_O_S;
448 else if (enumDir == OC_DIR_O_W)
449 _eGC = OC_ROAD_O_W;
450 } else
451
452 *-----------------------------------------*
453 if (ubNumberNeighbour == 1) {
454 //debug
455 cout << "number neighbour == 1" << endl;
456 if (enumDir == OC_DIR_O_N) {
457 // we don't process the case OC_ROAD_O_N
458 if (_eGC == OC_ROAD_O_E)
459 _eGC = OC_ROAD_N_E;
460 else if (_eGC == OC_ROAD_O_S)
461 _eGC = OC_ROAD_S_N;
462 else if (_eGC == OC_ROAD_O_W)
463 _eGC = OC_ROAD_N_W;
464 else
465 return;
466 } else
467
468 if (enumDir == OC_DIR_O_E) {
469 // we don't process the case OC_ROAD_O_E
470 if (_eGC == OC_ROAD_O_N)
471 _eGC = OC_ROAD_N_E;
472 else if (_eGC == OC_ROAD_O_S)
473 _eGC = OC_ROAD_S_E;
474 else if (_eGC == OC_ROAD_O_W)
475 _eGC = OC_ROAD_W_E;
476 else
477 return;
478 } else
479
480 if (enumDir == OC_DIR_O_S) {
481 // we don't process the case OC_ROAD_O_S
482 if (_eGC == OC_ROAD_O_N)
483 _eGC = OC_ROAD_S_N;
484 else if (_eGC == OC_ROAD_O_E)
485 _eGC = OC_ROAD_S_E;
486 else if (_eGC == OC_ROAD_O_W)
487 _eGC = OC_ROAD_S_W;
488 else
489 return;
490 } else
491
492 if (enumDir == OC_DIR_O_W) {
493 // we don't process the case OC_ROAD_O_W
494 if (_eGC == OC_ROAD_O_N)
495 _eGC = OC_ROAD_N_W;
496 else if (_eGC == OC_ROAD_O_E)
497 _eGC = OC_ROAD_W_E;
498 else if (_eGC == OC_ROAD_O_S)
499 _eGC = OC_ROAD_S_W;
500 else
501 return;
502 }
503 } else
504
505 *-----------------------------------------*
506 if (ubNumberNeighbour == 2) {
507 if (enumDir == OC_DIR_O_N) {
508 // we don't process the case OC_ROAD_S_N, N_E, N_W
509 if (_eGC == OC_ROAD_S_E)
510 _eGC = OC_ROAD_S_N_E;
511 else if (_eGC == OC_ROAD_S_W)
512 _eGC = OC_ROAD_S_W_E;
513 else if (_eGC == OC_ROAD_W_E)
514 _eGC = OC_ROAD_N_W_E;
515 else
516 return;
517 } else
518
519 if (enumDir == OC_DIR_O_E) {
520 // we don't process the case OC_ROAD_W_E, N_E, S_E
521 if (_eGC == OC_ROAD_S_W)
522 _eGC = OC_ROAD_S_W_E;
523 else if (_eGC == OC_ROAD_N_W)
524 _eGC = OC_ROAD_N_W_E;
525 else if (_eGC == OC_ROAD_S_N)
526 _eGC = OC_ROAD_S_N_E;
527 else
528 return;
529 } else
530
531 if (enumDir == OC_DIR_O_S) {
532 // we don't process the case OC_ROAD_S_N, S_E, S_W
533 if (_eGC == OC_ROAD_N_E)
534 _eGC = OC_ROAD_S_N_E;
535 else if (_eGC == OC_ROAD_N_W)
536 _eGC = OC_ROAD_S_N_W;
537 else if (_eGC == OC_ROAD_W_E)
538 _eGC = OC_ROAD_S_W_E;
539 else
540 return;
541 } else
542
543 if (enumDir == OC_DIR_O_W) {
544 // we don't process the case OC_ROAD_W_E, N_W, S_W
545 if (_eGC == OC_ROAD_S_E)
546 _eGC = OC_ROAD_S_W_E;
547 else if (_eGC == OC_ROAD_N_E)
548 _eGC = OC_ROAD_N_W_E;
549 else if (_eGC == OC_ROAD_S_N)
550 _eGC = OC_ROAD_S_N_W;
551 else
552 return;
553 }
554 } else
555
556 *-----------------------------------------*
557 if (ubNumberNeighbour == 3) {
558 if (enumDir == OC_DIR_O_N) {
559 if (_eGC == OC_ROAD_S_W_E)
560 _eGC = OC_ROAD_S_N_W_E;
561 else
562 return;
563 } else
564 if (enumDir == OC_DIR_O_E) {
565 if (_eGC == OC_ROAD_S_N_W)
566 _eGC = OC_ROAD_S_N_W_E;
567 else
568 return;
569 } else
570 if (enumDir == OC_DIR_O_S) {
571 if (_eGC == OC_ROAD_N_W_E)
572 _eGC = OC_ROAD_S_N_W_E;
573 else
574 return;
575 } else
576 if (enumDir == OC_DIR_O_W) {
577 if (_eGC == OC_ROAD_S_N_E)
578 _eGC = OC_ROAD_S_N_W_E;
579 else
580 return;
581 }
582 }
583 else {
584 // impossible case !
585 cout << "WARNING: internal game design error !" << endl;
586 return;
587 }
588
589 ubNumberNeighbour++;
590 }
591 */
592
593
594 /*======================================================================*/
595 const OC_BYTE &
596 PathStructure::GetTraffic() const
597 {
598 return this->ubTraffic;
599 }
600
601
602 /*======================================================================*/
603 void
604 PathStructure::SetTraffic(
605 const OC_BYTE & rubTraffic )
606 {
607 this->ubTraffic = rubTraffic;
608 }
609
610
611 /*======================================================================*/
612 const uint &
613 PathStructure::GetLength() const
614 {
615 return this->uiLength;
616 }
617
618
619 /*======================================================================*/
620 void
621 PathStructure::SetLength(
622 const uint & rcuiValue )
623 {
624 this->uiLength = rcuiValue;
625 }