Debian Codegraph @

src/buildinglayer.cpp

raw symbols SCIP
1 /***************************************************************************
2 buildinglayer.cpp - description
3 -------------------
4 begin : september 20th, 2003
5 copyright : (C) 2003-2008 by Duong Khang NGUYEN
6 email : neoneurone @ gmail com
7
8 $Id: buildinglayer.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 "buildinglayer.h"
26 #include "city.h"
27 #include "rcistructure.h"
28 #include "wegstructure.h"
29 #include "pathstructure.h"
30 #include "treestructure.h"
31 #include "guicontainer.h"
32 #include "guibutton.h"
33
34 // Global settings
35 #include "globalvar.h"
36 extern GlobalVar gVars;
37
38
39 /*=====================================================================*/
40 BuildingLayer::BuildingLayer
41 (
42 const City & rcCity
43 )
44 {
45 OPENCITY_DEBUG( "ctor" );
46 // initialization of width and height
47 rcCity.GetWL( _uiLayerWidth, _uiLayerLength );
48
49 // initialization of the table of pointer of Structure
50 uint citySurface = _uiLayerLength * _uiLayerWidth;
51 _tabpStructure = new Structure*[citySurface];
52 for ( uint counter = 0; counter < citySurface; counter++ ) {
53 _tabpStructure[ counter ] = NULL;
54 }
55
56 }
57
58
59 /*=====================================================================*/
60 BuildingLayer::~BuildingLayer()
61 {
62 OPENCITY_DEBUG("dtor");
63
64 uint citySurface = _uiLayerLength * _uiLayerWidth;
65 for ( uint counter = 0; counter < citySurface; counter++ ) {
66 // delete seems to have no effects on NULL
67 if (_tabpStructure[counter] != NULL) {
68 delete _tabpStructure[counter];
69 _tabpStructure[counter] = NULL;
70 }
71 }
72
73 delete [] _tabpStructure;
74 _tabpStructure = NULL;
75 }
76
77
78 /*=====================================================================*/
79 void
80 BuildingLayer::SaveTo( std::fstream& rfs )
81 {
82 OPENCITY_DEBUG( __PRETTY_FUNCTION__ << "saving" );
83 Structure* p = NULL;
84 uint w = 0, l = 0, linear = 0;
85
86 rfs << _uiLayerWidth << std::ends;
87 rfs << _uiLayerLength << std::ends;
88
89 for ( l = 0; l < _uiLayerLength; l++ )
90 for ( w = 0; w < _uiLayerWidth; w++ ) {
91 p = _tabpStructure[linear++];
92
93 // IF this is a part of a bigger structure THEN
94 if ((p != NULL) && (p->GetMain() != NULL)) {
95 p = NULL;
96 }
97
98 // IF there is structure's information to save THEN
99 if (p != NULL) {
100 rfs << 1 << std::ends;
101 rfs << p->GetType() << std::ends;
102 p->SaveTo( rfs );
103 }
104 else {
105 rfs << 0 << std::ends;
106 }
107 } // for
108 }
109
110
111 /*=====================================================================*/
112 void
113 BuildingLayer::LoadFrom( std::fstream& rfs )
114 {
115 OPENCITY_DEBUG( __PRETTY_FUNCTION__ << "loading" );
116 Structure* p = NULL;
117 uint extra = 0; // Extra info ? 1 = true; 0 = false;
118 uint w = 0, l = 0, anUint = 0, linear = 0;
119 OPENCITY_STRUCTURE_TYPE type = OC_TYPE_UNDEFINED; ///< Structure's object type
120
121 // Remove the old structures
122 uint citySurface = _uiLayerLength * _uiLayerWidth;
123 for ( linear = 0; linear < citySurface; linear++ ) {
124 delete _tabpStructure[linear];
125 }
126 delete [] _tabpStructure;
127
128 // Reset the counter
129 Structure::SetNumber( 0 );
130
131 // Read the saved size
132 rfs >> _uiLayerWidth; rfs.ignore();
133 rfs >> _uiLayerLength; rfs.ignore();
134
135 // initialization of the table of pointer of Structure
136 citySurface = _uiLayerLength * _uiLayerWidth;
137 _tabpStructure = new Structure*[citySurface];
138 for ( linear = 0; linear < citySurface; linear++ ) {
139 _tabpStructure[linear] = NULL;
140 }
141
142 // Load the structures
143 for ( l = 0; l < _uiLayerLength; l++ )
144 for ( w = 0; w < _uiLayerWidth; w++ ) {
145 rfs >> extra; rfs.ignore();
146
147 // IF there is no extra information to read THEN continue
148 if (extra == 0)
149 continue;
150
151 rfs >> anUint; rfs.ignore(); type = (OPENCITY_STRUCTURE_TYPE)anUint;
152 switch (type) {
153 case OC_TYPE_RESIDENCE:
154 case OC_TYPE_COMMERCE:
155 case OC_TYPE_INDUSTRY:
156 p = new RCIStructure();
157 break;
158
159 case OC_TYPE_WATER:
160 case OC_TYPE_ELECTRICITY:
161 case OC_TYPE_GAS:
162 case OC_TYPE_GOVERNMENT: // hack
163 p = new WEGStructure();
164 break;
165
166 case OC_TYPE_PATH:
167 p = new PathStructure();
168 break;
169
170 case OC_TYPE_TREE:
171 p = new TreeStructure();
172 break;
173
174 default:
175 OPENCITY_DEBUG( "Unknown structure's type: " << type );
176 assert( 0 );
177 }
178
179 p->LoadFrom( rfs );
180 _LoadStructure( w, l, p );
181 } // for
182 }
183
184
185 /*=====================================================================*/
186 const bool
187 BuildingLayer::IsConstructive(
188 uint W1, uint L1,
189 uint W2, uint L2,
190 const OPENCITY_STRUCTURE_CODE & enumStructCode ) const
191 {
192 uint linearIndex;
193 uint w;
194
195 // Out of map ?
196 if ((L2 >= _uiLayerLength) || (W2 >= _uiLayerWidth))
197 return false;
198
199 // cheking for the availability of a free room
200 while (L1 <= L2) {
201 linearIndex = (L1*_uiLayerWidth) + W1;
202 w = W1;
203 while (w <= W2) {
204 switch (enumStructCode) {
205 case OC_STRUCTURE_RES:
206 case OC_STRUCTURE_IND:
207 case OC_STRUCTURE_COM:
208 case OC_STRUCTURE_PARK:
209 case OC_STRUCTURE_FLORA:
210 case OC_STRUCTURE_EPLANT_COAL:
211 case OC_STRUCTURE_EPLANT_NUCLEAR:
212 case OC_STRUCTURE_FIREDEPT:
213 case OC_STRUCTURE_POLICEDEPT:
214 case OC_STRUCTURE_HOSPITALDEPT:
215 case OC_STRUCTURE_EDUCATIONDEPT:
216 case OC_STRUCTURE_TEST:
217 // IF there's a structure on the square
218 // OR the square is not plane
219 // then the square is not constructive
220 if ((this->GetLinearStructure(linearIndex) != NULL)
221 ||(gVars.gpMapMgr->IsSquarePlane(w, L1) != true ))
222 return false;
223 break;
224
225 case OC_STRUCTURE_ROAD:
226 case OC_STRUCTURE_ELINE:
227 // The case of "road structure"
228 // OR "eline" is
229 // processed in a seperate method
230 if (_IsPathConstructive(w, L1, enumStructCode) != true)
231 return false;
232 break;
233
234 default:
235 OPENCITY_DEBUG( "Unknown structure" );
236 assert(0);
237 break;
238
239 } // switch
240 w++;
241 linearIndex++;
242 } // while w
243 L1++;
244 }
245
246 return true;
247 }
248
249
250 /*=====================================================================*/
251 const OPENCITY_ERR_CODE
252 BuildingLayer::BuildPreview(
253 const uint & W,
254 const uint & L,
255 const OPENCITY_STRUCTURE_CODE & enumStructCode,
256 OPENCITY_GRAPHIC_CODE & enumGraphicCode ) const
257 {
258 // OPENCITY_DEBUG( "I'm previewing a building" );
259 static uint W1, L1, W2, L2;
260 static uint sw, sl, sh; // Structure's width, length and height
261
262
263 // FIXME: Work for WEG structure only !
264 // Get the graphic code of the structure
265 enumGraphicCode = gVars.gpPropertyMgr->GetGC( enumStructCode );
266 if ( enumGraphicCode == OC_EMPTY ) {
267 OPENCITY_DEBUG( "WARNING: not implemented" );
268 assert(0);
269 }
270
271 // Get the corresponding WLH and calculate the range
272 // An coal electric plant is 4x4 size by default
273 gVars.gpPropertyMgr->GetWLH( enumGraphicCode, sw, 4, sl, 4, sh, 1 );
274 W1 = W;
275 L1 = L;
276 W2 = W + sw - 1;
277 L2 = L + sl - 1;
278
279 // Isn't there enough space ?
280 // OR is there already something on the surface ?
281 if (IsConstructive(W1, L1, W2, L2, enumStructCode) == false) {
282 return OC_ERR_SOMETHING;
283 }
284
285 return OC_ERR_FREE;
286 }
287
288
289 /*=====================================================================*/
290 const OPENCITY_ERR_CODE
291 BuildingLayer::BuildStructure(
292 const uint & W1,
293 const uint & L1,
294 const uint & W2,
295 const uint & L2,
296 const OPENCITY_STRUCTURE_CODE & enumStructCode,
297 uint& rCost )
298 {
299 // OPENCITY_DEBUG( "I'm building some structures" );
300
301 // For more obvious coding
302 OPENCITY_ERR_CODE errCode;
303
304 switch (enumStructCode) {
305 case OC_STRUCTURE_RES:
306 case OC_STRUCTURE_COM:
307 case OC_STRUCTURE_IND:
308 case OC_STRUCTURE_PARK:
309 case OC_STRUCTURE_TEST:
310 errCode = _BuildRCIStructure( W1, L1, W2, L2, enumStructCode, rCost );
311 break;
312
313 case OC_STRUCTURE_EDUCATIONDEPT:
314 case OC_STRUCTURE_FIREDEPT:
315 case OC_STRUCTURE_POLICEDEPT:
316 case OC_STRUCTURE_HOSPITALDEPT:
317 case OC_STRUCTURE_EPLANT_COAL:
318 case OC_STRUCTURE_EPLANT_NUCLEAR:
319 errCode = _BuildWEGStructure( W1, L1, enumStructCode, rCost );
320 break;
321
322 case OC_STRUCTURE_ROAD:
323 case OC_STRUCTURE_ELINE:
324 errCode = _BuildPathStructure( W1, L1, W2, L2, enumStructCode, rCost );
325 break;
326
327 case OC_STRUCTURE_FLORA:
328 errCode = _BuildFloraStructure( W1, L1, W2, L2, enumStructCode, rCost );
329 break;
330
331 default:
332 OPENCITY_DEBUG( "WARNING: Unknown structure to build" );
333 errCode = OC_ERR_SOMETHING;
334 assert(0);
335 break;
336 } // switch
337
338 return errCode;
339 }
340
341
342 /*=====================================================================*/
343 const bool
344 BuildingLayer::GetMainWL
345 (
346 const Structure* const pstruct,
347 uint & w,
348 uint & l
349 ) const
350 {
351 uint sw = 0, sl = 0, sh = 0;
352 uint w1 = w, l1 = l, w2 = w, l2 = l;
353 uint linearIndex = 0;
354 const Structure* pMain = NULL;
355
356 assert( pstruct != NULL );
357 pMain = (pstruct->GetMain() != NULL) ? pstruct->GetMain() : pstruct;
358
359 // Calculate the possible area to limit cpu usage
360 gVars.gpPropertyMgr->GetWLH( pMain->GetGraphicCode(), sw, 0, sl, 0, sh, 0 );
361 assert( (sw != 0) && (sl != 0) );
362 sw--; sl--; // We calculate the range
363
364 gVars.gpMapMgr->GetPossibleWH( w1, l1, -sw, -sl );
365 gVars.gpMapMgr->GetPossibleWH( w2, l2, sw, sl );
366
367 // Now search for all STRUCTURE_PART and the main structure, destroy em
368 for ( l = l1; l <= l2; l++ ) {
369 linearIndex = l*_uiLayerWidth + w1;
370 for ( w = w1; w <= w2; w++ ) {
371 // WARNING: pointer comparaison !
372 if ( pMain == _tabpStructure[ linearIndex ] )
373 return true;
374 linearIndex++;
375 }
376 }
377
378 return false;
379 }
380
381
382 /*=====================================================================*/
383 const OPENCITY_ERR_CODE
384 BuildingLayer::ResizeStructure(
385 const uint & w,
386 const uint & l,
387 const OPENCITY_GRAPHIC_CODE & oldGC )
388 {
389 uint linearIndex = (l*_uiLayerWidth) + w;
390 Structure* pStruct = NULL, * pTemp = NULL, * pMain = NULL;
391 uint ow = 0, ol = 0, oh = 0; // Old structure dimensions
392 uint nw = 0, nl = 0, nh = 0; // New structure dimensions
393 uint xw = 0, xl = 0, xh = 0; // Destroyed structure dimensions
394 uint sxw = 0, sxl = 0; // Destroyed structure coordinates
395 uint dw, dl; // Used in the "for loop"
396 uint uiCost; // Temp cost, necessary to call internal methods
397
398 OPENCITY_STRUCTURE_CODE structCode;
399 OPENCITY_STRUCTURE_CODE destroyedSC; // The destroyed structure's code. It's used for rebuild
400
401 // Get the pointer to the structure that we need to modify
402 pStruct = _tabpStructure[ linearIndex ];
403 assert( pStruct != NULL );
404
405 // IF the graphic code has not changed THEN it's okay
406 if ( oldGC == pStruct->GetGraphicCode() )
407 return OC_ERR_FREE;
408
409 // OPENCITY_DEBUG( "Begin - W/L " << w << "/" << l );
410 structCode = pStruct->GetCode();
411
412 // Get the old WLH
413 gVars.gpPropertyMgr->GetWLH(
414 oldGC,
415 ow, 0,
416 ol, 0,
417 oh, 0 );
418 assert((ow != 0) && (ol != 0)); // not used yet: && (oh != 0)
419
420 // Get the new WLH
421 gVars.gpPropertyMgr->GetWLH(
422 pStruct->GetGraphicCode(),
423 nw, 0,
424 nl, 0,
425 nh, 0 );
426 assert((nw != 0) && (nl != 0)); // not used yet: && (nh != 0)
427
428 // Remove all existing marks on the old surface used by the structure
429 for (dl = l; dl < l + ol; dl++) {
430 linearIndex = (dl*_uiLayerWidth) + w;
431 for (dw = w; dw < w + ow; dw++) {
432 pTemp = _tabpStructure[ linearIndex++ ];
433 // NOTE: the following "if" is unnecessary, since pTemp is never NULL
434 assert( pTemp != NULL );
435 if (pTemp != NULL)
436 pTemp->Unset( OC_STRUCTURE_MARK );
437 }
438 }
439
440 // Build the eventual missing parts of the new structure
441 for (dl = l; dl < l + nl; dl++) {
442 linearIndex = (dl*_uiLayerWidth) + w;
443 for (dw = w; dw < w + nw; dw++) {
444 pTemp = _tabpStructure[ linearIndex ];
445
446 // IF there is nothing here, THEN build a new part
447 if (pTemp == NULL) {
448 pTemp = new RCIStructure( OC_STRUCTURE_PART, pStruct );
449 _tabpStructure[ linearIndex ] = pTemp;
450 }
451 // ELSE IF there is something
452 // AND it's not the current structure
453 // AND it's not the current structure's part neither THEN remove it
454 else if (pTemp != pStruct and pTemp->GetMain() != pStruct) {
455 // Remember the structure which is going to be destroyed
456 sxw = dw; sxl = dl;
457 if (GetMainWL(pTemp, sxw, sxl) == false) {
458 assert(0);
459 }
460 if (pTemp->GetCode() == OC_STRUCTURE_PART)
461 pMain = pTemp->GetMain();
462 else
463 pMain = pTemp;
464
465 // Save the structure code and its main coordinates
466 destroyedSC = pMain->GetCode();
467 gVars.gpPropertyMgr->GetWLH( pMain->GetGraphicCode(), xw, 0, xl, 0, xh, 0 );
468
469 // OPENCITY_DEBUG( "Delete structure - W/L " << dw << "/" << dl );
470 _DestroyStructure( dw, dl, uiCost );
471 pTemp = new RCIStructure( OC_STRUCTURE_PART, pStruct );
472 _tabpStructure[ linearIndex ] = pTemp;
473
474 // Rebuild the neighbourhood
475 if (xw > 1 or xl > 1) {
476 BuildStructure( sxw, sxl, dw + xw-1, dl + xl-1, destroyedSC, uiCost );
477 }
478 }
479 pTemp->Set( OC_STRUCTURE_MARK );
480 linearIndex++;
481 }
482 }
483
484 // Remove the unmarked structures on the old surface
485 // then build the first level structures on it
486 for (dl = l; dl < l + ol; dl++) {
487 linearIndex = (dl*_uiLayerWidth) + w;
488 for (dw = w; dw < w + ow; dw++) {
489 // OPENCITY_DEBUG( "Delete unmarked structure - W/L " << dw << "/" << dl );
490 pTemp = _tabpStructure[ linearIndex ];
491 // debug
492 assert( pTemp != NULL );
493 if (pTemp->IsSet(OC_STRUCTURE_MARK) == false) {
494 // We don't use this _DestroyStructure( dw, dl, uiCost ) because it will
495 // get the structure's size and delete the whole structure
496 // that we're working on
497 delete _tabpStructure[ linearIndex ];
498 _tabpStructure[ linearIndex ] = new RCIStructure( structCode );
499 }
500 linearIndex++;
501 } // for dw
502 } // for dl
503
504 // OPENCITY_DEBUG( "End - W/L " << w << "/" << l );
505
506 return OC_ERR_FREE;
507 }
508
509
510 /*=====================================================================*/
511 const OPENCITY_ERR_CODE
512 BuildingLayer::DestroyStructure(
513 uint W1, uint L1,
514 uint W2, uint L2,
515 uint& rCost )
516 {
517 uint w, l;
518 uint costPerSquare;
519
520 OPENCITY_SWAP( W1, W2, uint );
521 OPENCITY_SWAP( L1, L2, uint );
522 rCost = 0;
523
524 for (w = W1; w <= W2; w++)
525 for (l = L1; l <= L2; l++) {
526 if (_DestroyStructure( w, l, costPerSquare ) == OC_ERR_FREE)
527 rCost += costPerSquare;
528 }
529
530 if (rCost > 0)
531 return OC_ERR_FREE;
532 else
533 return OC_ERR_SOMETHING;
534 }
535
536
537 /*=====================================================================*/
538 Structure *
539 BuildingLayer::GetLinearStructure( const uint cuiLinearIndex ) const
540 {
541 return _tabpStructure[ cuiLinearIndex ];
542 }
543
544
545 /*=====================================================================*/
546 Structure*
547 BuildingLayer::GetStructure(
548 const uint & w,
549 const uint & h ) const
550 {
551 return _tabpStructure[ h*_uiLayerWidth + w ];
552 }
553
554
555 /*=====================================================================*/
556 Structure*
557 BuildingLayer::GetRandomStructure(
558 uint & w,
559 uint & h,
560 const OPENCITY_STRUCTURE_CODE & enumStructCode ) const
561 {
562 if (enumStructCode == OC_STRUCTURE_UNDEFINED)
563 return NULL;
564
565 // Nothing can be static because we can be called by more than 1 thread
566 uint linear;
567 uint counter;
568 Structure* pstruct;
569
570 // We try 100 times before giving up
571 // OC_CHANCE_COUNTER_MAX = 100
572 counter = 0;
573 do {
574 linear = rand() % (_uiLayerWidth*_uiLayerLength);
575 pstruct = GetLinearStructure(linear);
576 if (pstruct != NULL)
577 if ((pstruct->GetCode() == enumStructCode)
578 || (enumStructCode == OC_STRUCTURE_ANY)) {
579 h = linear / _uiLayerWidth;
580 w = linear % _uiLayerWidth;
581 return pstruct;
582 }
583
584 counter++;
585 } while (counter < OC_CHANCE_COUNTER_MAX);
586
587 return NULL;
588 }
589
590
591 /*=====================================================================*/
592 const uint
593 BuildingLayer::GetNumberStructure() const
594 {
595 cout << "WARNING: not implemented" << endl;
596 assert(0);
597
598 return 0;
599 }
600
601
602 /*=====================================================================*/
603 void
604 BuildingLayer::StructureSet(
605 const OC_BYTE & status )
606 {
607 uint citySurface = _uiLayerLength * _uiLayerWidth;
608 for ( uint counter = 0; counter < citySurface; counter++ ) {
609 if (_tabpStructure[ counter ] != NULL)
610 _tabpStructure[ counter ]->Set( status );
611 }
612 }
613
614
615 /*=====================================================================*/
616 void
617 BuildingLayer::StructureUnset(
618 const OC_BYTE & status )
619 {
620 uint citySurface = _uiLayerLength * _uiLayerWidth;
621 for ( uint counter = 0; counter < citySurface; counter++ ) {
622 if (_tabpStructure[ counter ] != NULL)
623 _tabpStructure[ counter ]->Unset( status );
624 }
625 }
626
627
628 /*=====================================================================*/
629 const bool
630 BuildingLayer::ContainStructure(
631 const uint & W1,
632 const uint & L1,
633 const uint & W2,
634 const uint & L2,
635 const OPENCITY_STRUCTURE_CODE & enumStructCode ) const
636 {
637 uint w, h, linear;
638 Structure* pstruct;
639
640 switch (enumStructCode) {
641 case OC_STRUCTURE_UNDEFINED:
642 return false;
643
644 //TODO: better checking for each type
645 case OC_STRUCTURE_ANY:
646 case OC_STRUCTURE_PART:
647 case OC_STRUCTURE_RES:
648 case OC_STRUCTURE_COM:
649 case OC_STRUCTURE_IND:
650 case OC_STRUCTURE_PARK:
651 case OC_STRUCTURE_ROAD:
652 case OC_STRUCTURE_ELINE:
653 case OC_STRUCTURE_EPLANT_COAL:
654 case OC_STRUCTURE_EPLANT_NUCLEAR:
655 case OC_STRUCTURE_FIREDEPT:
656 case OC_STRUCTURE_POLICEDEPT:
657 case OC_STRUCTURE_EDUCATIONDEPT:
658 case OC_STRUCTURE_TEST:
659 for (h = L1; h <= L2; h++) {
660 linear = h*_uiLayerWidth + W1;
661 for (w = W1; w <= W2; w++, linear++) {
662 pstruct = GetLinearStructure( linear );
663 if (pstruct != NULL) {
664 if ((pstruct->GetCode() == enumStructCode)
665 ||(enumStructCode == OC_STRUCTURE_ANY))
666 return true;
667 }
668 }
669 }
670 break;
671
672 case OC_STRUCTURE_ELECTRIC:
673 for (h = L1; h <= L2; h++) {
674 linear = h*_uiLayerWidth + W1;
675 for (w = W1; w <= W2; w++, linear++) {
676 pstruct = GetLinearStructure( linear );
677 if (pstruct != NULL) {
678 // return true if this structure is
679 // connected to the electric system
680 if (pstruct->IsSet( OC_STRUCTURE_E ) == true)
681 return true;
682 }
683 }
684 }
685 break;
686
687 default:
688 cerr << "WARNING: unknown structure code" << endl;
689 assert(0);
690 break; // keep the compiler happy
691 } // switch
692
693 return false;
694 }
695
696
697 /*=====================================================================*/
698 const bool
699 BuildingLayer::ContainStructureOnly(
700 const uint & W1,
701 const uint & L1,
702 const uint & W2,
703 const uint & L2,
704 const OPENCITY_STRUCTURE_CODE & enumStructCode ) const
705 {
706 // Safe
707 assert( W1 >= 0 );
708 assert( L1 >= 0 );
709 assert( W2 < _uiLayerWidth );
710 assert( L2 < _uiLayerLength );
711
712
713 uint w = 0, l = 0, linear = 0;
714 Structure* pstruct = NULL, *pMain = NULL;
715
716 switch (enumStructCode) {
717 case OC_STRUCTURE_UNDEFINED:
718 return false;
719
720 case OC_STRUCTURE_ANY:
721 return true;
722
723 case OC_STRUCTURE_PART:
724 case OC_STRUCTURE_RES:
725 case OC_STRUCTURE_COM:
726 case OC_STRUCTURE_IND:
727 case OC_STRUCTURE_PARK:
728 case OC_STRUCTURE_ROAD:
729 case OC_STRUCTURE_ELINE:
730 case OC_STRUCTURE_EPLANT_COAL:
731 case OC_STRUCTURE_EPLANT_NUCLEAR:
732 case OC_STRUCTURE_FIREDEPT:
733 case OC_STRUCTURE_POLICEDEPT:
734 case OC_STRUCTURE_EDUCATIONDEPT:
735 case OC_STRUCTURE_TEST:
736 for (l = L1; l <= L2; l++) {
737 linear = l*_uiLayerWidth + W1;
738 for (w = W1; w <= W2; w++, linear++) {
739 // WARNING: IF this is a "part" structure
740 // THEN we look for the main structure code
741 pstruct = GetLinearStructure( linear );
742 if (pstruct != NULL) {
743 pMain = pstruct->GetMain();
744 if (pMain == NULL)
745 pMain = pstruct;
746 if (pMain->GetCode() != enumStructCode)
747 return false;
748 }
749 }
750 }
751 break;
752
753 case OC_STRUCTURE_ELECTRIC:
754 // Return true if all the contained structures are electrified
755 for (l = L1; l <= L2; l++) {
756 linear = l*_uiLayerWidth + W1;
757 for (w = W1; w <= W2; w++, linear++) {
758 pstruct = GetLinearStructure( linear );
759 if (pstruct != NULL) {
760 // Return true if this structure is
761 // connected to the electric system
762 if (pstruct->IsSet( OC_STRUCTURE_E ) == false)
763 return false;
764 }
765 }
766 }
767 break;
768
769 default:
770 cerr << "WARNING: unknown structure code" << endl;
771 assert(0);
772 break; // keep the compiler happy
773 } // switch
774
775 return true;
776 }
777
778
779 /*=====================================================================*/
780 GUIContainer* const
781 BuildingLayer::QueryStructure
782 (
783 const uint & w,
784 const uint & l
785 ) const
786 {
787 Structure* pstruct = _tabpStructure[ l*_uiLayerWidth + w ];
788
789 // Look for the RCI structures around and WEG properties
790 if ( pstruct != NULL ) {
791 // Check for RCI
792 if ( pstruct->IsSet( OC_STRUCTURE_R ) ) {
793 Layer::pbtnQRo->Set( OC_GUIMAIN_VISIBLE );
794 Layer::pbtnQRf->Unset( OC_GUIMAIN_VISIBLE );
795 }
796 else {
797 Layer::pbtnQRo->Unset( OC_GUIMAIN_VISIBLE );
798 Layer::pbtnQRf->Set( OC_GUIMAIN_VISIBLE );
799 }
800
801 if ( pstruct->IsSet( OC_STRUCTURE_C ) ) {
802 Layer::pbtnQCo->Set( OC_GUIMAIN_VISIBLE );
803 Layer::pbtnQCf->Unset( OC_GUIMAIN_VISIBLE );
804 }
805 else {
806 Layer::pbtnQCo->Unset( OC_GUIMAIN_VISIBLE );
807 Layer::pbtnQCf->Set( OC_GUIMAIN_VISIBLE );
808 }
809
810 if ( pstruct->IsSet( OC_STRUCTURE_I ) ) {
811 Layer::pbtnQIo->Set( OC_GUIMAIN_VISIBLE );
812 Layer::pbtnQIf->Unset( OC_GUIMAIN_VISIBLE );
813 }
814 else {
815 Layer::pbtnQIo->Unset( OC_GUIMAIN_VISIBLE );
816 Layer::pbtnQIf->Set( OC_GUIMAIN_VISIBLE );
817 }
818
819 // We check only the E bit, WG are NOT implemented yet
820 if ( pstruct->IsSet( OC_STRUCTURE_E ) ) {
821 Layer::pbtnQEo->Set( OC_GUIMAIN_VISIBLE );
822 Layer::pbtnQEf->Unset( OC_GUIMAIN_VISIBLE );
823 }
824 else {
825 Layer::pbtnQEo->Unset( OC_GUIMAIN_VISIBLE );
826 Layer::pbtnQEf->Set( OC_GUIMAIN_VISIBLE );
827 }
828 }
829 else {
830 // Put all the controls in their off state
831 Layer::pbtnQRf->Set( OC_GUIMAIN_VISIBLE );
832 Layer::pbtnQCf->Set( OC_GUIMAIN_VISIBLE );
833 Layer::pbtnQIf->Set( OC_GUIMAIN_VISIBLE );
834 Layer::pbtnQWf->Set( OC_GUIMAIN_VISIBLE );
835 Layer::pbtnQEf->Set( OC_GUIMAIN_VISIBLE );
836 Layer::pbtnQGf->Set( OC_GUIMAIN_VISIBLE );
837
838 Layer::pbtnQRo->Unset( OC_GUIMAIN_VISIBLE );
839 Layer::pbtnQCo->Unset( OC_GUIMAIN_VISIBLE );
840 Layer::pbtnQIo->Unset( OC_GUIMAIN_VISIBLE );
841 Layer::pbtnQWo->Unset( OC_GUIMAIN_VISIBLE );
842 Layer::pbtnQEo->Unset( OC_GUIMAIN_VISIBLE );
843 Layer::pbtnQGo->Unset( OC_GUIMAIN_VISIBLE );
844 }
845
846 return Layer::pctrQ;
847 }
848
849
850 /*=====================================================================*/
851 void
852 BuildingLayer::Keyboard( const SDL_KeyboardEvent& rcEvent )
853 {}
854
855 void
856 BuildingLayer::MouseMotion( const SDL_MouseMotionEvent& rcEvent )
857 {}
858
859 void
860 BuildingLayer::MouseButton( const SDL_MouseButtonEvent& rcEvent )
861 {}
862
863 void
864 BuildingLayer::Expose( const SDL_ExposeEvent& rcEvent )
865 {}
866
867 void
868 BuildingLayer::Resize( const SDL_ResizeEvent& rcEvent )
869 {}
870
871
872 /*=====================================================================*/
873 /* PRIVATE METHODS */
874 /*=====================================================================*/
875 const bool
876 BuildingLayer::_IsPathConstructive(
877 const uint& w,
878 const uint& h,
879 const OPENCITY_STRUCTURE_CODE& enumStructCode ) const
880 {
881 signed char tabH[4];
882 signed char minH = 127;
883 signed char maxH = -127;
884 signed char deltaH = 0;
885 signed char minHNb = 0;
886
887 uint uiIndex = 0;
888 Structure* pStruct = NULL;
889
890 // Return immediatly if there's already
891 // something different than enumStructCode built on it
892 pStruct = this->GetStructure(w,h);
893 if ((pStruct != NULL) && (pStruct->GetCode() != enumStructCode))
894 return false;
895
896 // Calculate the min and the max heights
897 gVars.gpMapMgr->GetSquareHeight( w, h, tabH );
898 for (uiIndex = 0; uiIndex < 4; uiIndex++) {
899 if (tabH[uiIndex] < minH)
900 minH = tabH[uiIndex];
901 if (tabH[uiIndex] > maxH)
902 maxH = tabH[uiIndex];
903 }
904
905 // Calculate the absolute difference
906 deltaH = maxH - minH;
907 if (deltaH > 1)
908 // we can not build road if the difference is too big
909 return false;
910
911 // Else, calculate the number of minH points
912 // minHNb is already initialized
913 for (uiIndex = 0; uiIndex < 4; uiIndex++) {
914 if (tabH[uiIndex] == minH)
915 minHNb++;
916 }
917
918 // The number of minH points should be the multiple of 2
919 if ((minHNb % 2) != 0)
920 return false;
921
922 // Here, the square is "road constructive"
923 return true;
924 }
925
926
927 /*=====================================================================*/
928 const OPENCITY_ERR_CODE
929 BuildingLayer::_BuildPathStructure(
930 uint W1, uint L1,
931 uint W2, uint L2,
932 const OPENCITY_STRUCTURE_CODE & enumStructCode,
933 uint& rCost )
934 {
935 OPENCITY_DEBUG( "I'm building some path structures" );
936
937 uint w = 0, l = 0;
938 uint cost = 0;
939
940 OPENCITY_SWAP( W1, W2, uint );
941 OPENCITY_SWAP( L1, L2, uint );
942
943 rCost = 0;
944
945 // is the area road constructive ?
946 // which is the largest side ?
947 if (W2 - W1 >= L2 - L1) {
948 // we test the largest side first
949 for (w = W1; w < W2; w++)
950 if (_IsPathConstructive( w, L1, enumStructCode ) == false)
951 return OC_ERR_SOMETHING;
952 // we test the rest
953 for (l = L1; l <= L2; l++)
954 if (_IsPathConstructive( W2, l, enumStructCode ) == false)
955 return OC_ERR_SOMETHING;
956 }
957 else {
958 // we test the largest side first
959 for (l = L1; l < L2; l++)
960 if (_IsPathConstructive( W1, l, enumStructCode ) == false)
961 return OC_ERR_SOMETHING;
962 // we test the rest
963 for (w = W1; w <= W2; w++)
964 if (_IsPathConstructive( w, L2, enumStructCode ) == false)
965 return OC_ERR_SOMETHING;
966 }
967
968
969 // it's OK, let's GO !
970 uint linearIndex = 0;
971
972 if (W2 - W1 >= L2 - L1) {
973 // we build the largest side first
974 linearIndex = (L1*_uiLayerWidth) + W1;
975 for (w = W1; w < W2; w++, linearIndex++) {
976 _BuildPathStructure( w, L1, linearIndex, enumStructCode, cost );
977 rCost += cost;
978 }
979
980 // then we build the rest
981 // this is already calculated by the previous "for"
982 // linearIndex = (L1*_uiLayerWidth) + W2;
983 for (l = L1; l <= L2; l++, linearIndex += _uiLayerWidth) {
984 _BuildPathStructure( W2, l, linearIndex, enumStructCode, cost );
985 rCost += cost;
986 }
987 }
988 else {
989 // we build the largest side first
990 linearIndex = (L1*_uiLayerWidth) + W1;
991 for (l = L1; l < L2; l++, linearIndex += _uiLayerWidth) {
992 _BuildPathStructure( W1, l, linearIndex, enumStructCode, cost );
993 rCost += cost;
994 }
995
996 // then we build the rest
997 // this is already calculated by the previous "for"
998 // linearIndex = (L2*_uiLayerWidth) + W1;
999 for (w = W1; w <= W2; w++, linearIndex++) {
1000 _BuildPathStructure( w, L2, linearIndex, enumStructCode, cost );
1001 rCost += cost;
1002 }
1003 }
1004
1005 return OC_ERR_FREE;
1006 }
1007
1008
1009 /*=====================================================================*/
1010 void
1011 BuildingLayer::_BuildPathStructure(
1012 const uint & w,
1013 const uint & l,
1014 const uint & linearIndex,
1015 const OPENCITY_STRUCTURE_CODE & enumStructCode,
1016 uint& rCost )
1017 {
1018 Structure* pstructNeighbour = NULL;
1019 PathStructure* ppathstructNeighbour = NULL;
1020 PathStructure* pNewStructure = NULL;
1021 uint nW = 0, nL = 0;
1022
1023
1024 // IF there's already something, THEN we return
1025 if (_tabpStructure[linearIndex] != NULL) {
1026 rCost = 0;
1027 return;
1028 }
1029
1030 // Build the new road
1031 pNewStructure = new PathStructure(enumStructCode);
1032 rCost = gVars.gpPropertyMgr->Get( OC_TOOL_COST, enumStructCode );
1033 _tabpStructure[ linearIndex ] = pNewStructure;
1034
1035 // Get the neighbour in the North
1036 if (gVars.gpMapMgr->GetNeighbourWH( w, l, nW, nL, OC_DIR_O_N ) == true) {
1037 pstructNeighbour = BuildingLayer::GetStructure( nW, nL );
1038 // if the neighbour is a road structure
1039 // then add this new road structure as its new neighbour
1040 // we do the same with the new road structure
1041 if ( (pstructNeighbour != NULL)
1042 &&(pstructNeighbour->GetCode() == enumStructCode)) {
1043 //debug cout << "found 1 neighbour in the North" << endl;
1044 ppathstructNeighbour = (PathStructure*)pstructNeighbour;
1045 ppathstructNeighbour->AddNeighbour(
1046 *pNewStructure, OC_DIR_O_S );
1047 pNewStructure->AddNeighbour(
1048 *ppathstructNeighbour, OC_DIR_O_N );
1049 }
1050 }
1051
1052 // Get the neighbour in the South
1053 if (gVars.gpMapMgr->GetNeighbourWH( w, l, nW, nL, OC_DIR_O_S ) == true) {
1054 pstructNeighbour = BuildingLayer::GetStructure( nW, nL );
1055 if ( (pstructNeighbour != NULL)
1056 &&(pstructNeighbour->GetCode() == enumStructCode)) {
1057 ppathstructNeighbour = (PathStructure*)pstructNeighbour;
1058 ppathstructNeighbour->AddNeighbour(
1059 *pNewStructure, OC_DIR_O_N );
1060 pNewStructure->AddNeighbour(
1061 *ppathstructNeighbour, OC_DIR_O_S );
1062 }
1063 }
1064
1065 // Get the neighbour in the West
1066 if (gVars.gpMapMgr->GetNeighbourWH( w, l, nW, nL, OC_DIR_O_W ) == true) {
1067 pstructNeighbour = BuildingLayer::GetStructure( nW, nL );
1068 if ( (pstructNeighbour != NULL)
1069 &&(pstructNeighbour->GetCode() == enumStructCode)) {
1070 ppathstructNeighbour = (PathStructure*)pstructNeighbour;
1071 ppathstructNeighbour->AddNeighbour(
1072 *pNewStructure, OC_DIR_O_E );
1073 pNewStructure->AddNeighbour(
1074 *ppathstructNeighbour, OC_DIR_O_W );
1075 }
1076 }
1077
1078 // Get the neighbour in the East
1079 if (gVars.gpMapMgr->GetNeighbourWH( w, l, nW, nL, OC_DIR_O_E ) == true) {
1080 pstructNeighbour = BuildingLayer::GetStructure( nW, nL );
1081 // if the neighbour is a road structure
1082 // then add this new road structure as its new neighbour
1083 // we do the same with the new road structure
1084 if ( (pstructNeighbour != NULL)
1085 &&(pstructNeighbour->GetCode() == enumStructCode)) {
1086 ppathstructNeighbour = (PathStructure*)pstructNeighbour;
1087 ppathstructNeighbour->AddNeighbour(
1088 *pNewStructure, OC_DIR_O_W );
1089 pNewStructure->AddNeighbour(
1090 *ppathstructNeighbour, OC_DIR_O_E );
1091 }
1092 }
1093 }
1094
1095
1096 /*=====================================================================*/
1097 const OPENCITY_ERR_CODE
1098 BuildingLayer::_BuildRCIStructure(
1099 uint W1, uint L1,
1100 uint W2, uint L2,
1101 const OPENCITY_STRUCTURE_CODE & enumStructCode,
1102 uint& rCost )
1103 {
1104 OPENCITY_DEBUG( "I'm building some RCI structures" );
1105
1106 static uint w, l;
1107 static uint linearIndex;
1108 static int cost;
1109
1110
1111 OPENCITY_SWAP( W1, W2, uint );
1112 OPENCITY_SWAP( L1, L2, uint );
1113
1114 //debug testing
1115 //cout << "size of RCIStructure is: " << sizeof( RCIStructure ) << endl;
1116
1117 rCost = 0;
1118 cost = gVars.gpPropertyMgr->Get( OC_TOOL_COST, enumStructCode );
1119
1120 // Let's GO !
1121 for (l = L1; l <= L2; l++) {
1122 linearIndex = (l*_uiLayerWidth) + W1;
1123 for (w = W1; w <= W2; w++, linearIndex++) {
1124 if ((_tabpStructure[ linearIndex ] == NULL)
1125 && (gVars.gpMapMgr->IsSquarePlane(w, l) == true )) {
1126 _tabpStructure[ linearIndex ] = new RCIStructure(enumStructCode);
1127 rCost += cost;
1128 }
1129 }
1130 }
1131
1132 return (rCost > 0) ? OC_ERR_FREE : OC_ERR_SOMETHING;
1133 }
1134
1135
1136 /*=====================================================================*/
1137 const OPENCITY_ERR_CODE
1138 BuildingLayer::_BuildFloraStructure(
1139 uint W1, uint L1,
1140 uint W2, uint L2,
1141 const OPENCITY_STRUCTURE_CODE & enumStructCode,
1142 uint& rCost )
1143 {
1144 // OPENCITY_DEBUG( "I'm building some flora structures" );
1145
1146 static uint w, l;
1147 static uint linearIndex;
1148 static int cost;
1149
1150
1151 OPENCITY_SWAP( W1, W2, uint );
1152 OPENCITY_SWAP( L1, L2, uint );
1153
1154 //debug testing
1155 //cout << "size of RCIStructure is: " << sizeof( RCIStructure ) << endl;
1156
1157 rCost = 0;
1158 cost = gVars.gpPropertyMgr->Get( OC_TOOL_COST, enumStructCode );
1159
1160 // Let's GO !
1161 for (l = L1; l <= L2; l++) {
1162 linearIndex = (l*_uiLayerWidth) + W1;
1163 for (w = W1; w <= W2; w++, linearIndex++) {
1164 if ((_tabpStructure[ linearIndex ] == NULL)
1165 && (gVars.gpMapMgr->IsSquarePlane(w, l) == true )) {
1166 _tabpStructure[ linearIndex ] = new TreeStructure(enumStructCode);
1167 rCost += cost;
1168 }
1169 }
1170 }
1171
1172 return (rCost > 0) ? OC_ERR_FREE : OC_ERR_SOMETHING;
1173 }
1174
1175
1176 /*=====================================================================*/
1177 const OPENCITY_ERR_CODE
1178 BuildingLayer::_BuildWEGStructure(
1179 uint W1, uint L1,
1180 const OPENCITY_STRUCTURE_CODE & enumStructCode,
1181 uint& rCost )
1182 {
1183 OPENCITY_DEBUG( "I'm building some WEG structures" );
1184
1185 uint W2, L2;
1186 uint sw, sl, sh; // Structure's width, length and height
1187 OPENCITY_GRAPHIC_CODE gcode;
1188
1189 // Get the graphic code of the structure
1190 gcode = gVars.gpPropertyMgr->GetGC( enumStructCode );
1191 if ( gcode == OC_EMPTY ) {
1192 OPENCITY_DEBUG( "WARNING: not implemented" );
1193 assert(0);
1194 }
1195
1196 // Get the corresponding WLH and calculate the range
1197 // NOTE: An coal electric plant is 4x4 size by default
1198 gVars.gpPropertyMgr->GetWLH( gcode, sw, 4, sl, 4, sh, 1 );
1199 W2 = W1 + sw - 1;
1200 L2 = L1 + sl - 1;
1201
1202 // Isn't there enough space ?
1203 // OR is there already something on the surface ?
1204 if (IsConstructive(W1, L1, W2, L2, enumStructCode) == false) {
1205 return OC_ERR_SOMETHING;
1206 }
1207
1208 // Let's do it
1209 _BuildWEGStructure( W1, L1, W2, L2, enumStructCode, rCost );
1210
1211 return OC_ERR_FREE;
1212 }
1213
1214
1215 /*=====================================================================*/
1216 void
1217 BuildingLayer::_BuildWEGStructure(
1218 uint W1, uint L1,
1219 uint W2, uint L2,
1220 const OPENCITY_STRUCTURE_CODE & enumStructCode,
1221 uint& rCost )
1222 {
1223 uint w, l;
1224 Structure* pNewStructure, *pMainStructure;
1225 uint linearIndex;
1226
1227 // Create the main structure first, but we don't insert it
1228 pMainStructure = new WEGStructure( enumStructCode );
1229 rCost = gVars.gpPropertyMgr->Get( OC_TOOL_COST, enumStructCode );
1230
1231 // Create all the area as parts of a bigger main structure
1232 for (l = L1; l <= L2; l++) {
1233 linearIndex = (l*_uiLayerWidth) + W1;
1234 for (w = W1; w <= W2; w++, linearIndex++) {
1235 // Build new WEG as a structure part
1236 _tabpStructure[linearIndex] = new Structure( OC_STRUCTURE_PART, pMainStructure );
1237 }
1238 }
1239
1240 // Delete the part structure at the coordinates W1,L1
1241 linearIndex = (L1*_uiLayerWidth) + W1;
1242 pNewStructure = _tabpStructure[ linearIndex ];
1243 delete pNewStructure;
1244 // Put the main structure upon it
1245 _tabpStructure[ linearIndex ] = pMainStructure;
1246 }
1247
1248
1249 /*=====================================================================*/
1250 void
1251 BuildingLayer::_LoadStructure(
1252 const uint& w1,
1253 const uint& l1,
1254 Structure* pMainStruct )
1255 {
1256 OPENCITY_DEBUG( "Loading some structures" );
1257
1258 uint w = 0, l = 0, w2 = 0, l2 = 0; // Map coordinates
1259 uint sw = 0, sl = 0, sh = 0; // Structure's width, length and height
1260 OPENCITY_GRAPHIC_CODE gcode = OC_EMPTY;
1261 uint linearIndex = 0;
1262
1263 // Get the graphic code of the structure
1264 gcode = pMainStruct->GetGraphicCode();
1265 if ( gcode == OC_EMPTY ) {
1266 OPENCITY_FATAL( "Not implemented" );
1267 assert(0);
1268 }
1269
1270 // Get the corresponding WLH and calculate the range
1271 // NOTE: An coal electric plant is 4x4 size by default
1272 gVars.gpPropertyMgr->GetWLH( gcode, sw, 1, sl, 1, sh, 1 );
1273 w2 = w1 + sw - 1;
1274 l2 = l1 + sl - 1;
1275
1276 // Create all the area as parts of a bigger main structure
1277 if (sw > 1 || sl > 1 || sh > 1) {
1278 for( l = l1; l <= l2; l++) {
1279 linearIndex = (l*_uiLayerWidth) + w1;
1280 for( w = w1; w <= w2; w++, linearIndex++) {
1281 // Build new WEG as a structure part
1282 _tabpStructure[linearIndex] = new Structure( OC_STRUCTURE_PART, pMainStruct );
1283 }
1284 }
1285
1286 // Delete the part structure at the coordinates W1, L1
1287 delete _tabpStructure[ (l1*_uiLayerWidth) + w1 ];
1288 }
1289
1290 // Put the main structure at W1, L1
1291 _tabpStructure[ (l1*_uiLayerWidth) + w1 ] = pMainStruct;
1292 }
1293
1294
1295 /*=====================================================================*/
1296 const OPENCITY_ERR_CODE
1297 BuildingLayer::_DestroyStructure(
1298 const uint & w,
1299 const uint & l,
1300 uint& rCost )
1301 {
1302 uint linearIndex = (l*_uiLayerWidth) + w;
1303 Structure* pstruct, *pstructMain;
1304 uint dw1 = w, dl1 = l, dw2 = w, dl2 = l;
1305 uint tempW, tempL;
1306 uint sw, sl, sh; // Structure's width, length and height
1307 OPENCITY_ERR_CODE errCode;
1308
1309 // The fund used to destroy this structure
1310 rCost = 0;
1311
1312 // Get the structure, if there is nothing, we return error
1313 pstruct = _tabpStructure[ linearIndex ];
1314 if ( pstruct == NULL )
1315 return OC_ERR_SOMETHING;
1316
1317 rCost = gVars.gpPropertyMgr->Get( OC_DESTROY_COST, pstruct->GetCode(), pstruct );
1318
1319 // The main structure is pstruct itself
1320 pstructMain = pstruct;
1321 errCode = OC_ERR_FREE;
1322 switch ( pstruct->GetCode() ) {
1323 case OC_STRUCTURE_PART:
1324 // We get the main structure otherwise
1325 pstructMain = pstruct->GetMain();
1326 case OC_STRUCTURE_RES:
1327 case OC_STRUCTURE_COM:
1328 case OC_STRUCTURE_IND:
1329
1330 case OC_STRUCTURE_PARK:
1331 case OC_STRUCTURE_FLORA:
1332 case OC_STRUCTURE_TEST:
1333 case OC_STRUCTURE_FIREDEPT:
1334 case OC_STRUCTURE_POLICEDEPT:
1335 case OC_STRUCTURE_HOSPITALDEPT:
1336 case OC_STRUCTURE_EDUCATIONDEPT:
1337 case OC_STRUCTURE_EPLANT_COAL:
1338 case OC_STRUCTURE_EPLANT_NUCLEAR:
1339 // Get the width, length and height of the main structure
1340 // WARNING: invalid default values given.
1341 gVars.gpPropertyMgr->GetWLH( pstructMain->GetGraphicCode(), sw, 0, sl, 0, sh, 0 );
1342 assert( (sw != 0) && (sl != 0) );
1343 sw--; sl--; // We calculate the range
1344
1345 // Calculate the possible area to limit cpu usage
1346 gVars.gpMapMgr->GetPossibleWH( dw1, dl1, -sw, -sl );
1347 gVars.gpMapMgr->GetPossibleWH( dw2, dl2, sw, sl );
1348 // Now search for all STRUCTURE_PART and the main structure, destroy em
1349 for ( tempL = dl1; tempL <= dl2; tempL++ ) {
1350 linearIndex = tempL*_uiLayerWidth + dw1;
1351 for ( tempW = dw1; tempW <= dw2; tempW++ ) {
1352 //debug cout << "Hi coco " << endl;
1353 // WARNING: pointer comparaison !
1354 pstruct = _tabpStructure[ linearIndex ];
1355 if ( pstruct != NULL )
1356 if (( pstruct->GetMain() == pstructMain )
1357 || (pstruct == pstructMain)) {
1358 _tabpStructure[ linearIndex ] = NULL;
1359 delete pstruct;
1360 //debug cout << "coco died" << endl;
1361 }
1362 linearIndex++;
1363 }
1364 }
1365 break;
1366
1367 case OC_STRUCTURE_ELINE:
1368 case OC_STRUCTURE_ROAD:
1369 errCode = _DestroyPathStructure(
1370 w, l, pstruct->GetCode() );
1371 break;
1372
1373 default:
1374 errCode = OC_ERR_SOMETHING;
1375 OPENCITY_DEBUG( "WARNING: game design error" );
1376 assert( 0 );
1377 }
1378
1379 return errCode;
1380 }
1381
1382
1383 /*=====================================================================*/
1384 const OPENCITY_ERR_CODE
1385 BuildingLayer::_DestroyPathStructure(
1386 uint w, uint l,
1387 const OPENCITY_STRUCTURE_CODE & enumStructCode )
1388 {
1389 Structure* pstruct, *pstructNeighbour;
1390 PathStructure* ppathstructNeighbour, *ppathstruct;
1391 uint nW, nH;
1392
1393 pstruct = _tabpStructure[ l*_uiLayerWidth + w ];
1394 // this is not necessary because it's already checked by the user "Destroy"
1395 if (pstruct == NULL)
1396 return OC_ERR_SOMETHING;
1397
1398 ppathstruct = (PathStructure*)pstruct;
1399 // get the neighbour in the North
1400 if (gVars.gpMapMgr->GetNeighbourWH( w, l, nW, nH, OC_DIR_O_N ) == true) {
1401 pstructNeighbour = BuildingLayer::GetStructure( nW, nH );
1402 // if the neighbour is a path structure
1403 if ( (pstructNeighbour != NULL)
1404 &&(pstructNeighbour->GetCode() == enumStructCode)) {
1405 ppathstructNeighbour = (PathStructure*)pstructNeighbour;
1406 ppathstructNeighbour->RemoveNeighbour( *ppathstruct, OC_DIR_O_S );
1407 }
1408 }
1409
1410 // get the neighbour in the South
1411 if (gVars.gpMapMgr->GetNeighbourWH( w, l, nW, nH, OC_DIR_O_S ) == true) {
1412 pstructNeighbour = BuildingLayer::GetStructure( nW, nH );
1413 if ( (pstructNeighbour != NULL)
1414 &&(pstructNeighbour->GetCode() == enumStructCode)) {
1415 ppathstructNeighbour = (PathStructure*)pstructNeighbour;
1416 ppathstructNeighbour->RemoveNeighbour( *ppathstruct, OC_DIR_O_N );
1417 }
1418 }
1419
1420 // get the neighbour in the West
1421 if (gVars.gpMapMgr->GetNeighbourWH( w, l, nW, nH, OC_DIR_O_W ) == true) {
1422 pstructNeighbour = BuildingLayer::GetStructure( nW, nH );
1423 if ( (pstructNeighbour != NULL)
1424 &&(pstructNeighbour->GetCode() == enumStructCode)) {
1425 ppathstructNeighbour = (PathStructure*)pstructNeighbour;
1426 ppathstructNeighbour->RemoveNeighbour( *ppathstruct, OC_DIR_O_E );
1427 }
1428 }
1429
1430 // get the neighbour in the East
1431 if (gVars.gpMapMgr->GetNeighbourWH( w, l, nW, nH, OC_DIR_O_E ) == true) {
1432 pstructNeighbour = BuildingLayer::GetStructure( nW, nH );
1433 if ( (pstructNeighbour != NULL)
1434 &&(pstructNeighbour->GetCode() == enumStructCode)) {
1435 ppathstructNeighbour = (PathStructure*)pstructNeighbour;
1436 ppathstructNeighbour->RemoveNeighbour( *ppathstruct, OC_DIR_O_W );
1437 }
1438 }
1439
1440 delete pstruct;
1441 _tabpStructure[ l*_uiLayerWidth + w ] = NULL;
1442
1443 return OC_ERR_FREE;
1444 }
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481