/// @param level

var _level = argument[0];

enum LEVEL_WAVE {
	NAME,
	DELAY,
	COUNT,
	SIZE
};

var _level_waves = 0;
var _read_type = 0;

var _file = file_text_open_read(PATH_DATA + "levels/" + global.levels[_level-1] + ".lvl");

var _yy = 0;

while(!file_text_eof(_file)) {
	var _line = file_text_readln(_file);
	_line = string_replace_all(_line, chr(10), "");
	_line = string_replace_all(_line, chr(13), "");
	
	if (string_char_at(_line, 1) == "#")
		continue;
	else if (string_length(_line) == 0) {
		_read_type++;
		continue;
	}
	
	if (_read_type == 0) {
		global.money = real(_line);
	}
	else if (_read_type == 1) {
		var _type = 0;
		var _str = "";
		ds_grid_resize(global.level_data, LEVEL_WAVE.SIZE, _level_waves+1);
	
		for(var i = 1; i <= string_length(_line)+1; i++) {
			var _c = string_char_at(_line, i);
			if (_c == " " || i > string_length(_line)) {
				if (_type == LEVEL_WAVE.NAME)
					ds_grid_add(global.level_data, _type, _level_waves, _str);
				else
					ds_grid_add(global.level_data, _type, _level_waves, real(_str));
				_type++;
				_str = "";
			}
			else {
				_str += _c;
			}
		}
		_level_waves++;
	}
	else {
		for(var _xx = 0; _xx <= string_length(_line)+1; _xx++) {
			var _c = string_char_at(_line, _xx + 1);
			sc_place_object(_c, _xx, _yy);
		}
		//show_debug_message(_line);
		_yy++;
	}
}

file_text_close(_file);
/* DEBUG
var _str = "";
for (var j = 0; j < ds_grid_height(global.level_data); j++) {
	for (var i = 0; i < ds_grid_width(global.level_data); i++) {
		if (i == 0)
			_str += "\n";
		_str += string(global.level_data[# i, j]) + "|";
	}
}
show_debug_message(_str);
*/