File: levels.c

package info (click to toggle)
gnurobbo 0.68+dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,212 kB
  • sloc: ansic: 21,128; sh: 301; makefile: 185; awk: 98
file content (1236 lines) | stat: -rw-r--r-- 35,870 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
/*  GNU Robbo
 *  Copyright (C) 2002-2010 The GNU Robbo Team (see AUTHORS).
 *
 *  GNU Robbo 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, or (at your option)
 *  any later version.
 *
 *  GNU Robbo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the impled 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 GNU CC; see the file COPYING. If not, write to the
 *  Free Software Foundation, 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *
 */

#include "game.h"

/* Defines */
/*
#define DEBUG_LOAD_LEVEL_DATA
*/

#define DELAY_LEVEL_INIT_SCROLL (2 * game_cycle_limit)

/* Variables */
extern char *data_state[];

/* Function prototypes */
int transform_char (char c);


/***************************************************************************
 * Level Initialisation                                                    *
 ***************************************************************************/
/* Initialises a level (and Robbo) and reads level data from a dat file.
 * 
 *  On exit: returns 0 if successful
 *           returns 1 if level not found */

int level_init (void)
{
	int retval = FALSE;

	/* Initialise Robbo */
	init_robbo ();

	/* Initialise the level */
	level.w = 0;
	level.h = 0;
	level.author[0] = 0;
	level.notes[0] = 0;
	level.now_is_blinking = 0;
	/* This is Uint32 so -1 == 0xffffffff and I won't be using that colour */
	level.colour = UNDEFINED;
	/* This is Uint32 so -1 == 0xffffffff and I won't be using that colour */
	level.colour_override = UNDEFINED;
	/* Reset the pointer controls shoot state */
	pointer_controls.shoot_state = POINTER_CONTROLS_SHOOT_OFF;

	/* Clear board */
	clear_entire_board ();

	/* Attempt to load the level */
	if (load_level_data (level_packs[selected_pack].level_selected))
	{
		retval = TRUE;
	}
	else
	{
		read_skin_level_colour_override ();

		#ifdef DEBUG_LOAD_LEVEL_DATA
			printf ("*** Start %s ***\n", __func__);
			printf ("level.colour=%08X\n", level.colour);
			printf ("level.colour_override=%08X\n", level.colour_override);
			printf ("*** Stop %s ***\n", __func__);
		#endif

		#ifdef DEBUG_COLOUR_SELECT
			debug_colour_select_r = (level.colour_override >> 16) & 0xff;
			debug_colour_select_g = (level.colour_override >> 8) & 0xff;
			debug_colour_select_b = level.colour_override & 0xff;
		#endif

		/* This is only going to happen on levels with no screws */
		if (robbo.screws == 0) open_exit ();

		/* Set the id_questionmark property for all questionmarks */
		init_questionmarks ();

		/* Set the size of the viewport */
		/* Set viewport width */
		if (level.w >= viewport.max_w && !viewport.maximise)
		{
			viewport.w = viewport.max_w;
		}
		else
		{
			viewport.w = level.w;
		}
#ifdef _SMOOTH_SCRL_
		viewport.xs=0;
		viewport.ys=0;
#endif
		
		
		/* Now adjust it to fit within the dimensions of the screen */
		if (viewport.w > screen->w / video.field_size)
			viewport.w = screen->w / video.field_size;
		/* Set the xoffset and centre the viewport */
		viewport.xoffset = (screen->w - viewport.w *
			video.field_size) / 2 + video.xshift;

		/* Set viewport height */
		if (level.h >= viewport.max_h && !viewport.maximise)
		{
			viewport.h = viewport.max_h;
		}
		else
		{
			viewport.h = level.h;
		}
		/* Now adjust it to fit within the dimensions of the screen */
		if (viewport.h > screen->h / video.field_size - 3)
			viewport.h = screen->h / video.field_size - 3;
		/* Set the yoffset and centre the viewport */
		viewport.yoffset = (screen->h - viewport.h *
			video.field_size) / 2 + video.yshift;

		/* Set the scoreline screen offset */
		scoreline.xoffset = (screen->w - video.field_size *
			DEFAULT_VIEWPORT_WIDTH) / 2 + video.xshift;
		/* A special case when screen->w = 240 or 480 */
		if (scoreline.xoffset < 0) scoreline.xoffset = 0;
		scoreline.yoffset = screen->h / 2 + video.field_size *
			viewport.h / 2 + video.field_size / 4 + video.yshift;

		/* Set the authorline screen offset */
		authorline.xoffset = scoreline.xoffset + 2 *
			video.field_size / 16;
		authorline.yoffset = screen->h / 2 - video.field_size *
			viewport.h / 2 - video.field_size -
			video.field_size / 4 + video.yshift;

		/* Start scrolling from the opposite end of the board to Robbo */
		if (robbo.x < level.w / 2)
		{
			viewport.x = level.w - viewport.w;
		}
		else
		{
			viewport.x = 0;
		}
		if (robbo.y < level.h / 2)
		{
			viewport.y = level.h - viewport.h;
		}
		else
		{
			viewport.y = 0;
		}
		viewport.cycles_to_dest = DELAY_LEVEL_INIT_SCROLL;
#ifdef _SMOOTH_SCRL_
		viewport.x=viewport.x*SCRL_MULT2;
		viewport.y=viewport.y*SCRL_MULT2;
#endif
		
		#ifdef DEBUG_RECORD_DEMO
			demo_mode (DEMO_MODE_RECORD_INITIALISE, 0);
		#endif
	}

	return retval;
}

