//{ //Header
// Tafl
// script and map by Elypter

//}

//{ //Declares
//{ //PlayerDefines
//{ //Player
CHAR_DATA p
COUNTER px=0
COUNTER py=0
//}
//{ //Player1
PLAYER_PED p1 = (124.5, 125.5, 255.0) 7 225
COUNTER p1x=0
COUNTER p1y=0
//}
//{ //Player2
PLAYER_PED p2 = (116.5, 117.5, 255.0) 8 45
COUNTER p2x=0
COUNTER p2y=0
//}
//{ //Player3
PLAYER_PED p3 = (124.5, 117.5, 255.0) 9 315
COUNTER p3x=0
COUNTER p3y=0
//}
//{ //Player4
PLAYER_PED p4 = (116.5, 125.5, 255.0) 10 135
COUNTER p4x=0
COUNTER p4y=0
//}
//{ //Player5
PLAYER_PED p5 = (120.5, 117.5, 255.0) 11 0
COUNTER p5x=0
COUNTER p5y=0
//}
//{ //Player6
PLAYER_PED p6 = (120.5, 125.5, 255.0) 12 180
COUNTER p6x=0
COUNTER p6y=0
//}
//}

COUNTER player=0
COUNTER neverends=1
COUNTER in=0
COUNTER out=0
COUNTER diffx=0
COUNTER diffy=0
//ONSCREEN_COUNTER diffx_osc
//ONSCREEN_COUNTER diffy_osc
//ONSCREEN_COUNTER dummy_osc
//ONSCREEN_COUNTER p1dword_osc
COUNTER p1dword=0
GENERATOR freload = (120.5, 121.5, 1.0) 0 COLLECT_36 0 250 1
COUNTER molotov=1
COUNTER reload
//}

LEVELSTART

SWITCH_GENERATOR (freload, ON)
//ADD_ONSCREEN_COUNTER (dummy_osc,neverends)
//ADD_ONSCREEN_COUNTER (diffx_osc,p1x)
//ADD_ONSCREEN_COUNTER (diffy_osc,p1y)
//ADD_ONSCREEN_COUNTER (p1dword_osc,p1dword)

WHILE_EXEC ( neverends = 1 )

SET player=0

