procedure start;

variable weather;
variable region;
variable map;
variable season;

variable rainshaderdef;
variable snowshaderdef;
variable sandshaderdef;
variable rainshaderesc;
variable snowshaderesc;
variable sandshaderesc;
variable rainshaderinv;
variable snowshaderinv;
variable sandshaderinv;
variable rainshadermap;
variable snowshadermap;
variable sandshadermap;
variable rainshaderskd;
variable snowshaderskd;
variable sandshaderskd;
variable cshader;

variable map_has_weather;
variable active;
variable w1; //Begin
variable w2; //End
variable w3; //Special
variable increase;

//0 - nothing
//1 - -1 perception, +2 ac
//2 - -2 perception, +4 ac
//3 - -3 perception, +6 ac
//4 - -1 endurance, -3 perception, +4 ac
//5 - -4 perception, +8 ac
variable penelty;

variable last_game_time;

procedure update_penelty(variable newpenelty) begin
	if(penelty==newpenelty) then begin
   	return;
	end
	if(penelty==1) then begin
		set_pc_extra_stat(1, get_pc_extra_stat(1) + 1);
		set_pc_extra_stat(9, get_pc_extra_stat(9) - 2);
	end else if(penelty==2) then begin
		set_pc_extra_stat(1, get_pc_extra_stat(1) + 2);
		set_pc_extra_stat(9, get_pc_extra_stat(9) - 4);
	end else if(penelty==2) then begin
		set_pc_extra_stat(1, get_pc_extra_stat(1) + 3);
		set_pc_extra_stat(9, get_pc_extra_stat(9) - 6);
	end else if(penelty==4) then begin
		set_pc_extra_stat(1, get_pc_extra_stat(1) + 3);
		set_pc_extra_stat(2, get_pc_extra_stat(2) + 1);
		set_pc_extra_stat(9, get_pc_extra_stat(9) - 4);
	end else if(penelty==5) then begin
		set_pc_extra_stat(1, get_pc_extra_stat(1) + 4);
		set_pc_extra_stat(9, get_pc_extra_stat(9) - 8);
	end
	penelty:=newpenelty;
	set_sfall_global("tsrain01", penelty);
	if(penelty==1) then begin
		set_pc_extra_stat(1, get_pc_extra_stat(1) - 1);
		set_pc_extra_stat(9, get_pc_extra_stat(9) + 2);
	end else if(penelty==2) then begin
		set_pc_extra_stat(1, get_pc_extra_stat(1) - 2);
		set_pc_extra_stat(9, get_pc_extra_stat(9) + 4);
	end else if(penelty==2) then begin
		set_pc_extra_stat(1, get_pc_extra_stat(1) - 3);
		set_pc_extra_stat(9, get_pc_extra_stat(9) + 6);
	end else if(penelty==4) then begin
		set_pc_extra_stat(1, get_pc_extra_stat(1) - 3);
		set_pc_extra_stat(2, get_pc_extra_stat(2) - 1);
		set_pc_extra_stat(9, get_pc_extra_stat(9) + 4);
	end else if(penelty==5) then begin
		set_pc_extra_stat(1, get_pc_extra_stat(1) - 4);
		set_pc_extra_stat(9, get_pc_extra_stat(9) + 8);
	end
end

procedure update_shader(variable shader) begin
	if(shader==cshader) then begin
		return;
	end
	if(cshader==1) then begin
		deactivate_shader(rainshaderdef);
		deactivate_shader(rainshaderesc);
		deactivate_shader(rainshaderinv);
		deactivate_shader(rainshadermap);
		deactivate_shader(rainshaderskd);
	end else if(cshader==2) then begin
		deactivate_shader(snowshaderdef);
		deactivate_shader(snowshaderesc);
		deactivate_shader(snowshaderinv);
		deactivate_shader(snowshadermap);
		deactivate_shader(snowshaderskd);
	end else if(cshader==3) then begin
		deactivate_shader(sandshaderdef);
		deactivate_shader(sandshaderesc);
		deactivate_shader(sandshaderinv);
		deactivate_shader(sandshadermap);
		deactivate_shader(sandshaderskd);
	end
	if(shader==1) then begin
		activate_shader(rainshaderdef);
		activate_shader(rainshaderesc);
		activate_shader(rainshaderinv);
		activate_shader(rainshadermap);
		activate_shader(rainshaderskd);
	end else if(shader==2) then begin
		activate_shader(snowshaderdef);
		activate_shader(snowshaderesc);
		activate_shader(snowshaderinv);
		activate_shader(snowshadermap);
		activate_shader(snowshaderskd);
	end else if(shader==3) then begin
		activate_shader(sandshaderdef);
		activate_shader(sandshaderesc);
		activate_shader(sandshaderinv);
		activate_shader(sandshadermap);
		activate_shader(sandshaderskd);
	end
	cshader:=shader;
	if(shader==0) then begin
		force_graphics_refresh(0);
		call update_penelty(0);
	end else begin
		force_graphics_refresh(1);
	end
