Name | Type | is_array | initial_value |
i | integer | No | |
Unit | unit | Yes |
//TESH.scrollpos=0
//TESH.alwaysfold=0
_____________________________
| |
| Control System |
| by MF_Andreich |
| version 1.12 |
|_____________________________|
Jass New Gen is needed for usage.
If you use this system in your map,
please mention the author in the credits.
_______________DESCRIPTION_________________
Using this system, you can control the game
character with arrow keys. Mouse control is
also possible at the same time.
__________________HOW_TO_USE_IT?___________________
1) Copy the SCS trigger to your map.
2) Initialize control somewhere using SCS_Associate
function (see function description section below)
In this map this is done in "Init" trigger.
____________________FUNCTIONS_______________________
SCS_Associate( unit, player, integer, boolean )
Gives player control over unit using arrow keys.
integer denotes the walk animation id of the unit
and boolean flag determines if the camera
rotation will follow facing of the unit
SCS_RemoveAssociate( player )
Removes control association from player
SCS_RemoveControlUnit( player )
Removes control association from player and
removes the controlled unit from the game.
SCS_SetControl( player, boolean )
Sets if player will use arrow control.
boolean SCS_GetControl(player,boolean)
Returns true if player is using arrow control.
SCS_SetCamera( player, boolean )
Sets the camera mode for player. False denotes
generic game camera, true means that the camera
will follow the controlled unit.
boolean SCS_GetCamera( player )
Returns true if player is using following camera.
unit SCS_GetUnit( player )
Returns the unit, which is controlled by player
using this system.
_________________CREDITS____________________
MF_Andreich - system coding
alexkill - basic version
XGM 2009
//TESH.scrollpos=294
//TESH.alwaysfold=0
library SCS initializer init
/*
Control system by MF_Andreich
version 1.12
*/
// -------------------------- User-adjucted values -----------------------------------
// These variables can be changed by the user
globals
// Count of players, which will use such control style.
// First COUNT_OF_PLAYERS players will be counted.
// If anyhow, you need for example, only players 0 and 11 to use this system,
// please leave this value as it is (12)
constant integer COUNT_OF_PLAYERS = 12
// If the turns in backwards movement will be inversed (1 = yes, -1 = no)
constant integer INVERSE = 1
endglobals
// This function returns true if the unit can move.
// You can add any conditions you want here.
private function ConditionToMove takes unit u returns boolean
return (GetUnitState(u, UNIT_STATE_LIFE)>=0.45)and(GetUnitAbilityLevel(u, 'BSTN')==0)and(GetUnitAbilityLevel(u,'BPSE')==0)and(GetUnitAbilityLevel(u,'BUsl')==0)and(GetUnitAbilityLevel(u,'BUst')==0)and(GetUnitAbilityLevel(u,'BUim')==0)and(GetUnitAbilityLevel(u,'Bwea')==0)and(GetUnitAbilityLevel(u,'Bweb')==0)and(GetUnitAbilityLevel(u,'Bmlt')==0)
endfunction
//---------------------- End of user-adjucted section -----------------------------
// The next code shouldn't be altered
globals
private unit array Units[COUNT_OF_PLAYERS]
private boolean array Up[COUNT_OF_PLAYERS]
private boolean array Down[COUNT_OF_PLAYERS]
private boolean array Left[COUNT_OF_PLAYERS]
private boolean array Right[COUNT_OF_PLAYERS]
private boolean array ApplyCam[COUNT_OF_PLAYERS]
private boolean array ApplyControl[COUNT_OF_PLAYERS]
private integer array Anims[COUNT_OF_PLAYERS]
private boolean array Arround[COUNT_OF_PLAYERS]
private boolean array FlagArround[COUNT_OF_PLAYERS]
private boolean array ResetFlag[COUNT_OF_PLAYERS]
private location l
endglobals
private function UpUp_Func takes integer i returns nothing
set Up[i] = false
if GetUnitState(Units[i],UNIT_STATE_LIFE)>=0.45 then
call SetUnitTimeScale(Units[i], 1)
call SetUnitAnimation(Units[i], "stand")
endif
if(Down[i])and(Arround[i])then
set FlagArround[i] = true
call SetUnitFacingTimed( Units[i], (GetUnitFacing(Units[i])+180), 0.01 )
endif
if ResetFlag[i] then
set ResetFlag[i] = false
if GetUnitState(Units[i],UNIT_STATE_LIFE)>=0.45 and ApplyControl[i] and Anims[i]!=-1 then
call SetUnitAnimationByIndex(Units[i],Anims[i])
endif
if(not Up[i])and(Arround[i])then
call SetUnitTimeScale(Units[i], GetUnitMoveSpeed(Units[i])/GetUnitDefaultMoveSpeed(Units[i]))
set FlagArround[i] = true
call SetUnitFacingTimed( Units[i], (GetUnitFacing(Units[i])+180), 0 )
else
call SetUnitTimeScale(Units[i], -GetUnitMoveSpeed(Units[i])/GetUnitDefaultMoveSpeed(Units[i]))
endif
endif
endfunction
private function DownUp_Func takes integer i returns nothing
set Down[i] = false
if GetUnitState(Units[i],UNIT_STATE_LIFE)>=0.45 then
call SetUnitTimeScale(Units[i], 1)
call SetUnitAnimation(Units[i], "stand")
endif
if FlagArround[i] then
set FlagArround[i] = false
call SetUnitFacingTimed( Units[i], (GetUnitFacing(Units[i])+180), 0 )
endif
if ResetFlag[i] then
set ResetFlag[i] = false
if GetUnitState(Units[i],UNIT_STATE_LIFE)>=0.45 and ApplyControl[i] then
if(Anims[i]!=-1)then
call SetUnitAnimationByIndex(Units[i],Anims[i])
endif
call SetUnitTimeScale(Units[i], GetUnitMoveSpeed(Units[i])/GetUnitDefaultMoveSpeed(Units[i]))
endif
endif
endfunction
private function LeftUp_Func takes integer i returns nothing
set Left[i] = false
if (not Up[i])and(not Down[i]) then
call IssueImmediateOrder( Units[i], "stop" )
endif
endfunction
private function RightUp_Func takes integer i returns nothing
set Right[i] = false
if (not Up[i])and(not Down[i]) then
call IssueImmediateOrder( Units[i], "stop" )
endif
endfunction
public function GetUnit takes player p returns unit
return Units[GetPlayerId(p)]
endfunction
public function RemoveAssociate takes player p returns nothing
set Units[GetPlayerId(p)] = null
endfunction
public function Associate takes unit u, player p, integer a, boolean b returns nothing
if(Units[GetPlayerId(p)]!=null)then
call SetUnitTimeScale(Units[GetPlayerId(p)],1)
call SetUnitAnimation(Units[GetPlayerId(p)], "stand")
endif
set Units[GetPlayerId(p)] = u
set Anims[GetPlayerId(p)] = a
set Arround[GetPlayerId(p)] = b
set FlagArround[GetPlayerId(p)] = false
endfunction
public function RemoveControlUnit takes player p returns nothing
call RemoveUnit(Units[GetPlayerId(p)])
set Units[GetPlayerId(p)] = null
endfunction
public function SetControl takes player p, boolean b returns nothing
local integer i = GetPlayerId(p)
set ApplyControl[i] = b
if not b then
call UpUp_Func(i)
call DownUp_Func(i)
call RightUp_Func(i)
call LeftUp_Func(i)
endif
endfunction
public function SetCamera takes player p, boolean b returns nothing
set ApplyCam[GetPlayerId(p)] = b
if(GetLocalPlayer()==p)then
if not b then
call ResetToGameCamera(0)
call CameraSetSmoothingFactor(0)
else
call SetCameraTargetController(Units[GetPlayerId(p)],0,0,false)
call CameraSetSmoothingFactor(1)
endif
endif
endfunction
public function GetControl takes player p returns boolean
return ApplyControl[GetPlayerId(p)]
endfunction
public function GetCamera takes player p returns boolean
return ApplyCam[GetPlayerId(p)]
endfunction
private function UpUp_Actions takes nothing returns nothing
local integer i = GetPlayerId(GetTriggerPlayer())
call UpUp_Func(i)
endfunction
private function UpDown_Actions takes nothing returns nothing
local integer i = GetPlayerId(GetTriggerPlayer())
set Up[i] = true
if GetUnitState(Units[i],UNIT_STATE_LIFE)>=0.45 and ApplyControl[i] then
if(Anims[i]!=-1)then
call SetUnitAnimationByIndex(Units[i],Anims[i])
endif
call SetUnitTimeScale(Units[i], GetUnitMoveSpeed(Units[i])/GetUnitDefaultMoveSpeed(Units[i]))
endif
endfunction
private function DownUp_Actions takes nothing returns nothing
local integer i = GetPlayerId(GetTriggerPlayer())
call DownUp_Func(i)
endfunction
private function DownDown_Actions takes nothing returns nothing
local integer i = GetPlayerId(GetTriggerPlayer())
set Down[i] = true
if GetUnitState(Units[i],UNIT_STATE_LIFE)>=0.45 and ApplyControl[i] and Anims[i]!=-1 then
call SetUnitAnimationByIndex(Units[i],Anims[i])
endif
if(not Up[i])and(Arround[i])then
call SetUnitTimeScale(Units[i], GetUnitMoveSpeed(Units[i])/GetUnitDefaultMoveSpeed(Units[i]))
set FlagArround[i] = true
call SetUnitFacingTimed( Units[i], (GetUnitFacing(Units[i])+180), 0 )
else
call SetUnitTimeScale(Units[i], -GetUnitMoveSpeed(Units[i])/GetUnitDefaultMoveSpeed(Units[i]))
endif
endfunction
private function RightUp_Actions takes nothing returns nothing
local integer i = GetPlayerId(GetTriggerPlayer())
call RightUp_Func(i)
endfunction
private function RightDown_Actions takes nothing returns nothing
local integer i = GetPlayerId(GetTriggerPlayer())
set Right[i] = true
endfunction
private function LeftUp_Actions takes nothing returns nothing
local integer i = GetPlayerId(GetTriggerPlayer())
call LeftUp_Func(i)
endfunction
private function LeftDown_Actions takes nothing returns nothing
local integer i = GetPlayerId(GetTriggerPlayer())
set Left[i] = true
endfunction
private function Move_Actions takes nothing returns nothing
local real X
local real Y
local real Xh
local real Yh
local boolean bx
local boolean by
local real Angle
local integer i = 0
local integer iflag = 0
local boolean bflag = false
loop
exitwhen i >= COUNT_OF_PLAYERS
if (Units[i] != null)and(ApplyControl[i]) then
if Up[i] and (not Down[i]) and ConditionToMove(Units[i]) then
set bflag = true
set iflag = 1
elseif (not Up[i]) and Down[i] and ConditionToMove(Units[i]) then
set bflag = true
if(FlagArround[i])then
set iflag = 1
else
set iflag = -1
endif
else
set bflag = false
endif
if bflag then
set Angle = GetUnitFacing(Units[i])
set X = GetUnitX(Units[i])+iflag*(GetUnitMoveSpeed(Units[i])/100)*Cos(Angle*bj_DEGTORAD)
set Y = GetUnitY(Units[i])+iflag*(GetUnitMoveSpeed(Units[i])/100)*Sin(Angle*bj_DEGTORAD)
set Xh = GetUnitX(Units[i])
set Yh = GetUnitY(Units[i])
call SetUnitPosition(Units[i],X,Y)
call ClearTextMessages()
if(RAbsBJ(GetUnitX(Units[i])-X)>0.5)or(RAbsBJ(GetUnitY(Units[i])-Y)>0.5)then
call SetUnitPosition(Units[i],X,Yh)
set bx = RAbsBJ(GetUnitX(Units[i])-X)<=0.5
call SetUnitPosition(Units[i],Xh,Y)
set by = RAbsBJ(GetUnitY(Units[i])-Y)<=0.5
if bx then
call SetUnitPosition(Units[i],X,Yh)
elseif by then
call SetUnitPosition(Units[i],Xh,Y)
else
call SetUnitPosition(Units[i],Xh,Yh)
endif
endif
endif
if Right[i] and (not Left[i]) and ConditionToMove(Units[i]) then
set bflag = true
set iflag = 1
elseif (not Right[i]) and Left[i] and ConditionToMove(Units[i]) then
set bflag = true
set iflag = -1
else
set bflag = false
endif
if bflag then
if Down[i] then
call SetUnitFacingTimed( Units[i], (GetUnitFacing(Units[i])+iflag*INVERSE*(8.00)), 0.01 )
else
call SetUnitFacingTimed( Units[i], (GetUnitFacing(Units[i])-iflag*(8.00)), 0.01 )
endif
endif
endif
set i = i + 1
endloop
endfunction
private function Animation_Actions takes nothing returns nothing
local integer i = 0
loop
exitwhen i >= COUNT_OF_PLAYERS
if Up[i] and Down[i] and not ResetFlag[i] then
set ResetFlag[i] = true
call SetUnitAnimation(Units[i],"stand")
endif
if not ResetFlag[i] then
if (Up[i] or Down[i]) and ConditionToMove(Units[i]) and Anims[i]!=-1 then
call SetUnitAnimationByIndex(Units[i],Anims[i])
endif
endif
set i = i + 1
endloop
endfunction
private function Camera_Actions takes nothing returns nothing
local integer i = 0
local real Z1
local real Z2
local real Angle1
local real offset
local real Angle2
loop
exitwhen i >= COUNT_OF_PLAYERS
if (Units[i] != null) and (ApplyCam[i]) then
call MoveLocation(l,GetUnitX(Units[i]),GetUnitY(Units[i]))
set Z1 = GetLocationZ(l)
call MoveLocation(l,GetUnitX(Units[i])-400*Cos(GetUnitFacing(Units[i])*bj_DEGTORAD),GetUnitY(Units[i])-400*Sin(GetUnitFacing(Units[i])*bj_DEGTORAD))
set Z2 = GetLocationZ(l)
if(Z1+100>=Z2)then
set offset=GetCameraField(CAMERA_FIELD_ZOFFSET)+GetUnitFlyHeight(Units[i])+300+Z1-GetCameraEyePositionZ()
set Angle1 = -15
else
set offset=R2I(GetCameraField(CAMERA_FIELD_ZOFFSET))+300+GetUnitFlyHeight(Units[i])+Z2-R2I(GetCameraEyePositionZ())
set Angle1 = -50
endif
set Angle2 = GetUnitFacing(Units[i])
if (Down[i])and(not Up[i])and(Arround[i])then
set Angle2 = Angle2 + 180
endif
if(GetLocalPlayer()==Player(i))then
call SetCameraField(CAMERA_FIELD_ZOFFSET,offset,0.25)
call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE,450,0.25)
call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK,Angle1,0.25)
call SetCameraField(CAMERA_FIELD_FIELD_OF_VIEW,100,0.1)
call SetCameraField(CAMERA_FIELD_FARZ,bj_CAMERA_DEFAULT_FARZ,0.25)
call SetCameraField(CAMERA_FIELD_ROTATION,Angle2,0.25)
endif
endif
set i = i + 1
endloop
endfunction
private function init takes nothing returns nothing
local integer Count = 0
//create triggers
local trigger UpDown = CreateTrigger( )
local trigger UpUp = CreateTrigger( )
local trigger DownDown = CreateTrigger( )
local trigger DownUp = CreateTrigger( )
local trigger RightDown = CreateTrigger( )
local trigger RightUp = CreateTrigger( )
local trigger LeftDown = CreateTrigger( )
local trigger LeftUp = CreateTrigger( )
set l = Location(0,0)
// create events
loop
exitwhen Count>=COUNT_OF_PLAYERS
call TriggerRegisterPlayerEvent(UpDown, Player(Count), EVENT_PLAYER_ARROW_UP_DOWN)
call TriggerRegisterPlayerEvent(UpUp, Player(Count), EVENT_PLAYER_ARROW_UP_UP)
call TriggerRegisterPlayerEvent(DownDown, Player(Count), EVENT_PLAYER_ARROW_DOWN_DOWN)
call TriggerRegisterPlayerEvent(DownUp, Player(Count), EVENT_PLAYER_ARROW_DOWN_UP)
call TriggerRegisterPlayerEvent(LeftDown, Player(Count), EVENT_PLAYER_ARROW_LEFT_DOWN)
call TriggerRegisterPlayerEvent(LeftUp, Player(Count), EVENT_PLAYER_ARROW_LEFT_UP)
call TriggerRegisterPlayerEvent(RightDown, Player(Count), EVENT_PLAYER_ARROW_RIGHT_DOWN)
call TriggerRegisterPlayerEvent(RightUp, Player(Count), EVENT_PLAYER_ARROW_RIGHT_UP)
set Count = Count + 1
endloop
// create actions
call TriggerAddAction( UpDown, function UpDown_Actions )
call TriggerAddAction( UpUp, function UpUp_Actions )
call TriggerAddAction( DownDown, function DownDown_Actions )
call TriggerAddAction( DownUp, function DownUp_Actions )
call TriggerAddAction( RightDown, function RightDown_Actions )
call TriggerAddAction( RightUp, function RightUp_Actions )
call TriggerAddAction( LeftDown, function LeftDown_Actions )
call TriggerAddAction( LeftUp, function LeftUp_Actions )
call TimerStart(CreateTimer(),0.01,true,function Move_Actions)
call TimerStart(CreateTimer(),0.2,true,function Animation_Actions)
call TimerStart(CreateTimer(),0.01,true,function Camera_Actions)
endfunction
endlibrary
function InitTrig_SCS takes nothing returns nothing
endfunction