• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] A difficult problem, 2 jass triggers

Status
Not open for further replies.
Level 14
Joined
Jul 1, 2008
Messages
1,314
Hey guys,
i have a problem with 2 triggers.

Goal:

I want a pet for the hero, which hunts on his farm, if its allowed to, and the hunting should change with the Unit-Level of the pet.

So i got this integer variable array: [4 Players]
JASS:
udg_GP_Pet_HANDLING[]
[0-3] : How many targets per hunt?
[4-7] : 1= Pet is hunting, 0 = Pet isnt hunting
[8-11] : Hunting Moral, 100% = max.
[16-19] : 1 = Pet is Allowed to act automaticly, 0 = Pet is not ~
[20-23] : Target Ids Stored in those arrays

The 2 important Triggers

Organisations

1. Trigger is controlling everything, when the Pet's mana becomes less then 50 and 2. Trigger is controlling the settings, when a target unit dies, and one is

1.Trigger
JASS:
function PSE_HandleTargets takes nothing returns boolean
local unit u = GetFilterUnit()
if GetUnitTypeId(u) == udg_GP_Pet_HANDLING[udg_TempInt+20] then
   if GetUnitState(u,UNIT_STATE_LIFE) > 0 then
      set u = null
      return true
   endif
endif
 set u = null
 return false 
endfunction

function PSE_PetIsAllowed takes nothing returns boolean
  local integer i = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
  if udg_GP_Pet_HANDLING[i+16] == 1 and udg_GP_Pet_HANDLING[i+4] == 0 then
     return true
  endif
     return false
endfunction

function PSE_SetManaStatePet takes unit Pet, real State, integer UnitLvl returns nothing
// [....] This only sets the mana needed to hunt the targets
endfunction

function PSE_WaitForUnitDeathOrTimeOut takes unit Pet, unit targ2, timer t,integer UnitLvl, integer PlayerIndex returns boolean
   local boolean TargetsDead = false
   if UnitLvl >= 5 and udg_GP_Pet_HANDLING[PlayerIndex] == 1 and GetUnitCurrentOrder(Pet) != OrderId("attack") then
      if targ2 != null then   
         call IssueTargetOrder(Pet,"attack",targ2)
      else
call DisplayTextToPlayer(Player(0),0,0,"targ2 == null")
         set udg_GP_Pet_HANDLING[PlayerIndex+4] = 0
      endif
   endif
   // End Pet Hunt, Timing or Targets == null 
   if udg_GP_Pet_HANDLING[PlayerIndex+4] == 0 or TimerGetRemaining(t) <= 1.00 then
      set TargetsDead = true
   endif
call DisplayTextToPlayer(Player(0),0,0,"Loop")
   return TargetsDead
endfunction
            

function Trig_Pet_Selbstversorgung_Execute_Actions takes nothing returns nothing
    local unit Pet = GetTriggerUnit()
    local player Owner = GetOwningPlayer(Pet)
    local integer PlayerIndex = GetPlayerId(Owner)
    local unit PetHome = ConvertPlayerIndexToPetHome(PlayerIndex)
    local real State = GetUnitState(Pet,UNIT_STATE_MANA)
    local boolean efficient = false
    local unit targ1
    local unit targ2
    local group g
    local timer t
    local integer UnitLvl = GetUnitLevel(Pet)
call DisplayTextToPlayer(Player(0),0,0,"Trigger_Pet_Selbstversorgung")
    // 1. Search for something to eat in the house    
    if CountItemsOfTypeFromUnit(Pet,'I000') > 0 then
       call RemoveItem(GetItemOfTypeFromUnitBJ(Pet,'I000'))
       set State = State + 100.00
       call SetUnitState(Pet,UNIT_STATE_MANA,State) 
       call DisplayTextToPlayer(Player(PlayerIndex),0,0,"[|c000000ffHaustier|r]: Hat sich satt gefressen. Jetzige Ausdauer: |c00ffcc00" + R2S(State)+"|r" )
    else
       if CountItemsOfTypeFromUnit(PetHome,'I000') > 0 then
          call RemoveItem(GetItemOfTypeFromUnitBJ(PetHome,'I000'))
          set State = State + 100.00
          call SetUnitState(Pet,UNIT_STATE_MANA,State) 
          call DisplayTextToPlayer(Player(PlayerIndex),0,0,"[|c000000ffHaustier|r]: Hat sich satt gefressen. Jetzige Ausdauer: |c00ffcc00" + R2S(State)+"|r" )
       else
           // 2. PET HUNT, anything stored
           set udg_GP_Pet_HANDLING[PlayerIndex+4] = 1
           if GetRandomInt(1,100) <= udg_GP_Pet_HANDLING[PlayerIndex+8] then
              set efficient = true
           endif
           // EFFICIENT == FALSE -> Doesnt Want to hunt
           if efficient == true then
              set udg_TempInt = PlayerIndex
              set g = CreateGroup()
              call GroupEnumUnitsInRect(g,GetPlayableMapRect(),Condition(function PSE_HandleTargets))
              set targ1 = GroupPickRandomUnit(g)