end

procedure on_game_load begin
	variable tmp;
	if(graphics_funcs_available==0) then begin
		display_msg("Weather mod error: Shader functions not available");
		return;
	end
	if(get_shader_version<20) then begin
		display_msg("Weather mod error: Graphics card does not support sm2 shaders");
		return;
	end
	rainshaderdef:=load_shader("weather\\rain-def.fx");
	snowshaderdef:=load_shader("weather\\snow-def.fx");
	sandshaderdef:=load_shader("weather\\sand-def.fx");
	rainshaderesc:=load_shader("weather\\rain-esc.fx");
	snowshaderesc:=load_shader("weather\\snow-esc.fx");
	sandshaderesc:=load_shader("weather\\sand-esc.fx");
	rainshaderinv:=load_shader("weather\\rain-inv.fx");
	snowshaderinv:=load_shader("weather\\snow-inv.fx");
	sandshaderinv:=load_shader("weather\\sand-inv.fx");
	rainshadermap:=load_shader("weather\\rain-map.fx");
	snowshadermap:=load_shader("weather\\snow-map.fx");
	sandshadermap:=load_shader("weather\\sand-map.fx");
	rainshaderskd:=load_shader("weather\\rain-skd.fx");
	snowshaderskd:=load_shader("weather\\snow-skd.fx");
	sandshaderskd:=load_shader("weather\\sand-skd.fx");
	if(rainshaderdef==-1 or snowshaderdef==-1 or sandshaderdef==-1) then begin
		display_msg("Weather mod error: Could not load default shaders");
		return;
	end
	if(rainshaderesc==-1 or snowshaderesc==-1 or sandshaderesc==-1) then begin
		display_msg("Weather mod error: Could not load escape shaders");
		return;
	end
	if(rainshaderinv==-1 or snowshaderinv==-1 or sandshaderinv==-1) then begin
		display_msg("Weather mod error: Could not load inventory shaders");
		return;
	end
	if(rainshadermap==-1 or snowshadermap==-1 or sandshadermap==-1) then begin
		display_msg("Weather mod error: Could not load automap shaders");
		return;
	end
	if(rainshaderskd==-1 or snowshaderskd==-1 or sandshaderskd==-1) then begin
		display_msg("Weather mod error: Could not load skilldex shaders");
		return;
	end
	tmp:=get_shader_texture(rainshaderdef, 2);
	set_shader_texture(rainshaderesc, "tex2", tmp);
	set_shader_texture(rainshaderinv, "tex2", tmp);
	set_shader_texture(rainshadermap, "tex2", tmp);
	set_shader_texture(rainshaderskd, "tex2", tmp);
	tmp:=get_shader_texture(snowshaderdef, 2);
	set_shader_texture(snowshaderesc, "tex2", tmp);
	set_shader_texture(snowshaderinv, "tex2", tmp);
	set_shader_texture(snowshadermap, "tex2", tmp);
	set_shader_texture(snowshaderskd, "tex2", tmp);
	tmp:=get_shader_texture(sandshaderdef, 2);
	set_shader_texture(sandshaderesc, "tex2", tmp);
	set_shader_texture(sandshaderinv, "tex2", tmp);
	set_shader_texture(sandshadermap, "tex2", tmp);
	set_shader_texture(sandshaderskd, "tex2", tmp);
	
	tmp:=get_shader_texture(rainshaderdef, 1);
	set_shader_texture(snowshaderdef, "tex1", tmp);
	set_shader_texture(sandshaderdef, "tex1", tmp);
	tmp:=get_shader_texture(rainshaderesc, 1);
	set_shader_texture(snowshaderesc, "tex1", tmp);
	set_shader_texture(sandshaderesc, "tex1", tmp);
	tmp:=get_shader_texture(rainshaderinv, 1);
	set_shader_texture(snowshaderinv, "tex1", tmp);
	set_shader_texture(sandshaderinv, "tex1", tmp);
	tmp:=get_shader_texture(rainshadermap, 1);
	set_shader_texture(snowshadermap, "tex1", tmp);
	set_shader_texture(sandshadermap, "tex1", tmp);
	tmp:=get_shader_texture(rainshaderskd, 1);
	set_shader_texture(snowshaderskd, "tex1", tmp);
	set_shader_texture(sandshaderskd, "tex1", tmp);
	
	set_shader_mode(rainshaderdef, 30653);//0x77bd
	set_shader_mode(snowshaderdef, 30653);
	set_shader_mode(sandshaderdef, 30653);
	set_shader_mode(rainshaderesc, 30645);//0x77b5
	set_shader_mode(snowshaderesc, 30645);
	set_shader_mode(sandshaderesc, 30645);
	set_shader_mode(rainshaderinv, 26557);//0x67bd
	set_shader_mode(snowshaderinv, 26557);
	set_shader_mode(sandshaderinv, 26557);
	set_shader_mode(rainshadermap, 22461);//0x57bd
	set_shader_mode(snowshadermap, 22461);
	set_shader_mode(sandshadermap, 22461);
	set_shader_mode(rainshaderskd, 14269);//0x37bd
	set_shader_mode(snowshaderskd, 14269);
	set_shader_mode(sandshaderskd, 14269);
	set_shader_mode(rainshaderesc, 2147483656);//0x80000008
	set_shader_mode(snowshaderesc, 2147483656);
	set_shader_mode(sandshaderesc, 2147483656);
	set_shader_mode(rainshaderinv, 2147487744);//0x80001000
	set_shader_mode(snowshaderinv, 2147487744);
	set_shader_mode(sandshaderinv, 2147487744);
	set_shader_mode(rainshadermap, 2147491840);//0x80002000
	set_shader_mode(snowshadermap, 2147491840);
	set_shader_mode(sandshadermap, 2147491840);
	set_shader_mode(rainshaderskd, 2147500032);//0x80004000
	set_shader_mode(snowshaderskd, 2147500032);
	set_shader_mode(sandshaderskd, 2147500032);
	set_global_script_repeat(50);

	region:=-1;
	map:=-1;
	active:=0;
	last_game_time:=-100;
	map_has_weather:=0;
	cshader:=0;
	increase:=1;
	penelty:=get_sfall_global_int("tsrain01");