/***************************************************************************
 * Create User Pack                                                        *
 ***************************************************************************/
/* This will create the default user level pack for use with the designer
 * (it needs to be writeable and so its location is platform dependent).
 * If the target platform requires a local folder structure to accommodate
 * this file then it will be created too, otherwise the user pack will be
 * created within the normal location of PACKAGE_DATA_DIR LEVELS_DIR */

void create_userpack (void)
{
	FILE *fp;
	char fullpath[256];
	#if defined(PLATFORM_WIN32)
	#elif defined(PLATFORM_PC) || defined(PLATFORM_ZAURUS) || defined(PLATFORM_FREMANTLE)
		char foldername[256];
	#elif defined(PLATFORM_GP2X)  || defined(PLATFORM_CAANOO)
	#elif defined(PLATFORM_PSP)
	#endif

	/* Build a platform dependent full path to the DEFAULT_USER_LEVEL_PACK */
	fullpath[0] = 0;
	#if defined(PLATFORM_WIN32)
	#elif defined(PLATFORM_PC) || defined(PLATFORM_ZAURUS) || defined(PLATFORM_FREMANTLE)
		strcpy (fullpath, getenv ("HOME"));
		strcat (fullpath, "/" LOCAL_DATA_DIR "/" LEVELS_DIR "/" DEFAULT_USER_LEVEL_PACK);
	#elif defined(PLATFORM_GP2X) || defined(PLATFORM_PSP)  || defined(PLATFORM_CAANOO)
		strcpy (fullpath, PACKAGE_DATA_DIR "/" LEVELS_DIR "/" DEFAULT_USER_LEVEL_PACK);
	#endif
	
	if (fullpath[0] != 0)
	{
		/* Attempt to open the DEFAULT_USER_LEVEL_PACK to test its presence */
		if ((fp = fopen (fullpath, "r")) == NULL)
		{
			/* Failure, so attempt to create everything necessary */
			fprintf (stdout, "Creating %s\n", fullpath);

			/* Create directory structure ignoring errors */
			#if defined(PLATFORM_WIN32)
			#elif defined(PLATFORM_PC) || defined(PLATFORM_ZAURUS) || defined(PLATFORM_FREMANTLE)
				strcpy (foldername, getenv ("HOME"));
				strcat (foldername, "/" LOCAL_DATA_DIR);
				mkdir (foldername, 0755);
				strcat (foldername, "/" LEVELS_DIR);
				mkdir (foldername, 0755);
			#elif defined(PLATFORM_GP2X)  || defined(PLATFORM_CAANOO)
			#elif defined(PLATFORM_PSP)
			#endif

			/* Create and populate the DEFAULT_USER_LEVEL_PACK */
			if ((fp = fopen (fullpath, "w")) == NULL)
			{
		      fprintf (stdout, "Cannot write to file: %s\n", fullpath);
			}
			else 
			{
				fprintf (fp, "%s\n",
					"[name]\nMyLevels\n\n"
					"[last_level]\n1\n\n"
					"[default_level_colour]\n608050\n\n"
					"[notes]\nDefault user level pack for use with the designer.\n\n\n"
					"[level]\n1\n"
					"[colour]\n608050\n"
					"[size]\n16.31\n"
					"[author]\n"
					"[level_notes]\n"
					"[data]\n"
					"................\n"
					".R..............\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"........T.......\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"................\n"
					"..............!.\n"
					"................\n"
					"[additional]\n0\n"
					"[end]\n\n");
				fclose (fp);
			}
		}
		else 
		{
			fclose (fp);
		}
	}
}

/***************************************************************************
 * Find All DAT Files                                                      *
 ***************************************************************************/
/* This will search either one or two locations (platform dependent) for dat
 * files. Searching PACKAGE_DATA_DIR LEVELS_DIR is mandatory but if the
 * LOCAL_DATA_DIR LEVELS_DIR needs searching too then this should be set-up
 * below by the maintainer of the particular build.
 * On exit: returns 1 on error */

