-
Notifications
You must be signed in to change notification settings - Fork 6
/
map.c
1821 lines (1521 loc) · 51.3 KB
/
map.c
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright (c) 1994, 2002, 2003 Johannes Prix
* Copyright (c) 1994, 2002, 2003 Reinhard Prix
*
*
* This file is part of Freedroid
*
* Freedroid 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.
*
* Freedroid 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 Freedroid; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*/
/*----------------------------------------------------------------------
*
* Desc: All map-related functions, which also includes loading of decks
* and whole ships, starting the lifts and consoles if close to the
* paradroid, refreshes as well as determining the map brick that contains
* specified coordinates are done in this file.
*
*----------------------------------------------------------------------*/
#define _map_c
#include "system.h"
#include "defs.h"
#include "struct.h"
#include "proto.h"
#include "global.h"
#include "map.h"
#include "maped.h"
#define AREA_NAME_STRING "Area name=\""
#define LEVEL_NAME_STRING "Name of this level="
#define LEVEL_ENTER_COMMENT_STRING "Comment of the Influencer on entering this level=\""
#define BACKGROUND_SONG_NAME_STRING "Name of background song for this level="
void ResetLevelMap (Level Lev);
void GetThisLevelsDroids( const char* SectionPointer );
char *StructToMem(Level Lev);
void GetAlerts (Level Lev);
int IsWallBlock (int block);
/*@Function============================================================
@Desc: unsigned char GetMapBrick(Level deck, float x, float y): liefert
intern-code des Elements, das sich auf (deck x/y) befindet
@Ret:
@Int:
* $Function----------------------------------------------------------*/
unsigned char
GetMapBrick (Level deck, float x, float y)
{
int xx, yy;
xx = (int) rintf(x);
yy = (int) rintf(y);
if ( (yy >= deck->ylen)|| (yy < 0) || (xx >= deck->xlen) || (xx<0) )
return VOID;
else
return (deck->map[yy][xx]);
} /* GetMapBrick() */
/*@Function============================================================
@Desc: int GetCurrentLift: finds Lift-number to your position
@Ret: -1: Not found !!
num: Number of cur. Lift in AllLifts[]
@Int:
* $Function----------------------------------------------------------*/
int
GetCurrentLift (void)
{
int i;
int curlev = CurLevel->levelnum;
int gx, gy;
gx = rintf(Me.pos.x);
gy = rintf(Me.pos.y);
DebugPrintf( 1 , "\nint GetCurrentLift( void ): curlev=%d gx=%d gy=%d" , curlev, gx, gy );
DebugPrintf( 1 , "\nint GetCurrentLift( void ): List of elevators:\n");
for (i = 0; i < curShip.num_lifts+1; i++)
{
DebugPrintf( 1 , "\nIndex=%d level=%d gx=%d gy=%d" , i ,
curShip.AllLifts[i].level , curShip.AllLifts[i].x , curShip.AllLifts[i].y );
}
for (i = 0; i < curShip.num_lifts+1; i++) // we check for one more than present, so the last reached
// will really mean: NONE FOUND.
{
if (curShip.AllLifts[i].level != curlev)
continue;
if ((curShip.AllLifts[i].x == gx) &&
(curShip.AllLifts[i].y == gy))
break;
}
if (i == curShip.num_lifts+1) // none found
return -1;
else
return i;
}; // GetCurrentLift
/*@Function============================================================
@Desc: ActSpecialField: checks Influencer on SpecialFields like
Lifts and Konsoles and acts on it
@Ret: void
@Int:
* $Function----------------------------------------------------------*/
void
ActSpecialField (float x, float y)
{
unsigned char MapBrick;
float cx, cy; /* tmp: NullPunkt im Blockzentrum */
float myspeed2;
DebugPrintf (2, "\nvoid ActSpecialField(int x, int y): Real function call confirmed.");
MapBrick = GetMapBrick (CurLevel, x, y);
myspeed2 = Me.speed.x*Me.speed.x + Me.speed.y*Me.speed.y;
switch (MapBrick)
{
case LIFT:
if ( myspeed2 > 1.0 )
break;
if ( (Me.status == ACTIVATE) || (GameConfig.TakeoverActivates && (Me.status==TRANSFERMODE)) )
{
cx = rintf(x) - x ;
cy = rintf(y) - y ;
/* Lift nur betreten, wenn ca. im Zentrum */
if ((cx * cx + cy * cy) < Droid_Radius * Droid_Radius)
EnterLift ();
}
break;
case KONSOLE_R:
case KONSOLE_L:
case KONSOLE_O:
case KONSOLE_U:
if (myspeed2 > 1.0)
break;
if ( (Me.status == ACTIVATE) || (GameConfig.TakeoverActivates && (Me.status==TRANSFERMODE)))
{
EnterKonsole ();
DebugPrintf (2, "\nvoid ActSpecialField(int x, int y): Back from EnterKonsole().\n");
}
break;
case REFRESH1:
case REFRESH2:
case REFRESH3:
case REFRESH4:
RefreshInfluencer ();
break;
default:
break;
} /* switch */
DebugPrintf (2, "\nvoid ActSpecialField(int x, int y): end of function reached.");
} /* ActSpecialField */
/*@Function============================================================
@Desc: AnimateRefresh():
@Ret: void
@Int:
* $Function----------------------------------------------------------*/
void
AnimateRefresh (void)
{
static float InnerWaitCounter = 0;
//static int InnerPhase = 0; /* Zaehler fuer innere Phase */
int i, j;
int x, y;
DebugPrintf (2, "\nvoid AnimateRefresh(void): real function call confirmed.");
InnerWaitCounter += Frame_Time () * 10;
// if( (((int)rintf(InnerWaitCounter)) % INNER_REFRESH_COUNTER) == 0) {
// InnerPhase ++;
// InnerPhase %= INNER_PHASES;
//InnerPhase = (((int) rintf (InnerWaitCounter)) % INNER_PHASES);
for (i = 0; i < MAX_REFRESHES_ON_LEVEL; i++)
{
x = CurLevel->refreshes[i].x;
y = CurLevel->refreshes[i].y;
if (x == -1 || y == -1)
break;
CurLevel->map[y][x] = (((int) rintf (InnerWaitCounter)) % 4) + REFRESH1;
/* Inneres Refresh animieren */
for (j = 0; j < 4; j++)
{
; /* nix hier noch... */ // FIXME
} /* for */
} /* for */
DebugPrintf (2, "\nvoid AnimateRefresh(void): end of function reached.");
return;
} /* AnimateRefresh */
/*@Function============================================================
@Desc: LoadShip(): loads the data for a whole ship
@Ret: OK | ERR
@Int:
* $Function----------------------------------------------------------*/
int
LoadShip (char *filename)
{
char *fpath;
char *ShipData;
char *endpt; /* Pointer to end-strings */
char *LevelStart[MAX_LEVELS]; /* Pointer to a level-start */
int level_anz;
char *Buffer;
int i;
FreeShipMemory(); // clear vestiges of previous ship data, if any
/* Read the whole ship-data to memory */
#define END_OF_SHIP_DATA_STRING "*** End of Ship Data ***"
fpath = find_file (filename, MAP_DIR, NO_THEME, CRITICAL);
ShipData = ReadAndMallocAndTerminateFile( fpath , END_OF_SHIP_DATA_STRING ) ;
//--------------------
// Now we read the Area-name from the loaded data
//
Buffer = ReadAndMallocStringFromData (ShipData, AREA_NAME_STRING, "\"");
strncpy (curShip.AreaName, Buffer, 99);
curShip.AreaName[99]='\0';
free (Buffer);
//--------------------
// Now we count the number of levels and remember their start-addresses.
// This is done by searching for the LEVEL_END_STRING again and again
// until it is no longer found in the ship file. good.
//
level_anz = 0;
endpt = ShipData;
LevelStart[level_anz] = ShipData;
while ((endpt = strstr (endpt, LEVEL_END_STRING)) != NULL)
{
endpt += strlen (LEVEL_END_STRING);
level_anz++;
LevelStart[level_anz] = endpt + 1;
}
/* init the level-structs */
curShip.num_levels = level_anz;
for (i = 0; i < curShip.num_levels; i++)
{
curShip.AllLevels[i] = LevelToStruct (LevelStart[i]);
if (curShip.AllLevels[i] == NULL)
{
DebugPrintf (0, "ERROR: reading of level %d failed\n", i);
return (ERR);
}
InterpretMap (curShip.AllLevels[i]); // initialize doors, refreshes and lifts
}
free (ShipData);
return OK;
} /* LoadShip () */
/*@Function============================================================
@Desc: char *StructToMem(Level Lev):
@Ret: char *: pointer to Map in a memory field
@Int:
* $Function----------------------------------------------------------*/
char *StructToMem(Level Lev)
{
char *LevelMem;
int i, j;
size_t MemAmount=0; /* the size of the level-data */
int xlen = Lev->xlen, ylen = Lev->ylen;
int anz_wp; /* number of Waypoints */
char linebuf[500]; /* Buffer */
waypoint *this_wp;
anz_wp = Lev->num_waypoints;
/* estimate the amount of memory needed */
MemAmount = (xlen+1) * ylen; /* Map-memory */
MemAmount += anz_wp * MAX_WP_CONNECTIONS * 4;
MemAmount += 50000; /* Puffer fuer Dimensionen, mark-strings .. */
/* allocate some memory */
if( (LevelMem = (char*)MyMalloc(MemAmount)) == NULL) {
DebugPrintf(1, "\n\nError in StructToMem: Could not allocate memory...\n\nTerminating...\n\n");
Terminate(ERR);
}
// Write the data to memory:
// Here the levelnumber and general information about the level is written
sprintf(linebuf, "Levelnumber: %d\nxlen of this level: %d\nylen of this level: %d\ncolor of this level: %d\n",
Lev->levelnum, Lev->xlen, Lev->ylen, Lev->color);
strcpy(LevelMem, linebuf);
strcat(LevelMem, LEVEL_NAME_STRING );
strcat(LevelMem, Lev->Levelname );
strcat(LevelMem, "\n" );
strcat(LevelMem, LEVEL_ENTER_COMMENT_STRING );
strcat(LevelMem, Lev->Level_Enter_Comment );
strcat(LevelMem, "\n" );
strcat(LevelMem, BACKGROUND_SONG_NAME_STRING );
strcat(LevelMem, Lev->Background_Song_Name );
// strcat(LevelMem, Decknames[Lev->levelnum] );
strcat(LevelMem, "\n" );
// Now the beginning of the actual map data is marked:
strcat(LevelMem, MAP_BEGIN_STRING);
strcat(LevelMem, "\n");
// Now in the loop each line of map data should be saved as a whole
for( i = 0 ; i < ylen ; i++ ) {
ResetLevelMap (Lev); // make sure all doors are closed
for (j=0; j<xlen; j++)
{
sprintf (linebuf, "%2d ", Lev->map[i][j]);
strcat (LevelMem, linebuf);
}
strcat(LevelMem, "\n");
}
// --------------------
// The next thing we must do is write the waypoints of this level
strcat(LevelMem, WP_BEGIN_STRING);
strcat(LevelMem, "\n");
for(i=0; i< Lev->num_waypoints ; i++)
{
sprintf(linebuf, "Nr.=%3d x=%4d y=%4d", i, Lev->AllWaypoints[i].x , Lev->AllWaypoints[i].y );
strcat( LevelMem, linebuf );
strcat( LevelMem, "\t ");
strcat (LevelMem, CONNECTION_STRING);
this_wp = &Lev->AllWaypoints[i];
for( j=0; j < this_wp->num_connections; j++)
{
sprintf(linebuf, "%2d ", this_wp->connections[j]);
strcat(LevelMem, linebuf);
} /* for connections */
strcat(LevelMem, "\n");
} /* for waypoints */
strcat(LevelMem, LEVEL_END_STRING);
strcat(LevelMem, "\n----------------------------------------------------------------------\n");
/* FERTIG: hat die Memory - Schaetzung gestimmt ?? */
/* wenn nicht: :-( */
if( strlen(LevelMem) >= MemAmount)
{
DebugPrintf(0, "\n\nError in StructToMem: Estimate of memory was wrong...\nTerminating...\n");
Terminate(ERR);
}
/* all ok : */
return (LevelMem);
} /* Struct to Mem */
/*@Function============================================================
@Desc: int SaveShip(void): saves ship-data to disk
@Ret: OK | ERR
@Int:
* $Function----------------------------------------------------------*/
int SaveShip( const char *shipname)
{
char *LevelMem; /* linear memory for one Level */
const char *MapHeaderString;
FILE *ShipFile; // to this file we will save all the ship data...
char filename[FILENAME_LEN+1];
int level_anz;
int array_i, array_num;
int i;
DebugPrintf (2, "\nint SaveShip(char *shipname): real function call confirmed.");
/* Get the complete filename */
strcpy(filename, shipname);
strcat(filename, SHIP_EXT);
/* count the levels */
level_anz = 0;
while(curShip.AllLevels[level_anz++]);
level_anz --;
DebugPrintf (2, "\nint SaveShip(char *shipname): now opening the ship file...");
/* open file */
if( (ShipFile = fopen(filename, "w")) == NULL) {
printf("\n\nError opening ship file...\n\nTerminating...\n\n");
Terminate(ERR);
return ERR;
}
//--------------------
// Now that the file is opend for writing, we can start writing. And the first thing
// we will write to the file will be a fine header, indicating what this file is about
// and things like that...
//
MapHeaderString="\n\
----------------------------------------------------------------------\n\
This file was generated using the Freedroid level editor.\n\
Please feel free to make any modifications you like, but in order for you\n\
to have an easier time, it is recommended that you use the Freedroid level\n\
editor for this purpose. If you have created some good new maps, please \n\
send a short notice (not too large files attached) to the freedroid project.\n\
\n\
----------------------------------------------------------------------\n\
\n";
fwrite ( MapHeaderString , strlen( MapHeaderString), sizeof(char), ShipFile);
// Now we write the area name back into the file
fwrite ( AREA_NAME_STRING , strlen( AREA_NAME_STRING ), sizeof(char), ShipFile);
fwrite ( curShip.AreaName , strlen( curShip.AreaName ), sizeof(char), ShipFile);
fwrite( "\"\n\n ", strlen( "\"\n\n " ) , sizeof(char) , ShipFile );
/* Save all Levels */
DebugPrintf (2, "\nint SaveShip(char *shipname): now saving levels...");
for( i=0; i<level_anz; i++)
{
//--------------------
// What the heck does this do?
// Do we really need this? Why?
//
array_i =-1;
array_num = -1;
while( curShip.AllLevels[++array_i] != NULL)
{
if( curShip.AllLevels[array_i]->levelnum == i)
{
if( array_num != -1 )
{
printf("\n\nIdentical Levelnumber Error in SaveShip...\n\nTerminating\n\n");
Terminate(ERR);
return ERR;
}
else array_num = array_i;
}
} // while
if ( array_num == -1 ) {
printf("\n\nMissing Levelnumber error in SaveShip...\n\nTerminating\n\n");
Terminate(ERR);
level_anz ++;
continue;
}
//--------------------
// Now comes the real saving part FOR ONE LEVEL. First THE LEVEL is packed into a string and
// then this string is wirtten to the file. easy. simple.
//
LevelMem = StructToMem(curShip.AllLevels[array_num]);
fwrite(LevelMem, strlen(LevelMem), sizeof(char), ShipFile);
free(LevelMem);
}
//--------------------
// Now we are almost done writing. Everything that is missing is
// the termination string for the ship file. This termination string
// is needed later for the ship loading functions to find the end of
// the data and to be able to terminate the long file-string with a
// null character at the right position.
//
fwrite( END_OF_SHIP_DATA_STRING , strlen( END_OF_SHIP_DATA_STRING ) , sizeof(char) , ShipFile );
fwrite( "\n\n ", strlen( "\n\n " ) , sizeof(char) , ShipFile );
DebugPrintf (2, "\nint SaveShip(char *shipname): now closing ship file...");
if( fclose(ShipFile) == EOF)
{
printf("\n\nClosing of ship file failed in SaveShip...\n\nTerminating\n\n");
Terminate(ERR);
return ERR;
}
DebugPrintf (2, "\nint SaveShip(char *shipname): end of function reached.");
return OK;
} /* SaveShip */
/*@Function============================================================
* @Desc: Level LevelToStruct(char *data):
* This function is for LOADING map data!
* This function extracts the data from *data and writes them
* into a Level-struct:
*
* Doors and Waypoints Arrays are initialized too
*
* @Ret: Level or NULL
* $Function----------------------------------------------------------*/
Level
LevelToStruct (char *data)
{
Level loadlevel;
char *map_begin, *wp_begin, *level_end;
char *this_line, *next_line;
char *pos;
int i;
int nr, x, y;
int k;
int connection;
char* DataPointer;
int res;
int tmp;
/* Get the memory for one level */
loadlevel = (Level) MyMalloc (sizeof (level));
loadlevel->empty = FALSE;
DebugPrintf (2, "\n-----------------------------------------------------------------");
DebugPrintf (2, "\nStarting to process information for another level:\n");
/* Read Header Data: levelnum and x/ylen */
DataPointer = strstr( data , "Levelnumber:" );
if ( DataPointer == NULL )
{
DebugPrintf( 0 , "No Levelnumber entry found! Terminating! ");
Terminate(ERR);
}
sscanf ( DataPointer , "Levelnumber: %u \n xlen of this level: %u \n ylen of this level: %u \n color of this level: %u",
&(loadlevel->levelnum), &(loadlevel->xlen),
&(loadlevel->ylen), &(loadlevel->color));
DebugPrintf( 2 , "\nLevelnumber : %d ", loadlevel->levelnum );
DebugPrintf( 2 , "\nxlen of this level: %d ", loadlevel->xlen );
DebugPrintf( 2 , "\nylen of this level: %d ", loadlevel->ylen );
DebugPrintf( 2 , "\ncolor of this level: %d ", loadlevel->ylen );
loadlevel->Levelname = ReadAndMallocStringFromData ( data , LEVEL_NAME_STRING , "\n" );
loadlevel->Background_Song_Name = ReadAndMallocStringFromData ( data , BACKGROUND_SONG_NAME_STRING , "\n" );
loadlevel->Level_Enter_Comment = ReadAndMallocStringFromData ( data , LEVEL_ENTER_COMMENT_STRING , "\n" );
// find the map data
if ((map_begin = strstr (data, MAP_BEGIN_STRING)) == NULL)
return(NULL);
/* set position to Waypoint-Data */
if ((wp_begin = strstr (data, WP_BEGIN_STRING)) == NULL)
return(NULL);
// find end of level-data
if ((level_end = strstr (data, LEVEL_END_STRING)) == NULL)
return(NULL);
/* now scan the map */
next_line = map_begin;
this_line = strtok (next_line, "\n");
/* read MapData */
for (i = 0; i < loadlevel->ylen; i++)
{
if ((this_line = strtok (NULL, "\n")) == NULL)
return(NULL);
loadlevel->map[i] = MyMalloc( loadlevel->xlen * sizeof( loadlevel->map[0][0] ) );
pos = this_line;
pos += strspn (pos, WHITE_SPACE); // skip initial whitespace
for (k=0; k < loadlevel->xlen; k++)
{
if (*pos == '\0')
return (NULL);
res = sscanf (pos, "%d", &tmp);
*(loadlevel->map[i]+k) = (char)tmp;
if ( (res == 0) || (res == EOF) )
return (NULL);
pos += strcspn (pos, WHITE_SPACE); // skip last token
pos += strspn (pos, WHITE_SPACE); // skip initial whitespace of next one
}
}
/* Get Waypoints */
next_line = wp_begin;
this_line = strtok (next_line, "\n");
for (i=0; i<MAXWAYPOINTS ; i++)
{
if ( (this_line = strtok (NULL, "\n")) == NULL)
return (NULL);
if (this_line == level_end)
{
loadlevel->num_waypoints = i;
break;
}
sscanf( this_line , "Nr.=%d \t x=%d \t y=%d" , &nr , &x , &y );
loadlevel->AllWaypoints[i].x=x;
loadlevel->AllWaypoints[i].y=y;
pos = strstr (this_line, CONNECTION_STRING);
pos += strlen (CONNECTION_STRING); // skip connection-string
pos += strspn (pos, WHITE_SPACE); // skip initial whitespace
for ( k=0 ; k<MAX_WP_CONNECTIONS ; k++ )
{
if (*pos == '\0')
break;
res = sscanf( pos , "%d" , &connection );
if ( (connection == -1) || (res == 0) || (res == EOF) )
break;
loadlevel->AllWaypoints[i].connections[k]=connection;
pos += strcspn (pos, WHITE_SPACE); // skip last token
pos += strspn (pos, WHITE_SPACE); // skip initial whitespace for next one
} // for k < MAX_WP_CONNECTIONS
loadlevel->AllWaypoints[i].num_connections = k;
} // for i < MAXWAYPOINTS
return (loadlevel);
} /* LevelToStruct */
/*@Function============================================================
@Desc: GetDoors: initializes the Doors array of the given level structure
Of course the level data must be in the structure already!!
@Ret: Number of doors found or ERR
* $Function----------------------------------------------------------*/
int
GetDoors (Level Lev)
{
int i, line, col;
int xlen, ylen;
int curdoor = 0;
char brick;
xlen = Lev->xlen;
ylen = Lev->ylen;
/* init Doors- Array to 0 */
for (i = 0; i < MAX_DOORS_ON_LEVEL; i++)
Lev->doors[i].x = Lev->doors[i].y = -1;
/* now find the doors */
for (line = 0; line < ylen; line++)
{
for (col = 0; col < xlen; col++)
{
brick = Lev->map[line][col];
// if (brick == '=' || brick == '"')
if ( brick == V_ZUTUERE || brick == H_ZUTUERE )
{
Lev->doors[curdoor].x = col;
Lev->doors[curdoor++].y = line;
if (curdoor > MAX_DOORS_ON_LEVEL)
{
fprintf(stderr, "\n\
\n\
----------------------------------------------------------------------\n\
Freedroid has encountered a problem:\n\
The number of doors found in level %d seems to be greater than the number\n\
of doors currently allowed in a freedroid map.\n\
\n\
The constant for the maximum number of doors currently is set to %d in the\n\
freedroid defs.h file. You can enlarge the constant there, then start make\n\
and make install again, and the map will be loaded without complaint.\n\
\n\
The constant in defs.h is names 'MAX_DOORS_ON_LEVEL'. If you received this \n\
message, please also tell the developers of the freedroid project, that they\n\
should enlarge the constant in all future versions as well.\n\
\n\
Thanks a lot.\n\
\n\
But for now Freedroid will terminate to draw attention to this small map problem.\n\
Sorry...\n\
----------------------------------------------------------------------\n\
\n" , Lev->levelnum , MAX_DOORS_ON_LEVEL );
Terminate(ERR);
}
} /* if */
} /* for */
} /* for */
return curdoor;
} /* GetDoors */
/*@Function============================================================
@Desc: This function initialized the array of Refreshes for animation
within the level
@Ret: Number of refreshes found or ERR
* $Function----------------------------------------------------------*/
int
GetRefreshes (Level Lev)
{
int i, row, col;
int xlen, ylen;
int curref = 0;
xlen = Lev->xlen;
ylen = Lev->ylen;
/* init refreshes array to -1 */
for (i = 0; i < MAX_REFRESHES_ON_LEVEL; i++)
Lev->refreshes[i].x = Lev->refreshes[i].y = -1;
/* now find all the refreshes */
for (row = 0; row < ylen; row++)
for (col = 0; col < xlen; col++)
{
if (Lev->map[row][col] == REFRESH1 )
{
Lev->refreshes[curref].x = col;
Lev->refreshes[curref++].y = row;
if (curref > MAX_REFRESHES_ON_LEVEL)
{
fprintf(stderr, "\n\
\n\
----------------------------------------------------------------------\n\
Freedroid has encountered a problem:\n\
The number of refreshes found in level %d seems to be greater than the number\n\
of refreshes currently allowed in a freedroid map.\n\
\n\
The constant for the maximum number of refreshes currently is set to %d in the\n\
freedroid defs.h file. You can enlarge the constant there, then start make\n\
and make install again, and the map will be loaded without complaint.\n\
\n\
The constant in defs.h is names 'MAX_REFRESHES_ON_LEVEL'. If you received this \n\
message, please also tell the developers of the freedroid project, that they\n\
should enlarge the constant in all future versions as well.\n\
\n\
Thanks a lot.\n\
\n\
But for now Freedroid will terminate to draw attention to this small map problem.\n\
Sorry...\n\
----------------------------------------------------------------------\n\
\n" , Lev->levelnum , MAX_REFRESHES_ON_LEVEL );
Terminate(ERR);
return ERR;
}
} /* if */
} /* for */
return curref;
} // int GetRefreshed(Level lev)
//----------------------------------------------------------------------
// Find all alerts on this level and initialize their position-array
//----------------------------------------------------------------------
void
GetAlerts (Level Lev)
{
int i, row, col;
int xlen, ylen;
int curref = 0;
xlen = Lev->xlen;
ylen = Lev->ylen;
// init alert array to -1
for (i = 0; i < MAX_ALERTS_ON_LEVEL; i++)
Lev->alerts[i].x = Lev->alerts[i].y = -1;
// now find all the alerts
for (row = 0; row < ylen; row++)
for (col = 0; col < xlen; col++)
{
if (Lev->map[row][col] == ALERT_GREEN)
{
Lev->alerts[curref].x = col;
Lev->alerts[curref++].y = row;
if (curref > MAX_ALERTS_ON_LEVEL)
{
DebugPrintf(0, "WARNING: more alert-tiles found on level %d than allowed (%d)!!",
Lev->levelnum, MAX_ALERTS_ON_LEVEL);
DebugPrintf(0, "Remaining Alerts will be inactive... \n");
break;
}
} // if alert found
} // for cols
return;
} // int GetAlerts()
/*======================================================================
IsWallBlock(): Returns TRUE (1) for blocks classified as "Walls",
0 otherwise
======================================================================*/
int
IsWallBlock (int block)
{
switch (block)
{
case KREUZ:
case H_WALL:
case V_WALL:
case H_ZUTUERE:
case V_ZUTUERE:
case ECK_LU:
case T_U:
case ECK_RU:
case T_L:
case T_R:
case ECK_LO:
case T_O:
case ECK_RO:
return (TRUE);
default:
return (FALSE);
} // switch
} // IsWallBlock()
/*----------------------------------------------------------------------
* close all doors and set refreshes to first phase for "canonical map"
*
----------------------------------------------------------------------*/
void
ResetLevelMap (Level Lev)
{
int col;
int i;
// Now in the game and in the level editor, it might have happend that some open
// doors occur. The make life easier for the saving routine, these doors should
// be closed first.
for (col=0; col < Lev->xlen; col++)
{
for(i=0; i< Lev->ylen; i++)
{
switch ( Lev->map[i][col] )
{
case V_ZUTUERE:
case V_HALBTUERE1:
case V_HALBTUERE2:
case V_HALBTUERE3:
case V_GANZTUERE:
Lev->map[i][col]=V_ZUTUERE;
break;
case H_ZUTUERE:
case H_HALBTUERE1:
case H_HALBTUERE2:
case H_HALBTUERE3:
case H_GANZTUERE:
Lev->map[i][col]=H_ZUTUERE;
break;
case REFRESH1:
case REFRESH2:
case REFRESH3:
case REFRESH4:
Lev->map[i][col]=REFRESH1;
break;
case ALERT_GREEN:
case ALERT_YELLOW:
case ALERT_AMBER:
case ALERT_RED:
Lev->map[i][col] = ALERT_GREEN;
break;
default:
break;
}
}
}
return;
} // ResetLevelMap
/*-----------------------------------------------------------------
* @Desc: initialize doors, refreshes and lifts for the given level-data
*
* @Ret: OK | ERR
*
*-----------------------------------------------------------------*/
int
InterpretMap (Level Lev)
{
/* Get Doors Array */
GetDoors ( Lev );
// Get Refreshes
GetRefreshes ( Lev );
// Get Alerts
GetAlerts (Lev);
return(OK);
}
/*@Function============================================================
@Desc: GetLiftConnections(char *ship): loads lift-connctions
to cur-ship struct
@Ret: OK | ERR
@Int:
* $Function----------------------------------------------------------*/
int
GetLiftConnections (char *filename)
{
char *fpath;
char *Data;
char *EntryPointer;
int i;
int Label;
int DeckIndex;
int RectIndex;
int ElevatorIndex;
Lift CurLift;
int x,y,w,h;
#define END_OF_LIFT_DATA_STRING "*** End of elevator specification file ***"
#define START_OF_LIFT_DATA_STRING "*** Beginning of Lift Data ***"
#define START_OF_LIFT_RECTANGLE_DATA_STRING "*** Beginning of elevator rectangles ***"
#define END_OF_LIFT_CONNECTION_DATA_STRING "*** End of Lift Connection Data ***"
/* Now get the lift-connection data from "FILE.elv" file */
fpath = find_file (filename, MAP_DIR, NO_THEME, CRITICAL);
Data = ReadAndMallocAndTerminateFile( fpath , END_OF_LIFT_DATA_STRING ) ;
/*
if ( (EntryPointer = strstr( Data , START_OF_LIFT_RECTANGLE_DATA_STRING ) ) == NULL )
{
DebugPrintf ( 0 , "\nERROR! START OF LIFT RECTANGLE DATA STRING NOT FOUND! Terminating...");
Terminate(ERR);
}
*/