WHILE_EXEC(player<6)
    //{ //PlayerPointers
    ++player
    if(player=1)
        SET p=(p1+0)
        SET px=(p1x+0)
        SET py=(p1y+0)
    endif
    if(player=2)
        SET p=(p2+0)
        SET px=(p2x+0)
        SET py=(p2y+0)
    endif
    if(player=3)
        SET p=(p3+0)
        SET px=(p3x+0)
        SET py=(p3y+0)
    endif
    if(player=4)
        SET p=(p4+0)
        SET px=(p4x+0)
        SET py=(p4y+0)
    endif
    if(player=5)
        SET p=(p5+0)
        SET px=(p5x+0)
        SET py=(p5y+0)
    endif
    if(player=6)
        SET p=(p6+0)
        SET px=(p6x+0)
        SET py=(p6y+0)
    endif
    //}

	IF (CHECK_CHARACTER_HEALTH(p, 0))

	//{ //read player x

		SET in = (p+360)
		CHANGE_GANG_CHAR_RESPECT (in,out,104)
		SET in = (out+128)
		CHANGE_GANG_CHAR_RESPECT (in,out,104)
		SET in = (out+20)
		CHANGE_GANG_CHAR_RESPECT (in,px,104) //read x
		SET px=(px/16384)
	//}
	//{ //read player y
		SET in = (p+360)
		CHANGE_GANG_CHAR_RESPECT (in,out,104)
		SET in = (out+128)
		CHANGE_GANG_CHAR_RESPECT (in,out,104)
		SET in = (out+24)
		CHANGE_GANG_CHAR_RESPECT (in,py,104) //read y
		SET py=(py/16384)
	//}
		//IF(NOT((((px=124)AND(py=117))OR((px=116)AND(py=117)))OR(((px=116)AND(py=125))OR((px=124)AND(py=125)))))
		SET molotov=1
		IF(NOT((px=124)AND(py=117)))
		IF(NOT((px=116)AND(py=117)))
		IF(NOT((px=116)AND(py=125)))
		IF(NOT((px=124)AND(py=125)))
			SET molotov=0
		ENDIF
		ENDIF
		ENDIF
		ENDIF

		IF(molotov=0)
			SET diffx=(px/2)
			SET diffx=(diffx*2)
			SET diffx=(px - diffx)
			SET diffy=(py/2)
			SET diffy=(diffy*2)
			SET diffy=(py - diffy)

			//both even or both uneven=chessboard
			IF(NOT(diffx = diffy))
				IF(NOT(CHECK_CHAR_CURR_WEAPON(p,ROCKET_LAUNCHER)))
					REMOVE_WEAPONS (p)
				ENDIF
				GIVE_WEAPON (p,ROCKET_LAUNCHER,255)
			else
				IF(NOT(CHECK_CHAR_CURR_WEAPON(p,MACHINE_GUN)))
					REMOVE_WEAPONS (p)
				ENDIF
				GIVE_WEAPON (p,MACHINE_GUN,255)
				SET in=(p+368) //player + 368 is a pointer to the player ammo (only for first weapon slot)
				CHANGE_GANG_CHAR_RESPECT (in,out,104) //read those 4 bytes
				IF (out>0)
					SET reload=(out+2) //we don't care about ammo, reload countdown is located at ammo location + 2
					CHANGE_GANG_CHAR_RESPECT (reload,out,101) //read 1 byte
					IF (out>1)  // change 5 to 0 for instant reload
					  SET out=1 // change 5 to 0 for instant reload
					  CHANGE_GANG_CHAR_RESPECT (reload,out,111) //write 1 byte
					ENDIF
				ENDIF
			endif
			IF((px=120)AND(py=121))
				//IF(NOT(CHECK_CHAR_CURR_WEAPON(p,ROCKET_LAUNCHER)))
				//	REMOVE_WEAPONS (p)
				//ENDIF
				//GIVE_WEAPON (p,ROCKET_LAUNCHER,255)
				SET in=(p+368) //player + 368 is a pointer to the player ammo (only for first weapon slot)
				CHANGE_GANG_CHAR_RESPECT (in,out,104) //read those 4 bytes
				IF (out>0)
					SET reload=(out+2) //we don't care about ammo, reload countdown is located at ammo location + 2
					CHANGE_GANG_CHAR_RESPECT (reload,out,101) //read 1 byte
					IF (out>25)  // change 5 to 0 for instant reload
					  SET out=25 // change 5 to 0 for instant reload
					  CHANGE_GANG_CHAR_RESPECT (reload,out,111) //write 1 byte
					ENDIF
				ENDIF
			endif

		ELSE
			IF(NOT(CHECK_CHAR_CURR_WEAPON(p,MOLOTOV)))
				REMOVE_WEAPONS (p)
			ENDIF
			GIVE_WEAPON (p,MOLOTOV,255)
		ENDIF



	ENDIF
	//{ //PlayerPointers
    if(player=1)
        SET p1x=(px+0)
        SET p1y=(py+0)
    endif
    if(player=2)
        SET p2x=(px+0)
        SET p2y=(py+0)
    endif
    if(player=3)
        SET p3x=(px+0)
        SET p3y=(py+0)
    endif
    if(player=4)
        SET p4x=(px+0)
        SET p4y=(py+0)
    endif
    if(player=5)
        SET p5x=(px+0)
        SET p5y=(py+0)
    endif
    if(player=6)
        SET p6x=(px+0)
        SET p6y=(py+0)
    endif
    //}
ENDWHILE
ENDWHILE

LEVELEND