int
find_all_dat_files (void)
{
	DIR *dir;
	struct dirent *currentdirent;
	int default_dat_found = FALSE;
	char foldername[256];
	int count;

	#if defined(PLATFORM_WIN32)
		for (count = 0; count < 1; count++)
	#elif defined(PLATFORM_PC) || defined(PLATFORM_ZAURUS) || defined(PLATFORM_FREMANTLE)
		for (count = 0; count < 2; count++)
	#elif defined(PLATFORM_GP2X) || defined(PLATFORM_PSP)  || defined(PLATFORM_CAANOO)
		for (count = 0; count < 1; count++)
	#endif
	{
		if (count == 0)
		{
			strcpy (foldername, PACKAGE_DATA_DIR "/" LEVELS_DIR);
		}
		else if (count == 1)
		{
			#if defined(PLATFORM_WIN32)
			#elif defined(PLATFORM_PC) || defined(PLATFORM_ZAURUS) || defined(PLATFORM_FREMANTLE)
				strcpy (foldername, getenv ("HOME"));
				strcat (foldername, "/" LOCAL_DATA_DIR "/" LEVELS_DIR);
			#elif defined(PLATFORM_GP2X)  || defined(PLATFORM_CAANOO)
			#elif defined(PLATFORM_PSP)
			#endif
		}

		/* Open the installation or current directory */
		if ((dir = opendir (foldername)) != NULL)
		{
			/* Now search the directory for dat files */
			while ((currentdirent = readdir (dir)) != NULL)
			{
                                #if defined(PLATFORM_PSP)
                                	if ((strstr (currentdirent->d_name, ".dat") != NULL) || (strstr (currentdirent->d_name, ".DAT") != NULL))
                                #else
                                	if (strstr (currentdirent->d_name, ".dat") != NULL)
                                #endif
				{
					if (level_pack_count < MAX_LEVEL_PACKS)
					{
                                                #if defined(PLATFORM_PSP)
                                                      if (strcasecmp (currentdirent->d_name, DEFAULT_LEVEL_PACK) == 0)
                                                #else
                                                      if (strcmp (currentdirent->d_name, DEFAULT_LEVEL_PACK) == 0)        
                                                #endif
						{
							default_dat_found = TRUE;
						}
						else
						{
							strcpy (level_packs[level_pack_count].filename,
								foldername);
							strcat (level_packs[level_pack_count].filename,
								"/");
							strcat (level_packs[level_pack_count].filename,
								currentdirent->d_name);
							level_pack_count++;
							found_pack_count++;
						}
					}
				}
			}
			closedir (dir);
		}
		else
		{
			fprintf (stdout, "Couldn't open levels folder: %s\n", foldername);
		}
	}

	if (!default_dat_found)
	{
		fprintf (stdout, "Cannot find the default level file: %s\n",
			PACKAGE_DATA_DIR "/" LEVELS_DIR "/" DEFAULT_LEVEL_PACK);
		return 1;
	}

	return 0;
}

/***************************************************************************
 * Read Level Packs                                                        *
 ***************************************************************************/
/* Reads all level packs and populates the level pack list */

void
read_level_packs (void)
{
	FILE *fp;
	char line[1024];
	char filename[256];
	int name_found, last_level_found, count, count2;

	for (count = 0; count < level_pack_count; count++)
	{
		strcpy (filename, level_packs[count].filename);
		if ((fp = fopen (filename, "r")) == NULL)
		{
			fprintf (stdout, "Cannot read from file %s\n", filename);
			/* Set everything to uninitialised */
			strcpy (level_packs[count].name, SINGLE_SPACE);
			level_packs[count].last_level = DEFAULT_LEVEL_START;
			level_packs[count].level_reached = DEFAULT_LEVEL_START;
			level_packs[count].level_selected = DEFAULT_LEVEL_START;
			level_packs[count].selected = FALSE;
		}
		else
		{
			name_found = last_level_found = FALSE;
			while ((fgets (line, 1024, fp)) != NULL)
			{
				if (!strncmp (line, "[name]", 6))
				{
					fgets (line, 1024, fp);
					/* fgets will get [CR]LFs too so they should be removed */
					for (count2 = strlen (line) - 1; count2 >= 0; count2--)
						if (line[count2] == 13 || line[count2] == 10)
							line[count2] = 0;
					strncpy (level_packs[count].name, line, 20);
					level_packs[count].name[19] = 0;	/* Just in case we read all chars */
					name_found = TRUE;
				}
				else if (!strncmp (line, "[last_level]", 12))
				{
					fgets (line, 1024, fp);
					sscanf (line, "%i", &level_packs[count].last_level);
					last_level_found = TRUE;
				}
				if (name_found && last_level_found)
					break;
			}
			if (!name_found || !last_level_found)
			{
				if (!name_found)
				{
					fprintf (stdout, "[name] is missing from %s\n", filename);
					/* Default the name */
					strcpy (level_packs[count].name, SINGLE_SPACE);
				}
				if (!last_level_found)
				{
					fprintf (stdout, "[last_level] is missing from %s\n",
						filename);
					/* Default last_level */
					level_packs[count].last_level = DEFAULT_LEVEL_START;
				}
			}
			/* Default the rest */
			level_packs[count].level_reached = DEFAULT_LEVEL_START;
			level_packs[count].level_selected = DEFAULT_LEVEL_START;
			if (count)
				level_packs[count].selected = FALSE;
			/* Dump info to the console */
			fprintf (stdout, "Level pack found: %s with %i levels\n",
				level_packs[count].name, level_packs[count].last_level);
			fclose (fp);
		}
	}
}