end

procedure update_season begin
	variable month;
	month:=get_month;
	if ( month == 12) then begin
		month:=0;
	end
	if(month<=3) then begin
		season:="win";
	end else if(month<=6) then begin
		season:="spr";
	end else if(month<=9) then begin
		season:="sum";
	end else begin
		season:="aut";
	end
end

procedure update_region begin
   variable initial;
   variable tmp;
   variable rain;
   variable snow;
   variable sand;
	//display_msg("Update region: "+region);

	if(region==-1) then begin
		call update_shader(0);
		active:=0;
		return;
	end
	
	tmp:=get_ini_setting("weather.ini|r"+region+season+"|override");
	if(tmp==0) then begin
		initial:="weather.ini|r"+region+season+"|";
		end else begin
		initial:="weather.ini|r"+region+"|";
	end

	w1:=get_ini_setting(initial+"start");
	w2:=get_ini_setting(initial+"end");
	w3:=get_ini_setting(initial+"special");
	
	rain:=get_ini_setting(initial+"rain");
	snow:=get_ini_setting(initial+"snow");
	sand:=get_ini_setting(initial+"sand");

	if(w1==-1 or w2==-1 or w3==-1 or rain==-1 or snow==-1 or sand==-1) then begin
		active:=0;
	end else begin
		snow:=snow+rain;
		sand:=sand+snow;
		tmp:=random(1,100);
		if(tmp<=rain) then begin
			//display_msg("It's raining");
			active:=1;
		end else if(tmp<=snow) then begin
			//display_msg("It's snowing");
			active:=2;
		end else if(tmp<=sand) then begin
			//display_msg("It's sandstorming");
			active:=3;
		end else begin
			//display_msg("No weather");
			active:=0;
		end
	end

	call update_shader(active);
end

