CreatureEvent Passive Spells tfs 1.2

Discussion in 'GlobalEvents, Spells & CreatureEvents' started by xydius, Sep 4, 2017.

  1. xydius

    xydius Just an otland wanderer

    Joined:
    Feb 25, 2016
    Messages:
    43
    Likes Received:
    25
    Best Answers:
    0
    Hello fellow otlanders,
    Im releasing this simple "script" i just made for my custom ot where players have a passive spell which are cast from time to time while the player is in combat and has a target.

    1. First go to creaturescripts and create a new file and name it however you want and paste this
      Code (Lua):
      1. local config = {
      2.     enabled = true,
      3.     doChance = 10000, --chance to do spell 1000 = 1 %
      4.     TALKTYPE_ORANGE_1 = TALKTYPE_MONSTER_SAY,
      5.     TALKTYPE_ORANGE_2 = TALKTYPE_MONSTER_YELL
      6. }
      7.  
      8. local voc = {
      9.     {
      10.         id = 1, --sorc
      11.         spellname = "passive spell", --TODO think for proper names
      12.         combatDamage = COMBAT_DEATHDAMAGE,
      13.         CombatEffect = CONST_ME_MORTAREA,
      14.         distEffect = CONST_ANI_DEATH
      15.  
      16.     },
      17.     {
      18.         id = 2, --druid
      19.         spellname = "passive spell",
      20.         combatDamage = COMBAT_ICEDAMAGE,
      21.         CombatEffect = CONST_ME_ICEAREA,
      22.         distEffect = CONST_ANI_ICE
      23.  
      24.     },
      25.     {
      26.         id = 3, --pally
      27.         spellname = "passive spell",
      28.         combatDamage = COMBAT_PHYSICALDAMAGE,
      29.         CombatEffect = CONST_ME_HITAREA,
      30.         distEffect = CONST_ANI_THROWINGSTAR
      31.  
      32.     },
      33.     {
      34.         id = 4, --knight
      35.         spellname = "passive spell",
      36.         combatDamage = COMBAT_PHYSICALDAMAGE,
      37.         CombatEffect = CONST_ME_DRAWBLOOD,
      38.         distEffect = CONST_ANI_LARGEROCK
      39.     },
      40. }
      41.  
      42.  
      43.  
      44.  
      45.  
      46.  
      47.  
      48. function onThink(creature, interval)
      49.     if config.enabled then
      50.         if creature:isPlayer() then
      51.             local target = creature:getTarget()
      52.             if target then
      53.                 local chance = math.random(1, 100000)
      54.                 if chance <= config.doChance then
      55.                     local vocation = creature:getVocation():getBase():getId()
      56.                     for i=1, #voc do
      57.                         if vocation == voc[i].id then
      58.                             local level = creature:getLevel()
      59.                             local min = 0
      60.                             local max = 0
      61.                             if vocation == 1 or vocation == 2 then
      62.                                 local maglevel = creature:getMagicLevel()
      63.                                 min = (level / 5) + (maglevel * 1.4) + 8
      64.                                 max = (level / 5) + (maglevel * 2.2) + 14
      65.                             end
      66.                             if vocation == 3 then
      67.                                 local weapontype = nil
      68.                                 local slotItem = creature:getSlotItem(CONST_SLOT_LEFT)
      69.                                 if    slotItem then
      70.                                     weaponType = slotItem:getType():getWeaponType()
      71.                                 end
      72.                                 local skill = SKILL_FIST
      73.                                 if weaponType == 5 then
      74.                                     skill = SKILL_DISTANCE
      75.                                 end
      76.                                 local skilllvl = creature:getEffectiveSkillLevel(skill)
      77.                                 min = (level / 5) + skilllvl * 2
      78.                                 max = (level / 5) + skilllvl * 5
      79.                             end
      80.                             if vocation == 4 then
      81.                                 local weapontype = nil
      82.                                 local attack = 10
      83.                                 local uid = nil
      84.                                 local slotItem = creature:getSlotItem(CONST_SLOT_LEFT)
      85.                                 if slotItem then
      86.                                     uid = slotItem:getUniqueId()
      87.                                     attack = getItemAttribute(uid, ITEM_ATTRIBUTE_ATTACK, true)
      88.                                     weaponType = slotItem:getType():getWeaponType()
      89.                                 end
      90.                                 local skill = SKILL_FIST
      91.                                 if weaponType == 1 then
      92.                                     skill = SKILL_SWORD
      93.                                 end
      94.                                 if weaponType == 2 then
      95.                                     skill = SKILL_CLUB
      96.                                 end
      97.                                 if weaponType == 3 then
      98.                                     skill = SKILL_AXE
      99.                                 end
      100.  
      101.                                 local skilllvl = creature:getEffectiveSkillLevel(skill)
      102.                                 min = (level / 5) + (skilllvl * attack * 0.01) + 1
      103.                                 max = (level / 5) + (skilllvl * attack * 0.03) + 6
      104.                             end
      105.      
      106.  
      107.                             doCreatureSay(creature, voc[i].spellname, config.TALKTYPE_ORANGE_1)
      108.                             doSendDistanceShoot(creature:getPosition(), target:getPosition(), voc[i].distEffect)
      109.                             doTargetCombatHealth(creature, target, voc[i].combatDamage, -min, -max, voc[i].CombatEffect)
      110.                         end
      111.                     end
      112.                    
      113.                 end
      114.             end
      115.             if not target then
      116.                 creature:unregisterEvent("PassiveSpells")
      117.             end
      118.         end
      119.     end
      120. end
    2. Add this script on your creaturescripts.xml file like this just remmber to change "passive.lua" to your filename
      Code (Text):
      1. <!-- Passive Spells -->
      2.  <event type="think" name="PassiveSpells" script="passive.lua" />
    • Register the event, I suggest you register the event in creature.lua in events folder in "Creature: onTargetCombat(target)" function right after "if not self then return true end" add this
      Code (Lua):
      1. if self:isPlayer() and target:isMonster() then
      2.         self:registerEvent("PassiveSpells")
      3.     end
    And thats pretty much it, it is a bit configurable but it lacks of some stuff for example passive spells can go through walls LOL and have no exhaustion thats why i recommend leaving a low chance to cast.

    P.D.
    It uses the exact same formulas for "exori mort" for mages, "exori con" for paladins and "exori hur" for knights in tfs 1.2 version 10.98-10.99
    If i add some of this later I will edit this post.
    If you encounter any issue let me know and ill try to fix it.
    If you have any suggestion feel free to let me know so i can improve this code.
     
    Peonso, AdiMit and Homeslice like this.
  2. Yuggi

    Yuggi Member

    Joined:
    May 18, 2017
    Messages:
    64
    Likes Received:
    8
    Best Answers:
    0
    This is really interesting and I have never used onThink, could somebody explain what it does?
    onThink(creature, interval) what's the interval there?
     
  3. xydius

    xydius Just an otland wanderer

    Joined:
    Feb 25, 2016
    Messages:
    43
    Likes Received:
    25
    Best Answers:
    0
    Im actually new to onThink also while i was looking how to do this i came up with this.
    What i understand about it is that once it is triggered, every 500 ms it tries to do whatever it is supposed to do.
    As far as i know to change the interval it might need source edits (someone correct me if this not the case) but i guess it also can be done with a counter for example.
    Code (Lua):
    1. local count = 0
    2.  
    3. function onThink(creature, interval)
    4. count++ (dont know if count++ work havent tried it in lua)
    5. if count == 4  then
    6.     --do stuff
    7. count =0
    8. end
    9. end
    so every 4 "loops" it will do the stuff so basically 500ms * 4 == 2 seconds

    Thats why i unregister the event once you are not targeting anything because i really dont know it it lags the server or not.
     
  4. ond

    ond Well-Known Member

    Joined:
    Mar 24, 2008
    Messages:
    2,587
    Likes Received:
    363
    Best Answers:
    12
    I think onThink interval is executed every 1000 ms. You could handle it by storage value. Also, ++ operator doesn't exist in LUA, use count = count + 1
     
  5. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    3,870
    Likes Received:
    1,754
    Best Answers:
    292
    Lua
     
    Nekiro and ond like this.
  6. kito2

    kito2 https://mtibia.online

    Joined:
    Mar 9, 2009
    Messages:
    3,582
    Likes Received:
    174
    Best Answers:
    1
    Any gif/video to see how it works?
     
  7. Homeslice

    Homeslice -anoyn/Rage the Mage

    Joined:
    May 9, 2010
    Messages:
    109
    Likes Received:
    50
    Best Answers:
    2
    If player has target, has chance to cast spell.
     
  8. Yuggi

    Yuggi Member

    Joined:
    May 18, 2017
    Messages:
    64
    Likes Received:
    8
    Best Answers:
    0
    sholdnt the script "return true" somewhere after it has executed?
    im thinking else it wil case lagg?
     
  9. xydius

    xydius Just an otland wanderer

    Joined:
    Feb 25, 2016
    Messages:
    43
    Likes Received:
    25
    Best Answers:
    0
    Uhh maybe, i havent really tested it with a full server just me doing random stuff on a cuatom server im doing probably the best idea is to add return true before last end on the creaturescripts file
     
  10. Yuggi

    Yuggi Member

    Joined:
    May 18, 2017
    Messages:
    64
    Likes Received:
    8
    Best Answers:
    0
    yeah because my cpu usage went up to 150% from 10% after the script has been runing for some time, and i just did small minor edits that shouldnt affect it ^^
    or maybe this onthink thing just causes lag
     
  11. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    3,870
    Likes Received:
    1,754
    Best Answers:
    292
    150% ?
    oh shit
     
  12. Yuggi

    Yuggi Member

    Joined:
    May 18, 2017
    Messages:
    64
    Likes Received:
    8
    Best Answers:
    0
    do u have to post useless replies in all threds?
    yes if you use top command in linux it will go over 100% if it goes over the amount the cpu can handle and if u have 4 cores and all of them are fully used it will say 400%.
     
  13. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    3,870
    Likes Received:
    1,754
    Best Answers:
    292
  14. xydius

    xydius Just an otland wanderer

    Joined:
    Feb 25, 2016
    Messages:
    43
    Likes Received:
    25
    Best Answers:
    0
    Holy shit thats a lot lol when i get back home from vacations i might look at that and maybe come with a better idea
     
  15. Yuggi

    Yuggi Member

    Joined:
    May 18, 2017
    Messages:
    64
    Likes Received:
    8
    Best Answers:
    0
    soooo! guys any idea why my server laggs when using this `?

    Code (Text):
    1.  
    2. function onThink(creature, interval)
    3.     if creature:isPlayer() then
    4.         local target = creature:getTarget()
    5.         if target then
    6.             if creature:getStorageValue(7608) == 2 then
    7.                 local cooldowna = creature:getStorageValue(23157)
    8.                if cooldowna ~= nil then
    9.                     creature:setStorageValue(23157, 0)
    10.                 end      
    11.                 if cooldowna >= 5 then
    12.                     creature:setStorageValue(23157, 0)
    13.                     if creature:getPosition():isSightClear(target:getPosition()) then
    14.                         Position(target:getPosition() + Position(-6, -5, 0)):sendDistanceEffect(target:getPosition(), 34)
    15.                         doTargetCombatHealth(creature, target, COMBAT_FIREDAMAGE, -(creature:getLevel() * 1.5 * 0.7), -(creature:getLevel() * 2 * 0.8), CONST_ME_FIREAREA)
    16.                     end
    17.                 else
    18.                     creature:setStorageValue(23157, cooldowna+1)
    19.                 end
    20.             end
    21.         end
    22.     end
    23. end
    24. [/img]
    25.  
    26. the idea is after a player has gained a storagevalue he will automatically shoot this spell at his target every 5seconds. but for some reason it laggs the server? my cpu usage goes very high and stuff like i said in some posts before
     
  16. RazorBlade

    RazorBlade Retired Snek

    Joined:
    Nov 7, 2009
    Messages:
    2,028
    Likes Received:
    607
    Best Answers:
    1
    first of all why are you setting the storage to 0 if the cooldown isn't nil? it'll never be nil, if they don't have the storage it's -1 not nil. You're basically just setting it to 0 every second. It should never be able to reach 5. It should never be able to reach 2. Does your spell even cast?
     
  17. Jo3Bingham

    Jo3Bingham Well-Known Member

    Joined:
    Mar 3, 2008
    Messages:
    967
    Likes Received:
    403
    Best Answers:
    3
    isSightClear() is very costly to call, and onThink() is triggered every 1000ms. Which means, potentially, it’s being called every second. That’s why you’re lagging and seeing high CPU usage.
     
  18. Yuggi

    Yuggi Member

    Joined:
    May 18, 2017
    Messages:
    64
    Likes Received:
    8
    Best Answers:
    0
    @RazorBlade yes it work.. every second they get +1 storage idk much about coding but its working (only lagg is the problem but i don tthink anything u mentioned affects this lagg i have)

    hmm only few players like 6 has this on my server, but it doesnt lagg from the start it only laggs after being online for some hours.. hmm maybe there is somethink other wrong? and isSightClear i think is needed else players could shoot through walls
     
  19. Jo3Bingham

    Jo3Bingham Well-Known Member

    Joined:
    Mar 3, 2008
    Messages:
    967
    Likes Received:
    403
    Best Answers:
    3
    Yes, that may be, but it’s still probably the cause of the lag and high CPU usage. You could remove it from the script to see if it is. If you remove it and it doesn’t help then you can rule it out.
     
  20. Madzix

    Madzix Member

    Joined:
    Sep 8, 2016
    Messages:
    39
    Likes Received:
    52
    Best Answers:
    9
    @Jo3Bingham
    Actually "isSightClear" isn't that much costly at all event the tfs path finding using it to check per node so the server might end using this function even ten thousands times every seconds.
    [​IMG]
    However the script provided by Yuggi shouldn't work at all not to mention lagging.
     
    Jo3Bingham likes this.

Share This Page

Loading...