/***************************************************************************
 * Sort Level Packs                                                        *
 ***************************************************************************/
/* Sorts found level packs alphanumerically by name remembering to
 * preserve the selected pack :) This must be done last after
 * find_all_dat_files(), read_level_packs() and read_resource_file() */

void sort_level_packs (void)
{
	struct pack swap_pack;
	int swapped, count;

	/* Bubble sort found level packs by name */
	do
	{
		swapped = FALSE;
		for (count = 0; count < found_pack_count - 1; count++)
		{
			if (strcmp (level_packs[count].name, level_packs[count + 1].name) > 0)
			{
				swapped = TRUE;
				swap_pack = level_packs[count + 1];
				level_packs[count + 1] = level_packs[count];
				level_packs[count] = swap_pack;
			}
		}
	}
	while (swapped);

	/* Find the selected pack */
	for (count = 0; count < found_pack_count; count++)
	{
		if (level_packs[count].selected)
			selected_pack = count;
	}
}

int create_wall(int type,int x,int y)
{
    switch (type)
    {
    case WALL_RED:
	create_object (x, y, WALL);
	board[x][y].state = 1;
	break;
    case WALL_GREEN:
	create_object (x, y, WALL);
	board[x][y].state = 2;
	break;
    case BLACK_WALL:
	create_object (x, y, WALL);
	board[x][y].state = 3;
	break;
    case FAT_WALL:
	create_object (x, y, WALL);
	board[x][y].state = 4;
	break;
    case ROUND_WALL:
	create_object (x, y, WALL);
	board[x][y].state = 5;
	break;
    case BOULDER_WALL:
	create_object (x, y, WALL);
	board[x][y].state = 6;
	break;
    case SQUARE_WALL:
	create_object (x, y, WALL);
	board[x][y].state = 7;
	break;
    case LATTICE_WALL:
	create_object (x, y, WALL);
	board[x][y].state = 8;
	break;
    case S_WALL:
	create_object(x,y,WALL);
	board[x][y].state = 9;
	break;
    }
    return 0;
    }


/***************************************************************************
 * Load Level Data                                                         *
 ***************************************************************************/
/* This function is called every time we need a new level.
 * It has been rewritten to be more tolerant of incomplete data and to
 * display warnings via the console when something is missing.
 * 
 * On entry: level_number is a level within the currently selected pack
 *  On exit: returns 0 if successful
 *           returns 1 if level not found */