procedure update_weather begin
	variable tmp;
	
	//display_msg("Update weather: "+weather);
	
	if(weather<=w1 or map_has_weather==0) then begin
		call update_shader(0);
	end else begin
		call update_shader(active);
		tmp:=(weather-w1)*(1.0/(w2-w1));
		if(tmp>1.0) then begin
			tmp:=1.0;
		end
		if(active==1) then begin
			set_shader_float(rainshaderdef, "speed", 500 - (tmp*300));
			set_shader_float(rainshaderdef, "multi", 1.5 + tmp*6.5);
			set_shader_float(rainshaderesc, "speed", 500 - (tmp*300));
			set_shader_float(rainshaderesc, "multi", 1.5 + tmp*6.5);
			set_shader_float(rainshaderinv, "speed", 500 - (tmp*300));
			set_shader_float(rainshaderinv, "multi", 1.5 + tmp*6.5);
			set_shader_float(rainshadermap, "speed", 500 - (tmp*300));
			set_shader_float(rainshadermap, "multi", 1.5 + tmp*6.5);
			set_shader_float(rainshaderskd, "speed", 500 - (tmp*300));
			set_shader_float(rainshaderskd, "multi", 1.5 + tmp*6.5);
			if(tmp<0.7) then begin
				call update_penelty(0);
			end else if(tmp<0.99) then begin
				call update_penelty(1);
			end else begin
				call update_penelty(2);
			end
		end else if(active==2) then begin
			set_shader_float(snowshaderdef, "speed", 800 - (tmp*200));
			set_shader_float(snowshaderdef, "multi", 0.5 + tmp*0.5);
			set_shader_float(snowshaderesc, "speed", 800 - (tmp*200));
			set_shader_float(snowshaderesc, "multi", 0.5 + tmp*0.5);
			set_shader_float(snowshaderinv, "speed", 800 - (tmp*200));
			set_shader_float(snowshaderinv, "multi", 0.5 + tmp*0.5);
			set_shader_float(snowshadermap, "speed", 800 - (tmp*200));
			set_shader_float(snowshadermap, "multi", 0.5 + tmp*0.5);
			set_shader_float(snowshaderskd, "speed", 800 - (tmp*200));
			set_shader_float(snowshaderskd, "multi", 0.5 + tmp*0.5);
			if(tmp<0.2) then begin
				call update_penelty(0);
			end else if(tmp<0.5) then begin
				call update_penelty(1);
			end else if(tmp<0.9) then begin
				call update_penelty(2);
			end else begin
				call update_penelty(4);
			end
		end else begin
			set_shader_float(sandshaderdef, "speed", 400 - (tmp*200));
			set_shader_float(sandshaderdef, "multi", tmp);
			set_shader_float(sandshaderesc, "speed", 400 - (tmp*200));
			set_shader_float(sandshaderesc, "multi", tmp);
			set_shader_float(sandshaderinv, "speed", 400 - (tmp*200));
			set_shader_float(sandshaderinv, "multi", tmp);
			set_shader_float(sandshadermap, "speed", 400 - (tmp*200));
			set_shader_float(sandshadermap, "multi", tmp);
			set_shader_float(sandshaderskd, "speed", 400 - (tmp*200));
			set_shader_float(sandshaderskd, "multi", tmp);
			if(tmp<0.3) then begin
				call update_penelty(1);
			end else if(tmp<0.6) then begin
				call update_penelty(2);
			end else if(tmp<0.9) then begin
				call update_penelty(3);
			end else begin
				call update_penelty(5);
			end
		end
		
		//display_msg("Shader active: "+tmp);
	end
end

procedure modify_weather(variable amount) begin
	if(active==0 or amount==0) then begin
		return;
	end
	if(random(1,100)>95) then begin
		increase:=increase*(-1);
	end
	while(amount>0) do begin
		weather:=weather+increase;
		amount:=amount-1;
	end
	call update_weather;
end

procedure randomize_weather begin
	if(active==0) then begin
		return;
	end
	weather:=random(0,100);
	//weather:=0;
	call update_weather;
end

procedure start begin
	variable tmp;
	if (game_loaded) then begin
		call on_game_load;
		return;
	end

	tmp:=cur_map_index;
	if(tmp!=map) then begin
		map:=tmp;
		tmp:=get_ini_setting("weather.ini|maps|r"+map);
		map_has_weather:=get_ini_setting("weather.ini|maps|a"+map);
		if(map_has_weather==-1) then begin
			map_has_weather:=0;
		end
		//display_msg("Map change: "+map+", "+tmp);
		if(tmp!=region) then begin
			region:=tmp;
			call update_region;
			call randomize_weather;
			last_game_time:=game_time/200;
			return;
		end
		call update_weather;
		return;
	end
	
	if(active==0) then begin
	    return;
	end

	tmp:=game_time/200;
	if(tmp!=last_game_time) then begin
		display_msg("Clock change: "+last_game_time+" -> "+tmp);
		if(tmp-last_game_time>100) then begin
			call randomize_weather;
		end else begin
			call modify_weather(tmp-last_game_time);
		end
		last_game_time:=tmp;
		return;
	end

	//Lightening goes here
end