Action [TFS 1.2] Exercise Weapons

Discussion in 'Actions, MoveEvents & TalkActions' started by Discovery, Sep 6, 2018.

  1. Discovery

    Discovery Developing myself

    Joined:
    Apr 16, 2010
    Messages:
    297
    Likes Received:
    38
    Best Answers:
    3
    Hello guys, this is the first version of the new exercise weapons script!

    Feel free to help to make suggestions :)
    If you no have this new sprites, use another.. no have difference (like wodden dummy)

    [​IMG]


    Create the file exercise_training.lua

    Code (Lua):
    1.  
    2. local skills = {
    3.     [26397] = {id=SKILL_SWORD,voc=4},
    4.     [26398] = {id=SKILL_AXE,voc=4},
    5.     [26399] = {id=SKILL_CLUB,voc=4},
    6.     [26400] = {id=SKILL_DISTANCE,voc=3,range=CONST_ANI_SIMPLEARROW},
    7.     [26401] = {id=SKILL_MAGLEVEL,voc=2,range=CONST_ANI_ENERGY},
    8.     [26402] = {id=SKILL_MAGLEVEL,voc=1,range=CONST_ANI_FIRE},
    9. }
    10.  
    11. ------- CONFIG -----//
    12. local dummies = {26403,26404}
    13. local skill_gain = 1 -- per hit
    14. local gain_stamina = 60
    15.  
    16. local function start_train(pid,start_pos,itemid,fpos)
    17.     local player = Player(pid)
    18.     if player ~= nil then
    19.         local pos_n = player:getPosition()
    20.  
    21.         if start_pos:getDistance(pos_n) == 0 and getTilePzInfo(pos_n) then
    22.             if player:getItemCount(itemid) >= 1 then
    23.                 local exercise = player:getItemById(itemid,true)
    24.  
    25.                 if exercise:isItem() then
    26.                     if exercise:hasAttribute(ITEM_ATTRIBUTE_CHARGES) then
    27.                         local charges_n = exercise:getAttribute(ITEM_ATTRIBUTE_CHARGES)
    28.  
    29.                         if charges_n >= 1 then
    30.                             exercise:setAttribute(ITEM_ATTRIBUTE_CHARGES, (charges_n-1))
    31.  
    32.                             local required = 0
    33.                             local currently = 0
    34.                             local voc = player:getVocation()
    35.  
    36.                             if skills[itemid].id == SKILL_MAGLEVEL then
    37.                                 required = voc:getRequiredManaSpent(player:getBaseMagicLevel() + 1)/skill_gain
    38.                                 currently = player:getManaSpent()
    39.                                 player:addManaSpent(required - currently)
    40.                             else
    41.                                 required = voc:getRequiredSkillTries(skills[itemid].id, player:getSkillLevel(skills[itemid].id)+1)/skill_gain
    42.                                 currently = player:getSkillTries(skills[itemid].id)
    43.  
    44.                                 player:addSkillTries(skills[itemid].id, (required - currently))
    45.                             end
    46.  
    47.                             fpos:sendMagicEffect(CONST_ME_HITAREA)
    48.                             if skills[itemid].range then
    49.                                 pos_n:sendDistanceEffect(fpos, skills[itemid].range)
    50.                             end
    51.                             player:setStamina(player:getStamina() + 60)
    52.  
    53.                             if charges_n == 1 then
    54.                                 exercise:remove(1)
    55.                                 return true
    56.                             end
    57.                             local training = addEvent(start_train, voc:getAttackSpeed(), pid,start_pos,itemid,fpos)
    58.                         else
    59.                             exercise:remove(1)
    60.                             stopEvent(training)
    61.                         end
    62.                     end
    63.                 end
    64.             end
    65.         else
    66.             stopEvent(training)
    67.         end
    68.     else
    69.         stopEvent(training)
    70.     end
    71.     return true
    72. end
    73.  
    74. function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    75.     local start_pos = player:getPosition()
    76.  
    77.     if target:isItem() then
    78.         if isInArray(dummies,target:getId()) then
    79.             if not skills[item.itemid].range and (start_pos:getDistance(target:getPosition()) > 1) then
    80.                 stopEvent(training)
    81.                 return false
    82.             end
    83.             if not player:getVocation():getId() == skills[item.itemid].voc or not player:getVocation():getId() == (skills[item.itemid].voc+4) then
    84.                 stopEvent(training)
    85.                 return false
    86.             end
    87.             player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You started training.")
    88.             start_train(player:getId(),start_pos,item.itemid,target:getPosition())
    89.         end
    90.     end
    91.  
    92.     return true
    93. end
    94.  
    Added this line in actions.xml
    Code (Lua):
    1.  
    2.     <!-- Training -->
    3.     <action fromid="26397" toid="26402" script="exercise_training.lua" allowfaruse="1"/>
    4.  
    In events.xml

    - You will change enable="0" to "1".
    Code (Lua):
    1.  
    2.     <event class="Player" method="onTradeRequest" enabled="1" />
    3.  
    In /events/scripts/player.lua
    Added this lines in the top of the file
    Code (Lua):
    1.  
    2. local exercise_ids = {26397,26398,26399,26400,26401,26402}
    3.  
    - Go to the function function Player:eek:nMoveItem added this lines in the top.
    Code (Lua):
    1.  
    2.     -- Exercise Weapons
    3.     if isInArray(exercise_ids,item.itemid) then
    4.         self:sendCancelMessage('You cannot move this item outside this container.')
    5.         return false
    6.     end
    7.  
    8.  


    - Go to the function function Player:eek:nTradeRequest added this lines in the top.
    Code (Lua):
    1.  
    2. if isInArray(exercise_ids,item.itemid) then
    3.         return false
    4.     end
    5.  

    Have fun!
     
  2. Leesne

    Leesne New Member

    Joined:
    Dec 4, 2017
    Messages:
    10
    Likes Received:
    3
    Best Answers:
    0
    Haven't tested it yet on the server I'm working on, but looks good.
    Thanks for this.
     
  3. reddit1

    reddit1 Experienced G'

    Joined:
    May 27, 2014
    Messages:
    225
    Likes Received:
    39
    Best Answers:
    0
    Wow.. this is looking great! Keep up the good work!
     
  4. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,035
    Likes Received:
    1,847
    Best Answers:
    314
    looks cool friend
     
    Discovery likes this.
  5. hellboy

    hellboy Active Member

    Joined:
    Apr 6, 2008
    Messages:
    375
    Likes Received:
    51
    Best Answers:
    1
    It's nice.

    Think about "early exit" in functions. There are more readable in that way.

    Code (Text):
    1.  
    2. --- [...]
    3.   local player = Player(pid)
    4.   if player == nil then
    5.     stopEvent(training)
    6.     return true
    7.   end
    8.  
    9.   local pos_n = player:getPosition()
    10.   if start_pos:getDistance(pos_n) ~= 0 or not getTilePzInfo(pos_n) then
    11.     stopEvent(training)
    12.     return true
    13.   end
    14.  
    15.   if player:getItemCount(itemid) == 0 then
    16.     return true
    17.   end
    18. --- [...]
    19.  
     
  6. Discovery

    Discovery Developing myself

    Joined:
    Apr 16, 2010
    Messages:
    297
    Likes Received:
    38
    Best Answers:
    3
    Thank you @hellboy !

    I rewrite the code after this version and removed a lot of insecurity parts and wrong code architectures
    Now I can't edit this post .. but your suggestion is pretty excellent!
     
    hellboy likes this.
  7. Chriistian.L.B

    Chriistian.L.B Premium User Premium User

    Joined:
    Apr 17, 2008
    Messages:
    75
    Likes Received:
    13
    Best Answers:
    0
    Share your update code ? please !
     
  8. Vulcan_

    Vulcan_ Support Team Support Team

    Joined:
    Feb 14, 2015
    Messages:
    4,035
    Likes Received:
    1,847
    Best Answers:
    314
    report your post with a pastebin link with the new code requesting an update, the mods will update your main post with the new code
     
  9. oualid6496

    oualid6496 Premium User Premium User

    Joined:
    Aug 24, 2014
    Messages:
    332
    Likes Received:
    50
    Best Answers:
    0
    Nice! Gonna test it out for sure.
     

Share This Page

Loading...