call DisplayTextToPlayer(Player(0),0,0,GetUnitName(targ1))
              set udg_GP_Pet_HANDLING[PlayerIndex] = 1
              if UnitLvl >= 5 then
                 call GroupRemoveUnit(g,targ1)
                 set targ2 = GroupPickRandomUnit(g)
                 if targ1 != null then
                    set udg_GP_Pet_HANDLING[PlayerIndex] = 2
                 else
                    set udg_GP_Pet_HANDLING[PlayerIndex] = 1
                 endif
              endif
              // set Pet in Action, if targ1/targ2 != null, then >= 1 targ is alive
              if targ1 != null or targ2 != null or udg_GP_Pet_HANDLING[PlayerIndex+20] == 0 then
call DisplayTextToPlayer(Player(0),0,0,"targtrue")
                 if not RectContainsCoords(gg_rct_0,GetUnitX(Pet),GetUnitY(Pet)) then
                    call SetUnitX(Pet,GetRectCenterX(gg_rct_0))
                    call SetUnitX(Pet,GetRectCenterY(gg_rct_0))
                 endif
                 call PSE_SetManaStatePet(Pet,State,UnitLvl)
                 call TriggerSleepAction(0.20)
                 if targ1 != null then
                    call IssueTargetOrder(Pet,"attack",targ1)
                 else
                    call IssueTargetOrder(Pet,"attack",targ2)
                 endif
                 // WAIT FOR DEATH OF TARG1/ TARG2 or ExpiredTime
                 set t = CreateTimer()
                 call TimerStart(t,98.80,false,null)
                 loop
call DisplayTextToPlayer(Player(0),0,0,R2S(TimerGetRemaining(t)))
call DisplayTextToPlayer(Player(0),0,0,"Hunt == " + I2S(udg_GP_Pet_HANDLING[PlayerIndex+4])) 
                     exitwhen PSE_WaitForUnitDeathOrTimeOut(Pet,targ2,t,UnitLvl,PlayerIndex) == true
                     call TriggerSleepAction(0.10)
                 endloop
                 // All Targets are dead
                 call SetUnitX(Pet,GetRectCenterX(gg_rct_1))
                 call SetUnitX(Pet,GetRectCenterY(gg_rct_1))
call DisplayTextToPlayer(Player(0),0,0,"End of Selbsversorgung")
              else
                 // TARGTRUE == FALSE -> Any Targets there
                 call SimError(GetOwningPlayer(Pet),"Kein Beutetier ("+UnitId2StringBJ(udg_GP_Pet_HANDLING[PlayerIndex+20])+") auf der Farm.") 
              endif
           else
              call SimError(GetOwningPlayer(Pet),"Das Haustier ist nicht in Jagdlaune.")
              call DestroyGroup(g)
           endif
       endif
    endif
    set udg_GP_Pet_HANDLING[PlayerIndex+4] = 0
    set udg_GP_Pet_HANDLING[PlayerIndex] = 0
    // reset leaks
    set Pet = null
    set Owner = null
    set targ1 = null
    set targ2 = null
    set g = null 
    set PetHome = null
    call DestroyTimer(t)
    set t = null
endfunction

//===========================================================================
function InitTrig_Pet_Selbstversorgung_Execute takes nothing returns nothing
    set gg_trg_Pet_Selbstversorgung_Execute = CreateTrigger(  )
    call TriggerAddCondition(gg_trg_Pet_Selbstversorgung_Execute,Condition(function PSE_PetIsAllowed))
    call TriggerAddAction( gg_trg_Pet_Selbstversorgung_Execute, function Trig_Pet_Selbstversorgung_Execute_Actions )