int load_level_data (int level_number)
{
	FILE *fp;
	/* line[1024] because if fgets returns a partial line
	 * then line_count will be incremented again */
	char filename[256], symbol, line[1024];
	int count, line_count = 0, x, y = 0, rows = UNDEFINED;
	int additional_count = UNDEFINED, value_count;
	int value_read[16];
	int default_level_colour_read = DATA_UNREAD;
	int level_read = DATA_UNREAD;
	int colour_read = DATA_UNREAD;
	int size_read = DATA_UNREAD;
	int author_read = DATA_UNREAD;
	int data_read = DATA_UNREAD;
	int additional_read = DATA_UNREAD;
	int notes_read = DATA_UNREAD;
	int screws_read = DATA_UNREAD;
	int screws_to_set=-1;
//	int temp_symbol;
	/*int screws_read= DATA_UNREAD;	Thunor: redundant: not used - neurocyp:
	 * we want to have different number of screws, than default, some levels
	 * in robbo have different number for level, than real screws number */

	/* Attempt to open the level pack */
	strcpy (filename, level_packs[selected_pack].filename);
	if ((fp = fopen (filename, "r")) == NULL)
	{
		fprintf (stdout, "Cannot read from file: %s\n", filename);
		return TRUE;
	}

	#ifdef DEBUG_LOAD_LEVEL_DATA
		printf ("*** Start %s ***\n", __func__);
	#endif

	/* Read lines including the [CR]LFs until we have what we need
	 * or reach the end of file (line == NULL).
	 * We are looking for some or all of these things :-
	 * [default_level_colour] - OPTIONAL
	 * [level]
	 * [colour] - OPTIONAL
	 * [size]
	 * [author] - OPTIONAL
	 * [level_notes] - OPTIONAL
	 * [data]
	 * [additional] - if not applicable, this may be missing
	 * [end] */
	while ((fgets (line, 1024, fp)) != NULL)
	{
		line_count++;
		
		/* fgets will get [CR]LFs too so they should be removed */
		for (count = strlen (line) - 1; count >= 0; count--)
			if (line[count] == 13 || line[count] == 10) line[count] = 0;
			
		/* Use the tags as switches so we know what to read.
		 * NOTE: Reading consecutive (dataless) tags means that the
		 * previous tag may still be marked as DATA_READING */
		if ((!strncmp (line, "[default_level_colour]", 22)) ||
			(!strncmp (line, "[level]", 7) && level_read == DATA_UNREAD) ||
			(!strncmp (line, "[level_notes]", 13) && level_read == DATA_READ) ||
			(!strncmp (line, "[colour]", 8) && level_read == DATA_READ) ||
			(!strncmp (line, "[size]", 6) && level_read == DATA_READ) ||
			(!strncmp (line, "[author]", 8) && level_read == DATA_READ) ||
			(!strncmp (line, "[data]", 6) && level_read == DATA_READ) ||
			(!strncmp (line, "[additional]", 12) && level_read == DATA_READ) ||
			(!strncmp (line, "[screws]", 7) && level_read == DATA_READ) ||
			(!strncmp (line, "[end]", 5) && level_read == DATA_READ))
		{
			/* If a tag is being read and we read another tag then
			 * mark it as something else */
			if (notes_read == DATA_READING) 
			{
				notes_read = DATA_INCOMPLETE;
			} else if (screws_read == DATA_READING) 
			{
			    screws_read=DATA_INCOMPLETE;
			}
			else if (default_level_colour_read == DATA_READING)
			{
				default_level_colour_read = DATA_INCOMPLETE;
			}
			else if (level_read == DATA_READING)
			{
				level_read = DATA_UNREAD;	/* We need to keep reading these tags */
			}
			else if (colour_read == DATA_READING)
			{
				colour_read = DATA_INCOMPLETE;
			}
			else if (size_read == DATA_READING)
			{
				size_read = DATA_INCOMPLETE;
				fprintf (stdout, "%s:%i: Incomplete size data: width or "
					"height or both were found to be missing.\n",
					level_packs[selected_pack].filename, line_count);
			}
			else if (author_read == DATA_READING)
			{
				author_read = DATA_INCOMPLETE;
			}
			else if (data_read == DATA_READING)
			{
				data_read = DATA_INCOMPLETE;
				fprintf (stdout, "%s:%i: Insufficient data for declared "
					"height of %i.\n", level_packs[selected_pack].filename,
					line_count, level.h);
			}
			else if (additional_read == DATA_READING)
			{
				additional_read = DATA_INCOMPLETE;
				if (additional_count != UNDEFINED)
					fprintf (stdout, "%s:%i: Insufficient additional data "
						"objects for declared count of %i.\n",
						level_packs[selected_pack].filename, line_count,
						additional_count);
			}
			
			/* If the found tag is DATA_UNREAD then mark it for DATA_READING */
			if (!strncmp (line, "[default_level_colour]", 22))
			{
				if (default_level_colour_read == DATA_UNREAD)
					default_level_colour_read = DATA_READING;
			}
			else if (!strncmp (line, "[level]", 7))
			{
				if (level_read == DATA_UNREAD)
				{
					level_read = DATA_READING;
				}
				else if (level_read == DATA_READ)
				{
					break;	/* We've already read this; something is wrong */
				}
			}
			else if (!strncmp (line, "[colour]", 8))
			{
				if (colour_read == DATA_UNREAD) colour_read = DATA_READING;
			}
			else if (!strncmp (line, "[size]", 6))
			{
				if (size_read == DATA_UNREAD) size_read = DATA_READING;
			}
			else if (!strncmp (line, "[author]", 8))
			{
				if (author_read == DATA_UNREAD) author_read = DATA_READING;
			}
			else if (!strncmp (line, "[data]", 6))
			{
				if (data_read == DATA_UNREAD)
				{
					data_read = DATA_READING;
					/* This is a good place to reset variables
					 * before reading the data */
					y = 0;
				}
			}
			else if (!strncmp (line, "[additional]", 12))
			{
				if (additional_read == DATA_UNREAD)
				{
					additional_read = DATA_READING;
					additional_count = rows = UNDEFINED;
				}
			} else if (!strncmp(line,"[screws]",8)) {
			    screws_read=DATA_READING;
			}
			else if (!strncmp(line,"[level_notes]",13))
			{
				if (notes_read == DATA_UNREAD) notes_read = DATA_READING;
			}
			else  if (!strncmp (line, "[end]", 5))
			{
				break;		/* Stop reading the file now */
			}
		}
		else
		{
			/* It's not a tag but data (or newline) so we
			 * need to find what to do with it */
			if (notes_read == DATA_READING)
			{
				if (strlen(level.notes) < 1024 - 3)
				{
					/* Append some or all line data */
					count = 1024 - 3 - strlen(level.notes);
					if (strlen(line) <= count)
					{
						strcat (level.notes, line);
					}
					else
					{
						strncat (level.notes, line, count);
						/* Force termination when using strncat */
						level.notes[1024 - 1] = 0;
					}
					/* Enforce a newline else it causes problems.
					 * 3 chars have been reserved for buffer termination */
					strcat (level.notes, "\n");
				}
				else
				{
						notes_read = DATA_READ;
				}
				#ifdef DEBUG_LOAD_LEVEL_DATA
					printf ("%i: level_notes=\"%s\"\n", line_count, level.notes);
				#endif
			} else if (screws_read==DATA_READING) {
			    if(sscanf(line,"%i",&value_read[0])==1) {
				screws_to_set= value_read[0];
				screws_read=DATA_READ;
			    }
			}
			else if (default_level_colour_read == DATA_READING)
			{
				if (sscanf (line, "%x", &value_read[0]) == 1)
				{
					level.colour = value_read[0];
					default_level_colour_read = DATA_READ;
					#ifdef DEBUG_LOAD_LEVEL_DATA
						printf ("%i: default_level_colour=0x%06X\n",
							line_count, value_read[0]);
					#endif
				}
				else
				{
					/* Read once, found to be incomplete, the end :) */
					default_level_colour_read = DATA_INCOMPLETE;
				}
			}
			else if (level_read == DATA_READING)
			{
				if (sscanf (line, "%i", &value_read[0]) == 1)
				{
					if (value_read[0] == level_number)
					{
						level_read = DATA_READ;
						#ifdef DEBUG_LOAD_LEVEL_DATA
							printf ("%i: level=%i\n", line_count, value_read[0]);
						#endif
					}
					else
					{
						level_read = DATA_UNREAD;
					}
				}
				else
				{
					level_read = DATA_UNREAD;	/* We need to keep reading these tags */
				}
			}
			else if (colour_read == DATA_READING)
			{
				if (sscanf (line, "%x", &value_read[0]) == 1)
				{
					level.colour = value_read[0];
					colour_read = DATA_READ;
					#ifdef DEBUG_LOAD_LEVEL_DATA
						printf ("%i: colour=0x%06X\n", line_count, value_read[0]);
					#endif
				}
				else
				{
					colour_read = DATA_INCOMPLETE;
				}
			}
			else if (size_read == DATA_READING)
			{
				if (sscanf (line, "%i.%i", &value_read[0], &value_read[1]) == 2)
				{
					if ((value_read[0] > 0 && value_read[0] <= MAX_W) &&
						(value_read[1] > 0 && value_read[1] <= MAX_H))
					{
						level.w = value_read[0];
						level.h = value_read[1];
						size_read = DATA_READ;
						#ifdef DEBUG_LOAD_LEVEL_DATA
							printf ("%i: size w=%i h=%i\n", line_count,
								value_read[0], value_read[1]);
						#endif
					}
					else
					{
						size_read = DATA_INCOMPLETE;
						fprintf (stdout, "%s:%i: Invalid size data: width "
							"must be <= %i and height <= %i.\n",
							level_packs[selected_pack].filename,
							line_count, MAX_W, MAX_H);
					}
				}
				else
				{
					size_read = DATA_INCOMPLETE;
					fprintf (stdout, "%s:%i: Incomplete size data: width "
						"or height or both were found to be missing.\n",
						level_packs[selected_pack].filename, line_count);
				}
			}
			else if (author_read == DATA_READING)
			{
				strncpy (level.author, line, 60);	/* Prevent overruns */
				level.author[59] = 0;	/* Just in case we read all chars */
				author_read = DATA_READ;
				#ifdef DEBUG_LOAD_LEVEL_DATA
					printf ("%i: author=%s\n", line_count, line);
				#endif
			}
			else if (data_read == DATA_READING)
			{
				/* Only the quantity of data declared by size must be read.
				 * x and y will be used exclusively here whilst reading the data */
				x = 0;
				while (TRUE)
				{
					if (line[x] == 0)
					{		/* No data, end of string */
						data_read = DATA_INCOMPLETE;
						fprintf (stdout, "%s:%i: Insufficient data for "
							"declared width of %i.\n",
							level_packs[selected_pack].filename,
							line_count, level.w);
						break;
					}
					else
					{
						board[x][y].type = transform_char (line[x]);
						create_object (x, y, board[x][y].type);
						switch (board[x][y].type)
						{
							case -1:   // here we add support for more complicated type, of course in this place, we do not do anything
								break;
							case SCREW:
								if(screws_read==DATA_READ) {
								robbo.screws=screws_to_set;
								} else {
								    robbo.screws++;
								}
								break;
							case WALL_RED:
							case WALL_GREEN:
							case BLACK_WALL:
							case FAT_WALL:
							case ROUND_WALL:
							case BOULDER_WALL:
							case SQUARE_WALL:
							case LATTICE_WALL:
							case S_WALL:
							    create_wall(board[x][y].type,x,y);
							    break;
						}
						#ifdef DEBUG_LOAD_LEVEL_DATA
							printf ("%c", line[x]);
						#endif
					}
					if (++x >= level.w) break;
				}
				#ifdef DEBUG_LOAD_LEVEL_DATA
					printf ("\n");
				#endif
				if (++y >= level.h) data_read = DATA_READ;
			}
			else if (additional_read == DATA_READING)
			{
				/* rows is used exclusively here whilst reading the data.
				 * Are we reading the additional count? */
				if (additional_count == UNDEFINED)
				{
					/* If we read 2 then it's a data object and not a count */
					if (sscanf (line, "%i.%i", &value_read[0], &value_read[1]) == 1)
					{
						if (value_read[0] > 0)
						{
							additional_count = rows = value_read[0];
							#ifdef DEBUG_LOAD_LEVEL_DATA
								printf ("%i: additional=%i\n", line_count,
									value_read[0]);
							#endif
						}
						else
						{
							additional_read = DATA_INCOMPLETE;
						}
					}
					else
					{
						additional_read = DATA_INCOMPLETE;
						fprintf (stdout, "%s:%i: The additional data object "
							"count is missing. This should be directly below "
							"the [additional] tag.\n",
							level_packs[selected_pack].filename, line_count);
					}
				}
				else
				{
					/* It's not the additional count,
					 * so read the additional data objects */
					value_count = sscanf (line, "%i.%i.%c.%i.%i.%i.%i.%i.%i.%i.%i.%i.%i.%i",
						&x, &y, &symbol, &value_read[0], &value_read[1],
						&value_read[2], &value_read[3], &value_read[4],
						&value_read[5],&value_read[6],&value_read[7],
						&value_read[8],&value_read[9],&value_read[10]
						);
					#ifdef DEBUG_LOAD_LEVEL_DATA
						printf ("%i: %s\n", line_count, line);
					#endif
					/* Validate the values read */
					if (value_count <= 0)
					{
						additional_read = DATA_INCOMPLETE;
						fprintf (stdout, "%s:%i: Insufficient additional "
							"data objects for declared count of %i.\n",
							level_packs[selected_pack].filename,
							line_count, additional_count);
					}
					else if (value_count < 4)
					{
						fprintf (stdout, "%s:%i: Insufficient number of "
							"values defining additional data object.\n",
							level_packs[selected_pack].filename,
							line_count);
					}
					else if (x < 0 || x >= MAX_W)
					{
						fprintf (stdout, "%s:%i: Invalid value for x in "
							"additional data object.\n",
							level_packs[selected_pack].filename,
							line_count);
					}
					else if (y < 0 || y >= MAX_H)
					{
						fprintf (stdout, "%s:%i: Invalid value for y in "
							"additional data object.\n",
							level_packs[selected_pack].filename,
							line_count);
					}
					else
					{
						switch (transform_char (symbol))
						{
							case -1:
								switch (value_read[0]) {
								    case WALL_RED:
								    case WALL_GREEN:
								    case BLACK_WALL:
								    case FAT_WALL:
								    case ROUND_WALL:
								    case BOULDER_WALL:
								    case SQUARE_WALL:
								    case LATTICE_WALL:
								    case S_WALL:
									create_wall(value_read[0],x,y);
									//board[x][y].state=value_read[1];
									board[x][y].direction=value_read[2];
									board[x][y].destroyable=value_read[3];
									board[x][y].blowable=value_read[4];
									board[x][y].killing=value_read[5];
									board[x][y].movable=value_read[6];
									board[x][y].shooted=value_read[7];

								    break;
								    default:
									//fprintf(stdout,"%i %i %i %i %i\n",value_read[1],value_read[2],value_read[3],value_read[4],value_read[5]);
									create_object(x,y,value_read[0]);
									board[x][y].state=value_read[1];
									board[x][y].direction=value_read[2];
									board[x][y].destroyable=value_read[3];
									board[x][y].blowable=value_read[4];
									board[x][y].killing=value_read[5];
									board[x][y].movable=value_read[6];
									board[x][y].shooted=value_read[7];
								    break;	
								    }
								    break;
								
							case LASER_L:
							case LASER_D:
								board[x][y].direction=value_read[0];
								break;
							case TELEPORT:
								board[x][y].teleportnumber = value_read[0];
								board[x][y].teleportnumber2 = value_read[1];
								break;
							case GUN:
								board[x][y].direction = value_read[0];
								board[x][y].state = value_read[0];
								board[x][y].direction2 = value_read[1];
								board[x][y].solidlaser = value_read[2];
								board[x][y].movable = value_read[3];
								board[x][y].rotable = value_read[4];
								board[x][y].randomrotated = value_read[5];
								if(board[x][y].movable==1) {
									board[x][y].state+=4;
								}
								break;
							case MAGNET:
								board[x][y].state = value_read[0];
								if(value_count>4)   /* we would like to have rotable magnets */
									board[x][y].rotable=value_read[1];
								else	
									board[x][y].rotable=0;
							case BARRIER:
								board[x][y].direction = value_read[0];
								break;
							case BIRD:
								board[x][y].direction2 = value_read[1];
								board[x][y].shooting = value_read[2];
							case BEAR_B:
							case BEAR:
								board[x][y].direction = value_read[0];
								break;
							default:
								fprintf (stdout, "%s:%i: Invalid value for "
									"symbol in additional data object.\n",
									level_packs[selected_pack].filename,
									line_count);
								break;
						}
					}
					if (--rows <= 0) additional_read = DATA_READ;
				}
			}
		}
	}
	
	fclose (fp);
        rearange_walls();
	#ifdef DEBUG_LOAD_LEVEL_DATA
		printf ("default_level_colour_read=%s\n",
			data_state[default_level_colour_read]);
		printf ("level_read=%s\n", data_state[level_read]);
		printf ("colour_read=%s\n", data_state[colour_read]);
		printf ("size_read=%s\n", data_state[size_read]);
		printf ("author_read=%s\n", data_state[author_read]);
		printf ("notes_read=%s\n", data_state[notes_read]);
		printf ("data_read=%s\n", data_state[data_read]);
		printf ("additional_read=%s\n", data_state[additional_read]);
		printf ("*** Stop %s ***\n", __func__);
	#endif

	if (level_read == DATA_READ && size_read == DATA_READ &&
		data_read == DATA_READ)
	{
		return FALSE;
	}
	else
	{
		/* Show the user generally what caused the load abort. More
		 * descriptive messages may have already been displayed from
		 * the above code */
		fprintf (stdout, "Loading of pack %s level %i has been aborted due "
			"to :-\n", level_packs[selected_pack].filename, level_number);
		if (level_read != DATA_READ)
		{
			fprintf (stdout, "* The level was not found.\n");
		}
		else
		{
			if (size_read == DATA_UNREAD)
			{
				fprintf (stdout, "* The [size] tag was not found.\n");
			}
			else if (size_read == DATA_INCOMPLETE)
			{
				fprintf (stdout, "* Incomplete or invalid size data.\n");
			}
			if (data_read == DATA_UNREAD)
			{
				fprintf (stdout, "* The [data] tag was not found.\n");
			}
			else if (data_read == DATA_INCOMPLETE)
			{
				fprintf (stdout, "* Incomplete level data.\n");
			}
		}
	}

	return TRUE;
}