endfunction

2. Trigger

JASS:
function Trig_Pet_Handle_Target_Death_Actions takes nothing returns nothing
    local unit Killer = GetKillingUnit()
    local integer i = GetPlayerId(GetOwningPlayer(Killer))
    local unit Dying = GetDyingUnit()
call DisplayTextToPlayer(Player(0),0,0,"Trigger_Target_Death")
call DisplayTextToPlayer(Player(0),0,0,I2S(udg_GP_Pet_HANDLING[i+4]))
    if GetUnitTypeId(Dying) == 'e000' then
        // Automatic hunt, count down the rabbits
        if udg_GP_Pet_HANDLING[i] > 0 and udg_GP_Pet_HANDLING[i+4] == 1 then
            set udg_GP_Pet_HANDLING[i] = udg_GP_Pet_HANDLING[i] - 1
        else
            set udg_GP_Pet_HANDLING[i+4] = 0
        endif
            // Everytime, a rabbit dies
            call SetUnitState(Killer,UNIT_STATE_LIFE,GetUnitState(Killer,UNIT_STATE_LIFE)+50)
            call SetUnitState(Killer,UNIT_STATE_MANA,GetUnitState(Killer,UNIT_STATE_MANA)+100)
            call SetUnitUserData(Killer,GetUnitUserData(Killer)+5)
            set udg_SpecialFX = AddSpecialEffect("Objects\\Spawnmodels\\Orc\\Orcblood\\BattrollBlood.mdl",GetUnitX(Dying),GetUnitY(Dying))
            call TriggerExecute(gg_trg_Special_Effect_Destruction)
    endif
endfunction

//===========================================================================
function InitTrig_Pet_Handle_Target_Death takes nothing returns nothing
    set gg_trg_Pet_Handle_Target_Death = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Pet_Handle_Target_Death, EVENT_PLAYER_UNIT_DEATH )
    call TriggerAddAction( gg_trg_Pet_Handle_Target_Death, function Trig_Pet_Handle_Target_Death_Actions )
endfunction

THE PROBLEM

Well, as you can see, there are many debugg messages in these triggers, but i dont know why, they dont run as they should.

1.Trigger 1 sets udg_GP_Pet_HANDLING[PlayerIndex] to 1 and udg_GP_Pet_HANDLING[PlayerIndex+4] to 1 EVERYTIME, even if i set them to 0 at the end of the trigger.

2.The Debugg message in the loop only appears ONCE, so the loop is somehow ended, i have no idea, why the loop should set the var TargetsDead=true.:eek:

Your Help

I know, this is very long, but i dont see, whar i can do more. I tested it several hours and i cant fix this...

I really need your help guys! :hohum

Maybe you can read through this, and if you see something wrong, please comment :cry:

THANKS IN ADVANCE and dont mind the german names :wink:

Greets
Emm-A-
 
Level 11
Joined
Apr 6, 2008
Messages
760
i will look through it, i'll get back when i have solved it :)

1.Trigger 1 sets udg_GP_Pet_HANDLING[PlayerIndex] to 1 and udg_GP_Pet_HANDLING[PlayerIndex+4] to 1 EVERYTIME, even if i set them to 0 at the end of the trigger.

JASS:
set udg_GP_Pet_HANDLING[PlayerIndex+4] = 1
//=====some where else=========
set udg_GP_Pet_HANDLING[PlayerIndex] = 1 // Found 2 of these in the trigger

maybe these are the problem?

Edit:

any chance u can post some kind of map where i can try the triggers? it's quite hard to figure out 2'nd one
 
Level 14
Joined
Jul 1, 2008
Messages
1,314
hey Ciebron, didnt see you edited it.

I'm really glad that you want to help me :)

I will translate the comments in english and then send the map via Pn to you :D

Btw: the two things you found, are meant to be there. One is not nessecary i know (if you mean these:
JASS:
 set udg_GP_Pet_HANDLING[PlayerIndex] = 1
              if UnitLvl >= 5 then
                 call GroupRemoveUnit(g,targ1)
                 set targ2 = GroupPickRandomUnit(g)
                 if targ1 != null then
                    set udg_GP_Pet_HANDLING[PlayerIndex] = 2
                 else
                    set udg_GP_Pet_HANDLING[PlayerIndex] = 1
 
Status
Not open for further replies.
Top