/****************************************************/
/*** Transformation char for object value ***********/
/****************************************************/

int transform_char (char c)
{
	switch (c)
	{
		case 'a': 
			return S_WALL;
		case 'X':
			return STOP;
		case 'k':
			return RADIOACTIVE_FIELD;
		case '.':
			return EMPTY_FIELD;
		case 'R':
			return ROBBO;
		case 'O':
			return WALL;
		case 'Q':
			return WALL_RED;
		case 'T':
			return SCREW;
		case '\'':
			return BULLET;
		case '#':
			return BOX;
		case '%':
			return KEY;
		case 'B':
			return BOMB2;  /* robbo alex has at least two types of bombs */ 	
		case 'b':
			return BOMB;
		case 'D':
			return DOOR;
		case '?':
			return QUESTIONMARK;
		case '@':
			return BEAR;
			case '^':
		return BIRD;
		case '!':
			return CAPSULE;
		case 'H':
			return GROUND;
		case 'o':
			return WALL_GREEN;
		case '*':
			return BEAR_B;
		case 'V':
			return BUTTERFLY;
		case '&':
			return TELEPORT;
		case '}':
			return GUN;
		case 'M':
			return MAGNET;
		case '-':
			/* Thunor: I changed spaces to "-"
			 * because spaces are difficult to manage */
			return BLACK_WALL;
		case '~':
			return PUSH_BOX;
		case '=':
			return BARRIER;
		case 'q':
			return FAT_WALL;
		case 'p':
			return ROUND_WALL;
		case 'P':
			return BOULDER_WALL;
		case 's':
			return SQUARE_WALL;
		case 'S':
			return LATTICE_WALL;
		case '+':
			/* Thunor: "+" is a life which we don't support
			 * but remains in some levels */
 //			return EMPTY_FIELD;

/*neurocyp: STOP is more appropriate*/
			return STOP;
		case 'L':
			return LASER_L;
		case 'l':
			return LASER_D;	
		case ',':
			return -1; // This one must be additionally being resolved
		default:
			fprintf (stdout, "Unrecognised object identifier: \"%c\". "
				"Setting to EMPTY_FIELD\n", c);
			return EMPTY_FIELD;
	}
}