Advertisement
Guest User

Chess Event LUAappArc

a guest
Jan 8th, 2011
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 49.13 KB | None | 0 0
  1. --[[=========================================
  2.  _     _    _
  3. | |   | |  | |  /\                  /\
  4. | |   | |  | | /  \   _ __  _ __   /  \   _ __ ___
  5. | |   | |  | |/ /\ \ | '_ \| '_ \ / /\ \ | '__/ __|
  6. | |___| |__| / ____ \| |_) | |_) / ____ \| | | (__
  7. |______\____/_/    \_\ .__/| .__/_/    \_\_|  \___|
  8.   Scripting Project  | |   | | Improved LUA Engine
  9.                      |_|   |_|
  10.    SVN: http://svn.assembla.com/svn/LUAppArc
  11.    LOG: http://burning-azzinoth.de/arcemu/new/frontend/?t=2
  12.    ----------------------
  13.    Original Code: Kenuvis
  14.    Build 219 user_version
  15.    KI Build 1
  16.    ----------------------
  17.    Dont delete my Credits!!!]]
  18.  
  19. --[[
  20. Code by Kenuvis
  21. LuAppArc Scripting Project
  22. ]]
  23. --User Area
  24. ------------
  25.  
  26. --if you use Lua++ write 18, if normal Lua write 6
  27. local Lua_using = 18
  28.  
  29. --if someone wins, he would random get one of these items
  30. local Reward_Items = {23714, 23716, 23713, 34499, 34492, 34493, 33223, 38301, 32566, 32588, 19979}
  31.  
  32. -- if you want to spawn a GO (like a chest) after win the game (0 = no GO)
  33. local GO_ID = 0
  34.  
  35. --size of one chessfield (developed for 6)
  36. local size = 6
  37.  
  38. --If all Units stop working, whatever happened, after this time,
  39. --it should be work again. (reset = true/false; time in ms)
  40. local reset = true
  41. local auto_reset_timer = 180000
  42.  
  43. --i hope, its clear (autosave = true/false; time in ms)
  44. local autosave = false
  45. local autosave_timer = 180000
  46.  
  47. --Max.count of Saves. If the limit is exceeded, the oldest save would be delete.
  48. local max_save = 20
  49.  
  50. --Time in milliseconds, until a Unit would be selected, during stand in the Unit
  51. local Select_Timer = 3000
  52.  
  53. --Time in seconds to move a NPC or select another
  54. local Pawn_Timer = 8
  55. local Castle_Timer = 10
  56. local Horse_Timer = 8
  57. local Bishop_Timer = 10
  58. local Queen_Timer = 10
  59. local King_Timer = 8
  60.  
  61. --NPC to start the Event (gossiptalk)
  62. local Start_NPC = 198765
  63. --little Eventmanager (only for holding some functions)
  64. local Manage_NPC = 198766
  65.  
  66. --if you want to flow your serverwindow with numbers and other things, change it to "true"
  67. local debug = false
  68.  
  69. --------------------------
  70. --      ID Area         --
  71. --------------------------
  72.  
  73. local Horde_Pawn_ID = 17469
  74. local Horde_Castle_ID = 21726
  75. local Horde_Horse_ID = 21748
  76. local Horde_Bishop_ID = 21747
  77. local Horde_Queen_ID = 21750
  78. local Horde_King_ID = 21752
  79.  
  80. local Ally_Pawn_ID = 17211
  81. local Ally_Castle_ID = 21160
  82. local Ally_Horse_ID = 21664
  83. local Ally_Bishop_ID = 21682
  84. local Ally_Queen_ID = 21683
  85. local Ally_King_ID = 21684
  86.  
  87. local GO_intersection = 1607
  88. local GO_white_field = 92252
  89. local GO_black_field = 92388
  90.  
  91. --------------------------
  92. --      Text Area       --
  93. --------------------------
  94.  
  95. local challenge_timeover = "Herausforderung hatte Zeit \195\188berschritten und wurde automatisch gel\195\182scht."
  96. local challenge_accept_ally = "Challenge. (I want Alliance)"
  97. local challenge_accept_horde = "Challenge. (I will horde)"
  98. local challenge_decline = "Reject Challenge"
  99. local challenge_already_open = "There's still a challenge."
  100. local challenge_cancel = "Cancel the challenge to \195\188!"
  101. local challenge_exchange = "Challenge of %s to %s re-send"
  102. local challenge_against_ki = "Against the computer applies" --there is no ki at this moment
  103. local challenge_send = "Challenge is sent. The game will begins when the challenge from opponent whitin 30 secs will be approved."
  104. local challenge_obtained = "You have been challenge to a chess game by %s . Talk to the chess master to accept it."
  105. local challenge_canceled = "The Challenge was cancelled to \195\188!"
  106. local challenge_declined_by_you = "You have reject the challenge!"
  107. local challenge_declined = "The challenge was rejected by %s !"
  108.  
  109. local menu_save = "Save"
  110. local menu_gamesave = "Gamesave"
  111. local menu_autosave = "Autosave"
  112. local menu_load = "Load"
  113. local menu_saved = "Game saved"
  114. local menu_restart = "Restartting"
  115. local menu_restart_yes = "Yeah!"
  116. local menu_restart_no = "No, not yet!"
  117. local menu_quit = "Quit"
  118. local menu_quit_save = "Save Game and Quit"
  119. local menu_quit_no_save = "Exit without Save"
  120. local menu_start = "Start Chess"
  121. local menu_reset = "Script Reset"
  122. local menu_reset_unable = "Theres is no board to reset."
  123.  
  124. local side_horde = "You are the Horde."
  125. local side_ally = "You are the alliance."
  126.  
  127. local gameover_timer = "%s Seconds to game over."
  128. local lost_player_connection = "Connection to the player has failed. The Chessgame ends.."
  129. local whois = "%s is your turn."
  130.  
  131. local horde_win = "The Horde has won!"
  132. local ally_win = "The Alliance has won!"
  133.  
  134. ------------------------
  135. --User Area ends here!--
  136. ------------------------
  137.  
  138. --  NOTHING CHANGE NOW!!!!!!!!!!!!!!!!!!!
  139.  
  140. RegisterUnitEvent(Manage_NPC, Lua_using, "EventManage")
  141. RegisterUnitGossipEvent(Start_NPC, 1, "EventStart")
  142. RegisterUnitGossipEvent(Start_NPC, 2, "GossipSelect")
  143.  
  144. RegisterUnitEvent(Horde_Pawn_ID, Lua_using, "Horde_Bauer")    --1
  145. RegisterUnitEvent(Horde_Castle_ID, Lua_using, "Horde_Turm")     --2
  146. RegisterUnitEvent(Horde_Horse_ID, Lua_using, "Horde_Pferd")    --3
  147. RegisterUnitEvent(Horde_Bishop_ID, Lua_using, "Horde_Laufer")   --4
  148. RegisterUnitEvent(Horde_Queen_ID, Lua_using, "Horde_Konigin")  --5
  149. RegisterUnitEvent(Horde_King_ID, Lua_using, "Horde_Konig")    --6
  150.  
  151. RegisterUnitEvent(Ally_Pawn_ID, Lua_using, "Allianz_Bauer")  --7
  152. RegisterUnitEvent(Ally_Castle_ID, Lua_using, "Allianz_Turm")   --8
  153. RegisterUnitEvent(Ally_Horse_ID, Lua_using, "Allianz_Pferd")  --9
  154. RegisterUnitEvent(Ally_Bishop_ID, Lua_using, "Allianz_Laufer") --10
  155. RegisterUnitEvent(Ally_Queen_ID, Lua_using, "Allianz_Konigin")--11
  156. RegisterUnitEvent(Ally_King_ID, Lua_using, "Allianz_Konig")  --12
  157.  
  158. RegisterUnitEvent(Horde_King_ID, 4, "Horde_Konig_Dead")
  159. RegisterUnitEvent(Ally_King_ID, 4, "Allianz_Konig_Dead")
  160.  
  161. local Schachspiel = 0
  162. local Schachbrett = {}
  163. local Pos = {}
  164. local Pos2 = {}
  165. local Move = {}
  166. local en_passant = {}
  167. local Player_list = {}
  168. local Players = {}
  169. local game_mode = {0}
  170. local Ply = 0
  171. local Game_over_timer = 30
  172. local KI = false
  173.  
  174. local KI_Chess = {}
  175. local Wertigkeit = {2,10,6,6,20,100,2,10,6,6,20,100}
  176. local KI_Wert = {}
  177.  
  178. print ("############################################")
  179. print ("# ChessEventScript B219uv KI1 ")
  180. print ("# Code by Kenuvis")
  181. print ("# LUAppArc Scripting Project")
  182. print(string.format("# StartNPC %s", Start_NPC))
  183. print ("############################################")
  184.  
  185. function EventManage(Unit)
  186.     Player_List = {}
  187.     game_mode = {0}
  188.     Schachbrett[9][4] = Unit
  189.     Unit:RegisterEvent("auto_reset", auto_reset_timer, 0)
  190.     Unit:RegisterEvent("auto_save", autosave_timer, 0)
  191. end
  192.  
  193. function EventStart(Unit, Event, Player)
  194.     if (Schachspiel == -1) then return end
  195.  
  196.     if (game_mode[1] ~= 0) then
  197.         if (game_mode[4]+30 < os.time()) then
  198.             Player:SendBroadcastMessage(challenge_timeover)
  199.             game_mode = {0}
  200.             Player_list = {}
  201.             EventStart(Unit, Event, Player)
  202.             return
  203.         end
  204.         if (Player == game_mode[2]) then
  205.             Unit:GossipCreateMenu(20013, game_mode[2], 0)
  206.             Unit:GossipMenuAddItem(30, challenge_accept_ally, 2000, 0)
  207.             Unit:GossipMenuAddItem(30, challenge_accept_horde, 2001, 0)
  208.             Unit:GossipMenuAddItem(30, challenge_decline, 2002, 0)
  209.             Unit:GossipSendMenu(game_mode[2])
  210.         else
  211.             Player:SendBroadcastMessage(challenge_already_open)
  212.             Unit:GossipCreateMenu(20013, Player, 0)
  213.             Unit:GossipMenuAddItem(30, challenge_cancel, 1600, 0)
  214.             Unit:GossipMenuAddItem(30, string.format(challenge_exchange, game_mode[3]:GetName(), game_mode[2]:GetName()), 1601, 0)
  215.             Unit:GossipSendMenu(Player)
  216.         end
  217.         return
  218.     end
  219.  
  220.     Unit:GossipCreateMenu(20010, Player, 0)
  221.  
  222.     if (table.getn(read_file("scripts/chess_gamesave.txt")) > 0) or (table.getn(read_file("scripts/chess_autosave.txt")) > 0) then
  223.         Unit:GossipMenuAddItem(19, menu_load , 1, 0)
  224.     end
  225.  
  226.     if (Schachspiel ~= 0) then
  227.         Unit:GossipMenuAddItem(19, menu_save, 2, 1)
  228.         Unit:GossipMenuAddItem(19, menu_restart, 3, 0)
  229.         Unit:GossipMenuAddItem(16, menu_quit, 4, 0)
  230.     else
  231.         Unit:GossipMenuAddItem(17, menu_start, 5, 0)
  232.     end
  233.  
  234.     Unit:GossipMenuAddItem(19, menu_reset, 12, 0)
  235.     Unit:GossipSendMenu(Player)
  236. end
  237.  
  238. function GossipSelect(Unit, Event, Player, MenuId, id, Code)
  239.     if(id == 0) then
  240.         Player_list = {}
  241.         EventStart(Unit,Event, Player)
  242.         return
  243.     else
  244.         Unit:GossipCreateMenu(20011, Player, 0)
  245.         Unit:GossipMenuAddItem(21, "[..]", 0, 0)
  246.     end
  247.  
  248.     if (id == 1) then
  249.         if (table.getn(read_file("scripts/chess_gamesave.txt")) > 0) then
  250.             Unit:GossipMenuAddItem(30, menu_gamesave , 6, 0)
  251.         end
  252.         if (table.getn(read_file("scripts/chess_autosave.txt")) > 0) then
  253.             Unit:GossipMenuAddItem(30, menu_autosave , 7, 0)
  254.         end
  255.         Unit:GossipSendMenu(Player)
  256.     elseif (id == 2) then
  257.         game_save("scripts/chess_gamesave.txt", Code)
  258.         Player:SendBroadcastMessage(menu_saved)
  259.         Unit:GossipComplete(Player)
  260.     elseif (id == 3) then
  261.         Unit:GossipMenuAddItem(30, menu_restart_yes, 8, 0)
  262.         Unit:GossipMenuAddItem(30, menu_restart_no, 0, 0)
  263.         Unit:GossipSendMenu(Player)
  264.     elseif (id == 4) then
  265.         Unit:GossipMenuAddItem(30, menu_quit_save, 10, 1)
  266.         Unit:GossipMenuAddItem(30, menu_quit_no_save, 11, 0)
  267.         Unit:GossipSendMenu(Player)
  268.     elseif (id == 5) then
  269.         if (table.getn(Player_list) > 0) then
  270.             Player:SendBroadcastMessage(challenge_already_open)
  271.             Unit:GossipMenuAddItem(30, challenge_cancel, 1600, 0)
  272.             Unit:GossipSendMenu(Player)
  273.         else
  274.  
  275.             Unit:GossipCreateMenu(20012, Player, 0)
  276.             Unit:GossipMenuAddItem(21, "[..]", 0, 0)
  277.             --Unit:GossipMenuAddItem(30, challenge_against_ki, 1550, 0)
  278.  
  279.             for a = 1,20,1 do
  280.                 target = Player:GetRandomPlayer(0)
  281.                 if not already_see(target, Player_list) then
  282.                     table.insert(Player_list, target)
  283.                     Unit:GossipMenuAddItem(30, target:GetName(), 1000+table.getn(Player_list), 0)
  284.                 end
  285.             end
  286.             table.insert(Player_list, 1, Player)
  287.             Unit:GossipSendMenu(Player)
  288.         end
  289.     elseif (id > 1000) and (id < 1500) then
  290.         Player:SendBroadcastMessage(challenge_send)
  291.  
  292.         Player_list[id-999]:SendBroadcastMessage(string.format(challenge_obtained ,Player:GetName()))
  293.         game_mode[1] = 1
  294.         game_mode[2] = Player_list[id-999]
  295.         game_mode[3] = Player
  296.         game_mode[4] = os.time()
  297.         Unit:GossipComplete(Player)
  298.     elseif (id == 1550) then
  299.         Unit:GossipComplete(Player)
  300.         --Players = {Player, "ki"}
  301.         Players = {Player, Player}
  302.         Players[1]:SendBroadcastMessage(side_horde)
  303.         game_new(Unit, Unit:GetX(), Unit:GetY(), Unit:GetZ())
  304.         Schachspiel = 1
  305.         Unit:MoveTo(Unit:GetX() - 4 * size, Unit:GetY(), Unit:GetZ(), 6.235291)
  306.         Unit:SpawnCreature(Manage_NPC, Unit:GetX() - 4 * size-1.5, Unit:GetY()-1.5, Unit:GetZ(), 6.235291, 35, 0)
  307.         Unit:GossipComplete(Player)
  308.     elseif (id == 1600) then
  309.         Unit:GossipComplete(Player)
  310.         Player_list = {}
  311.  
  312.         Player:SendBroadcastMessage(challenge_canceled)
  313.         game_mode[2]:SendBroadcastMessage(challenge_canceled)
  314.  
  315.         game_mode = {0}
  316.     elseif (id == 1601) then
  317.         Unit:GossipComplete(Player)
  318.         game_mode[4] = os.time()
  319.         game_mode[3]:SendBroadcastMessage(challenge_send)
  320.         game_mode[2]:SendBroadcastMessage(string.format(challenge_obtained ,Player:GetName()))
  321.     elseif (id == 2000) then
  322.         Players = {Player_list[1], Player}
  323.         Players[1]:SendBroadcastMessage(side_horde)
  324.         Players[2]:SendBroadcastMessage(side_ally)
  325.  
  326.         if (game_mode[1] == 1) then
  327.             -- Position des Schachfeld
  328.             Schachbrett[9] = {}
  329.             Schachbrett[9][1] = Unit:GetX() - 3.5 * size
  330.             Schachbrett[9][2] = Unit:GetY() - 3.5 * size
  331.             Schachbrett[9][3] = Unit:GetZ()
  332.  
  333.             game_new(Unit, Unit:GetX(), Unit:GetY(), Unit:GetZ())
  334.  
  335.             Unit:MoveTo(Unit:GetX() - 4 * size, Unit:GetY(), Unit:GetZ(), 6.235291)
  336.             Unit:SpawnCreature(Manage_NPC, Unit:GetX() - 4 * size-1.5, Unit:GetY()-1.5, Unit:GetZ(), 6.235291, 35, 0)
  337.  
  338.         elseif (game_mode[1] == 2) then
  339.  
  340.             if (Schachbrett[9] == nil) then
  341.                 -- Position des Schachfeld
  342.                 Schachbrett[9] = {}
  343.                 Schachbrett[9][1] = Unit:GetX() - 3.5 * size
  344.                 Schachbrett[9][2] = Unit:GetY() - 3.5 * size
  345.                 Schachbrett[9][3] = Unit:GetZ()
  346.                 Schachbrett[9][5] = Unit
  347.                 Unit:MoveTo(Unit:GetX() - 4 * size, Unit:GetY(), Unit:GetZ(), 6.235291)
  348.                 Unit:SpawnCreature(Manage_NPC, Unit:GetX() - 4 * size-1.5, Unit:GetY()-1.5, Unit:GetZ(), 6.235291, 35, 0)
  349.             else
  350.                 clear_field()
  351.             end
  352.             game_load(loc[1], loc[2])
  353.  
  354.         end
  355.  
  356.         Player_list = {}
  357.         game_mode[1] = 0
  358.  
  359.         Unit:GossipComplete(Players[1])
  360.         Unit:GossipComplete(Players[2])
  361.     elseif (id == 2001) then
  362.         Players = {Player, Player_list[1]}
  363.         Players[1]:SendBroadcastMessage(side_horde)
  364.         Players[2]:SendBroadcastMessage(side_ally)
  365.  
  366.         if (game_mode[1] == 1) then
  367.             game_new(Unit, Unit:GetX(), Unit:GetY(), Unit:GetZ())
  368.  
  369.             Unit:MoveTo(Unit:GetX() - 4 * size, Unit:GetY(), Unit:GetZ(), 6.235291)
  370.             Unit:SpawnCreature(Manage_NPC, Unit:GetX() - 4 * size-1.5, Unit:GetY()-1.5, Unit:GetZ(), 6.235291, 35, 0)
  371.  
  372.         elseif (game_mode[1] == 2) then
  373.  
  374.             if (Schachbrett[9] == nil) then
  375.                 -- Position des Schachfeld
  376.                 Schachbrett[9] = {}
  377.                 Schachbrett[9][1] = Unit:GetX() - 3.5 * size
  378.                 Schachbrett[9][2] = Unit:GetY() - 3.5 * size
  379.                 Schachbrett[9][3] = Unit:GetZ()
  380.                 Schachbrett[9][5] = Unit
  381.                 Unit:MoveTo(Unit:GetX() - 4 * size, Unit:GetY(), Unit:GetZ(), 6.235291)
  382.                 Unit:SpawnCreature(Manage_NPC, Unit:GetX() - 4 * size-1.5, Unit:GetY()-1.5, Unit:GetZ(), 6.235291, 35, 0)
  383.             else
  384.                 clear_field()
  385.             end
  386.             game_load(loc[1], loc[2])
  387.  
  388.         end
  389.  
  390.         Player_list = {}
  391.         game_mode[1] = 0
  392.  
  393.         Unit:GossipComplete(Players[1])
  394.         Unit:GossipComplete(Players[2])
  395.     elseif (id == 2002) then
  396.         Player:SendBroadcastMessage(challenge_declined_by_you)
  397.         Player_list[1]:SendBroadcastMessage(string.format(challenge_declined ,Player:GetName()))
  398.         Player_list = {}
  399.         Unit:RemoveEvents()
  400.         Unit:GossipComplete(Player)
  401.         game_mode = {0}
  402.     elseif (id == 6) then
  403.         local _table = read_file("scripts/chess_gamesave.txt")
  404.  
  405.         for a, index in ipairs(_table) do
  406.             temp = os.date("*t", index[2])
  407.             Unit:GossipMenuAddItem(30, index[1].." / "..temp.day.."."..temp.month.." - "..temp.hour..":"..temp.min, 20+a, 0)
  408.         end
  409.         Unit:GossipSendMenu(Player)
  410.     elseif (id == 7) then
  411.         local _table = read_file("scripts/chess_autosave.txt")
  412.  
  413.         for a, index in ipairs(_table) do
  414.             temp = os.date("*t", a)
  415.             Unit:GossipMenuAddItem(30, temp.day.."."..temp.month.." - "..temp.hour..":"..temp.min, 20+a+max_save, 0)
  416.         end
  417.         Unit:GossipSendMenu(Player)
  418.     elseif (id == 8) then
  419.         clear_field()
  420.         game_new(Unit,Unit:GetX() + 4 * size, Unit:GetY(), Unit:GetZ())
  421.         Unit:GossipComplete(Player)
  422.     elseif (id == 10) then
  423.         game_save("scripts/chess_gamesave.txt", Code)
  424.         Player:SendBroadcastMessage(menu_saved)
  425.         Unit:GossipComplete(Player)
  426.         Schachbrett[9][4]:Despawn(1,0)
  427.         Players = {}
  428.         clear_field()
  429.         Unit:MoveTo(Unit:GetX() + 4 * size, Unit:GetY(), Unit:GetZ(), 6.235291)
  430.         Unit:GossipComplete(Player)
  431.     elseif (id == 11) then
  432.         clear_field()
  433.         Schachbrett[9][4]:Despawn(1,0)
  434.         Players = {}
  435.         Unit:MoveTo(Unit:GetX() + 4 * size, Unit:GetY(), Unit:GetZ(), 6.235291)
  436.         Unit:GossipComplete(Player)
  437.     elseif (id == 12) then
  438.         Player_list = {}
  439.         Unit:GossipComplete(Player)
  440.         game_mode = {0}
  441.  
  442.         if (table.getn(Schachbrett) >= 9) then
  443.             auto_reset(Schachbrett[9][4])
  444.         else
  445.             Player:SendBroadcastMessage(menu_reset_unable)
  446.         end
  447.         Unit:GossipComplete(Player)
  448.     end
  449.  
  450.     if (id > 20) and (id < 20+max_save) then
  451.         loc = {"scripts/chess_gamesave.txt", id-20}
  452.     elseif (id > 20+max_save) and (id < 20+2*max_save) then
  453.         loc = {"scripts/chess_autosave.txt", id-20+max_save}
  454.     end
  455.  
  456.     if (id > 20) and (id < 20-2*max_save) then
  457.         if (table.getn(Player_list) > 0) then
  458.             Player:SendBroadcastMessage(challenge_already_open)
  459.             Unit:GossipComplete(Player)
  460.         else
  461.  
  462.             Unit:GossipCreateMenu(20012, Player, 0)
  463.             Unit:GossipMenuAddItem(21, "[..]", 0, 0)
  464.  
  465.             for a = 1,20,1 do
  466.                 target = Unit:GetRandomPlayer(0)
  467.                 if not already_see(target, Player_list) then
  468.                     table.insert(Player_list, target)
  469.                     Unit:GossipMenuAddItem(30, target:GetName(), 3000+table.getn(Player_list), 0)
  470.                 end
  471.             end
  472.             table.insert(Player_list, 1, Player)
  473.             Unit:GossipSendMenu(Player)
  474.         end
  475.     elseif (id > 3000) and (id < 3500) then
  476.         Player:SendBroadcastMessage(challenge_send)
  477.  
  478.         Player_list[id-2999]:SendBroadcastMessage(string.format(challenge_obtained, Player:GetName()))
  479.  
  480.         game_mode[1] = 2
  481.         game_mode[2] = Player_list[id-2999]
  482.         game_mode[3] = Player
  483.         game_mode[4] = os.time()
  484.     end
  485. end
  486.  
  487. function game_new(Unit,x,y,z)
  488.     -- Spieleranfang
  489.     math.randomseed(os.time())
  490.     for i=1,math.random(10,50),1 do
  491.         Schachspiel = math.random(1,2)
  492.     end
  493.  
  494.     for a = 1,8,1 do
  495.         Schachbrett[a] = {}
  496.         for b = 1,8,1 do
  497.             Schachbrett[a][b] = {0,0}
  498.         end
  499.     end
  500.  
  501.     -- Position des Schachfeld
  502.     if (Schachbrett[9] == nil) then Schachbrett[9] = {} end
  503.     Schachbrett[9][1] = x - 3.5 * size
  504.     Schachbrett[9][2] = y - 3.5 * size
  505.     Schachbrett[9][3] = z
  506.     Schachbrett[9][5] = Unit
  507.  
  508.     e1 = 3.5 * size
  509.     e2 = 2.5 * size
  510.     e3 = 1.5 * size
  511.     e4 = 0.5 * size
  512.  
  513.     z = z+3
  514.     -- spawn horde seite
  515.     -- bauern
  516.     Unit:SpawnCreature(Horde_Pawn_ID, x+e1, y+e2, z, 5, 35, 0)
  517.     Unit:SpawnCreature(Horde_Pawn_ID, x+e2, y+e2, z, 5, 35, 0)
  518.     Unit:SpawnCreature(Horde_Pawn_ID, x+e3, y+e2, z, 5, 35, 0)
  519.     Unit:SpawnCreature(Horde_Pawn_ID, x+e4, y+e2, z, 5, 35, 0)
  520.     Unit:SpawnCreature(Horde_Pawn_ID, x-e4, y+e2, z, 5, 35, 0)
  521.     Unit:SpawnCreature(Horde_Pawn_ID, x-e3, y+e2, z, 5, 35, 0)
  522.     Unit:SpawnCreature(Horde_Pawn_ID, x-e2, y+e2, z, 5, 35, 0)
  523.     Unit:SpawnCreature(Horde_Pawn_ID, x-e1, y+e2, z, 5, 35, 0)
  524.     -- t?rme
  525.     Unit:SpawnCreature(Horde_Castle_ID, x+e1, y+e1, z, 5, 35, 0)
  526.     Unit:SpawnCreature(Horde_Castle_ID, x-e1, y+e1, z, 5, 35, 0)
  527.     -- pferd
  528.     Unit:SpawnCreature(Horde_Horse_ID, x+e2, y+e1, z, 5, 35, 0)
  529.     Unit:SpawnCreature(Horde_Horse_ID, x-e2, y+e1, z, 5, 35, 0)
  530.     -- l?ufer
  531.     Unit:SpawnCreature(Horde_Bishop_ID, x+e3, y+e1, z, 5, 35, 0)
  532.     Unit:SpawnCreature(Horde_Bishop_ID, x-e3, y+e1, z, 5, 35, 0)
  533.     -- k?nigin
  534.     Unit:SpawnCreature(Horde_Queen_ID, x+e4, y+e1, z, 5, 35, 0)
  535.     -- k?nig
  536.     Unit:SpawnCreature(Horde_King_ID, x-e4, y+e1, z, 5, 35, 0)
  537.  
  538.     -- spawn allianz seite
  539.     -- bauern
  540.     Unit:SpawnCreature(Ally_Pawn_ID, x+e1, y-e2, z, 1.585233, 35, 0)
  541.     Unit:SpawnCreature(Ally_Pawn_ID, x+e2, y-e2, z, 1.585233, 35, 0)
  542.     Unit:SpawnCreature(Ally_Pawn_ID, x+e3, y-e2, z, 1.585233, 35, 0)
  543.     Unit:SpawnCreature(Ally_Pawn_ID, x+e4, y-e2, z, 1.585233, 35, 0)
  544.     Unit:SpawnCreature(Ally_Pawn_ID, x-e4, y-e2, z, 1.585233, 35, 0)
  545.     Unit:SpawnCreature(Ally_Pawn_ID, x-e3, y-e2, z, 1.585233, 35, 0)
  546.     Unit:SpawnCreature(Ally_Pawn_ID, x-e2, y-e2, z, 1.585233, 35, 0)
  547.     Unit:SpawnCreature(Ally_Pawn_ID, x-e1, y-e2, z, 1.562182, 35, 0)
  548.     -- t?rme
  549.     Unit:SpawnCreature(Ally_Castle_ID, x+e1, y-e1, z, 1.562182, 35, 0)
  550.     Unit:SpawnCreature(Ally_Castle_ID, x-e1, y-e1, z, 1.562182, 35, 0)
  551.     -- pferd
  552.     Unit:SpawnCreature(Ally_Horse_ID, x+e2, y-e1, z, 1.562182, 35, 0)
  553.     Unit:SpawnCreature(Ally_Horse_ID, x-e2, y-e1, z, 1.585233, 35, 0)
  554.     -- l?ufer
  555.     Unit:SpawnCreature(Ally_Bishop_ID, x+e3, y-e1, z, 1.585233, 35, 0)
  556.     Unit:SpawnCreature(Ally_Bishop_ID, x-e3, y-e1, z, 1.585233, 35, 0)
  557.     -- k?nigin
  558.     Unit:SpawnCreature(Ally_Queen_ID, x+e4, y-e1, z, 1.585233, 35, 0)
  559.     -- k?nig
  560.     Unit:SpawnCreature(Ally_King_ID, x-e4, y-e1, z, 1.585233, 35, 0)
  561. end
  562.  
  563. function game_over(Unit)
  564.     if Game_over_timer == 30 then
  565.         Unit:SendChatMessage(12, 0, string.format(gameover_timer, Game_over_timer))
  566.     elseif Game_over_timer == 20 then
  567.         Unit:SendChatMessage(12, 0, string.format(gameover_timer, Game_over_timer))
  568.     elseif Game_over_timer == 15 then
  569.         Unit:SendChatMessage(12, 0, string.format(gameover_timer, Game_over_timer))
  570.     elseif Game_over_timer == 10 then
  571.         Unit:SendChatMessage(12, 0, string.format(gameover_timer, Game_over_timer))
  572.     elseif Game_over_timer < 10 then
  573.         Unit:SendChatMessage(12, 0, string.format(gameover_timer, Game_over_timer))
  574.     end
  575.  
  576.     Game_over_timer = Game_over_timer -1
  577.  
  578.     if (Game_over_timer == 0) then
  579.         clear_field()
  580.         if (GO_ID ~= 0) then
  581.             Schachbrett[9][4]:SpawnGameObject(GO_ID, Schachbrett[9][4]:GetX(), Schachbrett[9][4]:GetY(), Schachbrett[9][4]:GetZ(), 0, 300000)
  582.         end
  583.         Schachbrett[9][4]:Despawn(1,0)
  584.         Schachbrett[9][5]:MoveTo(-5201.031738, -695.866943, 86.111404, 4.187754)
  585.         Schachspiel = 0
  586.         Player_List = {}
  587.         game_mode = {0}
  588.     end
  589. end
  590.  
  591. --------------------------------------------------
  592. ------       private functions Anfang       ------
  593. --------------------------------------------------
  594.  
  595. function comparing(_x1, _y1, _x2, _y2)
  596.     if (_x1 == _x2) and (_y1 == _y2) then
  597.         return true
  598.     else
  599.         return false
  600.     end
  601. end
  602.  
  603. function xyi_Output(_Unit)
  604.     if (_Unit == nil) or (type(_Unit) ~= "userdata") then
  605.         return 0,0,0
  606.     end
  607.  
  608.     x = 0
  609.     repeat
  610.         x = x+1
  611.  
  612.         if (x > 8) then
  613.             x = 0
  614.             break
  615.         end
  616.     until (_Unit:GetX() < (Schachbrett[9][1]-size) + (x*size)+(size/2)) and (_Unit:GetX() > (Schachbrett[9][1]-size) + (x*size)-(size/2))
  617.  
  618.     y = 0
  619.     repeat
  620.         y = y+1
  621.  
  622.         if (y > 8) then
  623.             y = 0
  624.             break
  625.         end
  626.     until(_Unit:GetY() > (Schachbrett[9][2]-size) + (y*size)-(size/2)) and (_Unit:GetY() < (Schachbrett[9][2]-size) + (y*size)+(size/2))
  627.  
  628.     i = 0
  629.     if (x ~= 0) and (y ~= 0) then
  630.         if (table.getn(Schachbrett[x][y]) > 1) then
  631.             if (Schachbrett[x][y][2] < 7) and (Schachbrett[x][y][2] > 0) then
  632.                 i = 1
  633.             elseif (Schachbrett[x][y][2] > 6) and (Schachbrett[x][y][2] < 13) then
  634.                 i = 2
  635.             end
  636.         end
  637.     end
  638.  
  639.     return x, y, i
  640. end
  641.  
  642. function Check_Feld_AR(_x, _y, _array)
  643.     if (_x < 1) or (_y < 1) or (_x > 8) or (_y > 8) then
  644.         return 3
  645.     end
  646.  
  647.     if (_array[_x][_y] == 0) then
  648.         return 0
  649.     end
  650.  
  651.     if (_array[_x][_y] < 7) then
  652.         return 1
  653.     end
  654.  
  655.     if (_array[_x][_y] > 6) then
  656.         return 2
  657.     end
  658.  
  659.     return 0
  660. end
  661.  
  662. function Check_Feld(_x, _y)
  663.     if (_x < 1) or (_y < 1) or (_x > 8) or (_y > 8) then
  664.         return 3
  665.     end
  666.  
  667.     if (Schachbrett[_x][_y][2] == 0) then
  668.         return 0
  669.     end
  670.  
  671.     if (Schachbrett[_x][_y][2] < 7) then
  672.         return 1
  673.     end
  674.  
  675.     if (Schachbrett[_x][_y][2] > 6) then
  676.         return 2
  677.     end
  678.  
  679.     return 0
  680. end
  681.  
  682. function display_board(_Unit, _time)
  683.     for a=-(size/2),(7.5*size),size do
  684.         for b=-(size/2),(7.5*size),size do
  685.             _Unit:SpawnGameObject(GO_intersection, Schachbrett[9][1]+a, Schachbrett[9][2]+b, Schachbrett[9][3], 4.71, _time)
  686.         end
  687.     end
  688. end
  689.  
  690. function Select(_Unit)
  691.     _Unit:CastSpell(32261)
  692.         display_board(_Unit, 3000)
  693. end
  694.  
  695. function game_save(dir, str)
  696.     if (io.open(dir,"r") == nil) then
  697.         io.open(dir,"w")
  698.     end
  699.  
  700.     local f = assert(io.open(dir,"r"))
  701.  
  702.     temp = ""
  703.     for a = 1,8,1 do
  704.         for b = 1,8,1 do
  705.             if (type(Schachbrett[a][b][1]) == "userdata") then
  706.                 temp = string.format("%s%s%s%s ", temp, a, b, Schachbrett[a][b][2])
  707.             end
  708.         end
  709.     end
  710.  
  711.     if (str == nil) then str = "" end
  712.  
  713.     save = string.format("%s %s %s %s", str, os.time(), Schachspiel, temp)
  714.  
  715.     save_table = {}
  716.     temp = f:read("*l")
  717.  
  718.     while temp ~= nil do
  719.         table.insert(save_table, temp)
  720.         temp = f:read("*l")
  721.     end
  722.  
  723.     table.insert(save_table, save)
  724.  
  725.     while (table.getn(save_table) > max_save) do
  726.         save_table[max_save+1] = nil
  727.     end
  728.  
  729.     f:close()
  730.     local f = assert(io.open(dir,"w+"))
  731.  
  732.     str = ""
  733.     for i, l in pairs(save_table) do
  734.         str = string.format("%s%s%s", str,l,"\n")
  735.     end
  736.     f:write(str)
  737.     f:close()
  738. end
  739.  
  740. function game_load(dir, ID)
  741.     for a = 1,8,1 do
  742.         Schachbrett[a] = {}
  743.         for b = 1,8,1 do
  744.             Schachbrett[a][b] = {0,0}
  745.         end
  746.     end
  747.  
  748.     local _table = read_file(dir)
  749.     Schachspiel = _table[ID][3]
  750.     Schachspiel = math.abs(Schachspiel)
  751.  
  752.     for i=4,table.getn(_table[ID])-1,1 do
  753.         a = string.sub(_table[ID][i], 1, 1)
  754.         b = string.sub(_table[ID][i], 2, 2)
  755.         c = string.sub(_table[ID][i], 3)
  756.  
  757.         a = Schachbrett[9][1]+(a-1)*size
  758.         b = Schachbrett[9][2]+(b-1)*size
  759.         c = math.abs(c)
  760.  
  761.         if (c == 1) then
  762.             Schachbrett[9][4]:SpawnCreature(Horde_Pawn_ID, a, b, Schachbrett[9][3], 5, 35, 0)
  763.         elseif (c == 2) then
  764.             Schachbrett[9][4]:SpawnCreature(Horde_Castle_ID, a, b, Schachbrett[9][3], 5, 35, 0)
  765.         elseif (c == 3) then
  766.             Schachbrett[9][4]:SpawnCreature(Horde_Horse_ID, a, b, Schachbrett[9][3], 5, 35, 0)
  767.         elseif (c == 4) then
  768.             Schachbrett[9][4]:SpawnCreature(Horde_Bishop_ID, a, b, Schachbrett[9][3], 5, 35, 0)
  769.         elseif (c == 5) then
  770.             Schachbrett[9][4]:SpawnCreature(Horde_Queen_ID, a, b, Schachbrett[9][3], 5, 35, 0)
  771.         elseif (c == 6) then
  772.             Schachbrett[9][4]:SpawnCreature(Horde_King_ID, a, b, Schachbrett[9][3], 5, 35, 0)
  773.         elseif (c == 7) then
  774.             Schachbrett[9][4]:SpawnCreature(Ally_Pawn_ID, a, b, Schachbrett[9][3], 1.585233, 35, 0)
  775.         elseif (c == 8) then
  776.             Schachbrett[9][4]:SpawnCreature(Ally_Castle_ID, a, b, Schachbrett[9][3], 1.585233, 35, 0)
  777.         elseif (c == 9) then
  778.             Schachbrett[9][4]:SpawnCreature(Ally_Horse_ID, a, b, Schachbrett[9][3], 1.585233, 35, 0)
  779.         elseif (c == 10) then
  780.             Schachbrett[9][4]:SpawnCreature(Ally_Bishop_ID, a, b, Schachbrett[9][3], 1.585233, 35, 0)
  781.         elseif (c == 11) then
  782.             Schachbrett[9][4]:SpawnCreature(Ally_Queen_ID, a, b, Schachbrett[9][3], 1.585233, 35, 0)
  783.         elseif (c == 12) then
  784.             Schachbrett[9][4]:SpawnCreature(Ally_King_ID, a, b, Schachbrett[9][3], 1.585233, 35, 0)
  785.         end
  786.  
  787.     end
  788.  
  789.     who_is(Schachbrett[9][4])
  790. end
  791.  
  792. function read_file(dir)
  793.     if (io.open(dir,"r") == nil) then
  794.         return {}
  795.     end
  796.  
  797.     local f = io.open(dir, "r")
  798.     local count = 1
  799.     local return_value = {}
  800.  
  801.     t = f:read("*line")
  802.     while t ~= nil do
  803.         local a = 1
  804.         return_value[count] = {}
  805.  
  806.         i =0
  807.         repeat
  808.             i = i+1
  809.  
  810.             if (string.sub(t, i, i) == " ") then
  811.                 temp = string.sub(t, 1, i-1)
  812.                 t = string.sub(t, i+1)
  813.                 i = 1
  814.                 return_value[count][a] = temp
  815.                 a = a +1
  816.             end
  817.  
  818.         until (string.len(t) < i)
  819.  
  820.         return_value[count][a] = string.sub(t, 1, string.len(t)-1)
  821.  
  822.         count = count + 1
  823.         t = f:read("*line")
  824.     end
  825.  
  826.     return return_value
  827. end
  828.  
  829. function clear_field()
  830.     Schachspiel = 0
  831.  
  832.     for a=1,8,1 do
  833.         for b=1,8,1 do
  834.             if (type(Schachbrett[a][b][1]) == "userdata") then
  835.                 Schachbrett[a][b][1]:Despawn(1,0)
  836.             end
  837.             Schachbrett[a][b] = {0,0}
  838.         end
  839.     end
  840. end
  841.  
  842. function already_see(_target, _list)
  843.     if _target == nil then
  844.         return true
  845.     end
  846.  
  847.     for i,k in pairs(_list) do
  848.         if k == _target then
  849.             return true
  850.         end
  851.     end
  852.  
  853.     return false
  854. end
  855.  
  856. function Pcheck(_Player)
  857.     if _Player:GetName() ~= "Unknown" then
  858.         return true
  859.     else
  860.         Pos = {}
  861.         Pos2 = {}
  862.         Move = {}
  863.         en_passant = {}
  864.         Player_list = {}
  865.         Players = {}
  866.         game_mode = {0}
  867.         Ply = 0
  868.         clear_field()
  869.         Schachbrett[9][4]:SendChatMessage(14, 0, lost_player_connection)
  870.         print(lost_player_connection)
  871.         Schachbrett[9][4]:Despawn(1,0)
  872.         Unit = Schachbrett[9][5]
  873.         Unit:MoveTo(Unit:GetX() + 4 * size, Unit:GetY(), Unit:GetZ(), 6.235291)
  874.         return false
  875.     end
  876. end
  877.  
  878. function printout(_Text)
  879.     if debug then
  880.         print (_Text)
  881.     end
  882. end
  883.  
  884. --------------------------------------------------
  885. ------      private functions Ende          ------
  886. --------------------------------------------------
  887.  
  888. --------------------------------------------------
  889. ------              Steuerung               ------
  890. --------------------------------------------------
  891.  
  892. function Check(Unit)
  893.     if (Schachspiel > 0) then
  894.  
  895.         Ply = Players[Schachspiel]
  896.         if not Pcheck(Ply) then return end
  897.         local x,y = xyi_Output(Unit)
  898.  
  899.         if ((Schachbrett[x][y][2] < 7) and (Schachspiel == 1)) or ((Schachbrett[x][y][2] > 6) and (Schachspiel == 2)) then
  900.             if (Ply ~= nil) then
  901.                 xp,yp = xyi_Output(Ply)
  902.                 x,y = xyi_Output(Unit)
  903.                 if comparing(xp, yp, x, y) then
  904.                     Schachbrett[x][y][1]:RemoveEvents()
  905.                     Schachbrett[x][y][1]:RegisterEvent("Timer", Select_Timer, 1)
  906.                 end
  907.             end
  908.         end
  909.     end
  910. end
  911.  
  912. function Timer(Unit)
  913.     if not Pcheck(Ply) then return end
  914.     local x,y  = xyi_Output(Unit)
  915.     local xp,yp = xyi_Output(Ply)
  916.  
  917.     if comparing(xp, yp, x, y) then
  918.         Schachspiel = 0
  919.         Select(Unit)
  920.         zahler = 0
  921.  
  922.         Unit:RemoveEvents()
  923.         Unit:RegisterEvent(Schachbrett[x][y][3][1], 1000, Schachbrett[x][y][3][2])
  924.  
  925.         a = Schachbrett[x][y][3][2]/5
  926.         if (math.floor(a+0.5) > 1) then
  927.            Unit:RegisterEvent("Select", 2000, math.floor(a+0.5))
  928.         end
  929.  
  930.     else
  931.         Unit:RegisterEvent("Check", 500, 0)
  932.     end
  933. end
  934.  
  935. function GoTo_Timer(Unit)
  936.     if KI then
  937.         xp = Pos[1]
  938.         yp = Pos[2]
  939.     else
  940.         if not Pcheck(Ply) then return end
  941.         xp = Ply:GetX()
  942.         yp = Ply:GetY()
  943.     end
  944.  
  945.     if (xp == Pos[1]) and (yp == Pos[2]) then
  946.         local x, y = xyi_Output(Unit)
  947.         printout(Unit:GetName())
  948.         printout(x,y,xp,yp,Pos[1],Pos[2])
  949.  
  950.         Schachbrett[9][4]:RemoveEvents()
  951.         Schachbrett[9][4]:RegisterEvent("auto_reset", auto_reset_timer, 0)
  952.  
  953.         Unit:RemoveEvents()
  954.  
  955.         Unit:RegisterEvent("GoTo_Emote_Timer", 1000, 0)
  956.         select_emote = Schachbrett[x][y][5]
  957.  
  958.         if (type(select_emote) == "table") then
  959.             select_emote = select_emote[math.random(table.getn(select_emote))]
  960.         end
  961.  
  962.         move = {Unit:GetX()+(Pos[3]*size), Unit:GetY()+(Pos[4]*size), 0, 0, x, y, Ply:GetZ()}
  963.  
  964.         Unit:Emote(select_emote,900)
  965.     end
  966. end
  967.  
  968. function GoTo_Emote_Timer(Unit)
  969.     local x, y, f = xyi_Output(Unit)
  970.  
  971.     Unit:RemoveEvents()
  972.     enemy = -f+3
  973.  
  974.     if (Check_Feld(x+Pos[3], y+Pos[4]) == enemy) then
  975.         Schachbrett[x+Pos[3]][y+Pos[4]][1]:RemoveEvents()
  976.  
  977.         if (Schachbrett[x][y][6] == 1) then
  978.             if Pos[3] < 0 then
  979.                 move[3] = move[1]+1.5
  980.             else
  981.                 move[3] = move[1]-1.5
  982.             end
  983.  
  984.             if Pos[4] < 0 then
  985.                 move[4] = move[2]+1.5
  986.             else
  987.                 move[4] = move[2]-1.5
  988.             end
  989.  
  990.             Unit:MoveTo(move[3], move[4], move[7], Unit:GetO())
  991.             Unit:RegisterEvent("GoTo_Emote_Fight_Timer", 500, 0)
  992.         elseif (Schachbrett[x][y][6] == 2) then
  993.             Unit:CastSpellOnTarget(Schachbrett[x][y][7], Schachbrett[x+Pos[3]][y+Pos[4]][1])
  994.  
  995.             if Schachbrett[x+Pos[3]][y+Pos[4]][9] then
  996.                 Schachbrett[x+Pos[3]][y+Pos[4]][1]:CastSpell(Schachbrett[x+Pos[3]][y+Pos[4]][10])
  997.             end
  998.  
  999.             Unit:Kill(Schachbrett[x+Pos[3]][y+Pos[4]][1])
  1000.             Unit:RegisterEvent("GoTo_Move_Timer", 1000, 0)
  1001.         elseif (Schachbrett[x][y][6] == 3) then
  1002.             Unit:CastSpell(Schachbrett[x][y][7])
  1003.             Schachbrett[x+Pos[3]][y+Pos[4]][1]:CastSpell(Schachbrett[x][y][8])
  1004.  
  1005.             Unit:Kill(Schachbrett[x+Pos[3]][y+Pos[4]][1])
  1006.             Unit:RegisterEvent("GoTo_Move_Timer", 1000, 0)
  1007.         end
  1008.     else
  1009.         Unit:RegisterEvent("GoTo_Move_Timer", 1000, 0)
  1010.     end
  1011. end
  1012.  
  1013. function GoTo_Emote_Fight_Timer(Unit)
  1014.     if (Unit:GetX() == move[3]) and (Unit:GetY() == move[4]) then
  1015.         Unit:RemoveEvents()
  1016.         Unit:RegisterEvent("GoTo_Emote_Second_Fight_Timer", 1000, 0)
  1017.         if Schachbrett[x+Pos[3]][y+Pos[4]][9] then
  1018.             Schachbrett[x+Pos[3]][y+Pos[4]][1]:CastSpellOnTarget(Schachbrett[x+Pos[3]][y+Pos[4]][10], Schachbrett[x+Pos[3]][y+Pos[4]][1])
  1019.         end
  1020.  
  1021.         attack_emote = Schachbrett[x][y][7]
  1022.         if (type(attack_emote) == "table") then
  1023.             attack_emote = attack_emote[math.random(table.getn(attack_emote))]
  1024.         end
  1025.         Unit:Emote(attack_emote,1)
  1026.     end
  1027. end
  1028.  
  1029. function GoTo_Emote_Second_Fight_Timer(Unit)
  1030.     Unit:RemoveEvents()
  1031.     Unit:RegisterEvent("GoTo_Move_Timer", 1000, 0)
  1032.     if (Lua_using == 18) then
  1033.         Unit:Kill(Schachbrett[x+Pos[3]][y+Pos[4]][1])
  1034.     else
  1035.         Schachbrett[x+Pos[3]][y+Pos[4]][1]:Despawn(1,0)
  1036.         if (Schachbrett[x+Pos[3]][y+Pos[4]][2] == 6) then
  1037.             Horde_Konig_Dead(0)
  1038.         elseif (Schachbrett[x+Pos[3]][y+Pos[4]][2] == 12) then
  1039.             Allianz_Konig_Dead(0)
  1040.         end
  1041.     end
  1042.  
  1043.     attack_emote = Schachbrett[x][y][7]
  1044.     if (type(attack_emote) == "table") then
  1045.         attack_emote = attack_emote[math.random(table.getn(attack_emote))]
  1046.     end
  1047.     Unit:Emote(attack_emote, 1)
  1048. end
  1049.  
  1050. function GoTo_Move_Timer(Unit)
  1051.     Unit:RemoveEvents()
  1052.     Unit:Emote(0,1)
  1053.     Unit:MoveTo(move[1], move[2], move[7], Unit:GetO())
  1054.  
  1055.     Schachbrett[move[5]][move[6]][4] = false
  1056.     Schachbrett[move[5]+Pos[3]][move[6]+Pos[4]] = Schachbrett[move[5]][move[6]]
  1057.     Schachbrett[move[5]][move[6]] = {0,0}
  1058.  
  1059.     move = {}
  1060.  
  1061.     if (Pos[5] == "en passant input") then
  1062.         en_passant = {Pos[6], Pos[7]}
  1063.     elseif (Pos[5] == "en passant kickout") then
  1064.         Schachbrett[en_passant[1]][en_passant[2]-Pos[4]][1]:RemoveEvents()
  1065.         if (Lua_using == 18) then
  1066.             Unit:Kill(Schachbrett[en_passant[1]][en_passant[2]-Pos[4]][1])
  1067.         else
  1068.             Schachbrett[en_passant[1]][en_passant[2]-Pos[4]][1]:Despawn(1,0)
  1069.         end
  1070.         Schachbrett[en_passant[1]][en_passant[2]-Pos[4]] = {0,0}
  1071.         en_passant = {}
  1072.     else
  1073.         en_passant = {}
  1074.     end
  1075.  
  1076.     if (Schachspiel ~= -1) then
  1077.         Schachspiel = enemy
  1078.         who_is(Schachbrett[9][4])
  1079.     end
  1080.  
  1081.     Pos = {}
  1082.  
  1083.     if (Players[Schachspiel] == "ki") then
  1084.         Schachspiel = Schachspiel * -1
  1085.         Schachbrett[9][4]:RegisterEvent("KI_Start", 500, 1)
  1086.     end
  1087.     Unit:RegisterEvent("Check", 500, 0)
  1088. end
  1089.  
  1090. function RegUnit(_Unit, _table)
  1091.     if (Schachspiel > 0) then
  1092.         _Unit:RegisterEvent("Check", 500, 0)
  1093.         _Unit:CastSpell(32261)
  1094.  
  1095.         x, y = xyi_Output(_Unit)
  1096.         Schachbrett[x][y] = _table
  1097.  
  1098.         --_Unit:RegisterEvent("who_is", 500, 1)
  1099.     end
  1100. end
  1101.  
  1102. function who_is(_Unit)
  1103.     if Players[2] == "ki" then
  1104.         return
  1105.     end
  1106.  
  1107.     Players[2]:SendBroadcastMessage(string.format(whois, Players[Schachspiel]:GetName()))
  1108.     Players[1]:SendBroadcastMessage(string.format(whois, Players[Schachspiel]:GetName()))
  1109. end
  1110.  
  1111. function auto_reset(Unit)
  1112.     if reset and (Schachspiel ~= 0) then
  1113.  
  1114.         for a=1,8,1 do
  1115.             for b=1,8,1 do
  1116.                 temp = Schachbrett[a][b]
  1117.                 Schachbrett[a][b] = {0,0}
  1118.  
  1119.                 if (type(temp[1]) == "userdata") then
  1120.                     Schachbrett[a][b] = temp
  1121.                     Schachbrett[a][b][1]:RemoveEvents()
  1122.                     Schachbrett[a][b][1]:RegisterEvent("Check", 500, 0)
  1123.                 end
  1124.             end
  1125.         end
  1126.     end
  1127. end
  1128.  
  1129. function auto_save(Unit)
  1130.     if autosave then
  1131.         game_save("scripts/chess_autosave.txt", os.time())
  1132.     end
  1133. end
  1134.  
  1135. --------------------------------------------------
  1136. ------          Movements @ testing         ------
  1137. --------------------------------------------------
  1138.  
  1139. function Queen_GoTo(Queen)
  1140.     if not Pcheck(Ply) then return end
  1141.     local x,y,f = xyi_Output(Queen)
  1142.     local xp,yp = xyi_Output(Ply)
  1143.  
  1144.     zahler = zahler+1
  1145.  
  1146.     if comparing(xp, yp, x, y) then
  1147.         Check(Queen)
  1148.     else
  1149.         if (xp < x) then
  1150.             xort = -1
  1151.         elseif (xp > x) then
  1152.             xort = 1
  1153.         else
  1154.             xort = 0
  1155.         end
  1156.  
  1157.         if (yp < y) then
  1158.             yort = -1
  1159.         elseif (yp > y) then
  1160.             yort = 1
  1161.         else
  1162.             yort = 0
  1163.         end
  1164.  
  1165.         for i=1,7,1 do
  1166.             if (Check_Feld(x+(i*xort),y+(i*yort)) ~= f) and (comparing(x+(i*xort),y+(i*yort), xp, yp)) then
  1167.                 Pos[1] = Ply:GetX()
  1168.                 Pos[2] = Ply:GetY()
  1169.                 Pos[3] = i*xort
  1170.                 Pos[4] = i*yort
  1171.                 Queen:RegisterEvent("GoTo_Timer", 900, 1)
  1172.                 break
  1173.             elseif (Check_Feld(x+(i*xort),y+(i*yort)) ~= 0) then
  1174.                 break
  1175.             end
  1176.         end
  1177.     end
  1178.  
  1179.     if (Schachspiel == 0) and (zahler == Schachbrett[x][y][3][2]) then
  1180.         Schachspiel = f
  1181.         Queen:RemoveEvents()
  1182.         Queen:RegisterEvent("Check", 500, 0)
  1183.     end
  1184. end
  1185.  
  1186. function Pawn_GoTo(Pawn)
  1187.     if not Pcheck(Ply) then return end
  1188.     local x, y, f = xyi_Output(Pawn)
  1189.     local xp,yp = xyi_Output(Ply)
  1190.     zahler = zahler+1
  1191.  
  1192.     i = f-(2*(1/f))
  1193.  
  1194.     if comparing(xp, yp, x, y) then
  1195.         Check(Pawn)
  1196.     elseif comparing(xp, yp, x-1, y+i) and (Check_Feld(x-1, y+i) == 2*(1/f)) then
  1197.         Pos[1] = Ply:GetX()
  1198.         Pos[2] = Ply:GetY()
  1199.         Pos[3] = -1
  1200.         Pos[4] = i
  1201.         Pawn:RegisterEvent("GoTo_Timer", 900, 1)
  1202.     elseif comparing(xp, yp, x+1, y+i) and (Check_Feld(x+1, y+i) == 2*(1/f)) then
  1203.         Pos[1] = Ply:GetX()
  1204.         Pos[2] = Ply:GetY()
  1205.         Pos[3] = 1
  1206.         Pos[4] = i
  1207.         Pawn:RegisterEvent("GoTo_Timer", 900, 1)
  1208.     elseif comparing(xp, yp, x, y+i) and (Check_Feld(x, y+i) == 0) then
  1209.         Pos[1] = Ply:GetX()
  1210.         Pos[2] = Ply:GetY()
  1211.         Pos[3] = 0
  1212.         Pos[4] = i
  1213.         Pawn:RegisterEvent("GoTo_Timer", 900, 1)
  1214.     elseif (Schachbrett[x][y][4]) and comparing(xp, yp, x, y+2*i) and (Check_Feld(x, y+2*i) == 0) then
  1215.         Pos[1] = Ply:GetX()
  1216.         Pos[2] = Ply:GetY()
  1217.         Pos[3] = 0
  1218.         Pos[4] = 2*i
  1219.         Pos[5] = "en passant input"
  1220.         Pos[6] = x
  1221.         Pos[7] = y+i
  1222.         Pawn:RegisterEvent("GoTo_Timer", 900, 1)
  1223.     elseif (en_passant ~= {}) then
  1224.         if comparing(xp, yp, en_passant[1], en_passant[2]) and comparing(xp, yp, x-1, y+i) then
  1225.             Pos[1] = Ply:GetX()
  1226.             Pos[2] = Ply:GetY()
  1227.             Pos[3] = -1
  1228.             Pos[4] = i
  1229.             Pos[5] = "en passant kickout"
  1230.             Pawn:RegisterEvent("GoTo_Timer", 900, 1)
  1231.         elseif comparing(xp, yp, en_passant[1], en_passant[2]) and comparing(xp, yp, x+1, y+i) then
  1232.             Pos[1] = Ply:GetX()
  1233.             Pos[2] = Ply:GetY()
  1234.             Pos[3] = 1
  1235.             Pos[4] = i
  1236.             Pos[5] = "en passant kickout"
  1237.             Pawn:RegisterEvent("GoTo_Timer", 900, 1)
  1238.         end
  1239.     end
  1240.  
  1241.  
  1242.     if (Schachspiel == 0) and (zahler == Schachbrett[x][y][3][2]) then
  1243.         Schachspiel = f
  1244.         Pawn:RemoveEvents()
  1245.         Pawn:RegisterEvent("Check", 500, 0)
  1246.     end
  1247. end
  1248.  
  1249. function Castle_GoTo(Castle)
  1250.     if not Pcheck(Ply) then return end
  1251.     local x,y,f = xyi_Output(Castle)
  1252.     local xp,yp = xyi_Output(Ply)
  1253.  
  1254.     local xort = 0
  1255.     local yort = 0
  1256.     zahler = zahler+1
  1257.  
  1258.     if comparing(xp, yp, x, y) then
  1259.         Check(Castle)
  1260.     else
  1261.         if (x == xp) then
  1262.             if (yp < y) then
  1263.                 yort = -1
  1264.             else
  1265.                 yort = 1
  1266.             end
  1267.         elseif (yp == y) then
  1268.             if (xp < x) then
  1269.                 xort = -1
  1270.             else
  1271.                 xort = 1
  1272.             end
  1273.         end
  1274.  
  1275.         for i=1,7,1 do
  1276.             if (Check_Feld(x+(i*xort),y+(i*yort)) ~= f) and (comparing(x+(i*xort),y+(i*yort), xp, yp)) then
  1277.                 Pos[1] = Ply:GetX()
  1278.                 Pos[2] = Ply:GetY()
  1279.                 Pos[3] = i*xort
  1280.                 Pos[4] = i*yort
  1281.                 Castle:RegisterEvent("GoTo_Timer", 900, 1)
  1282.                 break
  1283.             elseif (Check_Feld(x+(i*xort),y+(i*yort)) ~= 0) then
  1284.                 break
  1285.             end
  1286.         end
  1287.     end
  1288.  
  1289.     if (Schachspiel == 0) and (zahler == Schachbrett[x][y][3][2]) then
  1290.         Schachspiel = f
  1291.         Castle:RemoveEvents()
  1292.         Castle:RegisterEvent("Check", 500, 0)
  1293.     end
  1294. end
  1295.  
  1296. function Horse_GoTo(Horse)
  1297.     if not Pcheck(Ply) then return end
  1298.     local x,y,f = xyi_Output(Horse)
  1299.     local xp,yp = xyi_Output(Ply)
  1300.     zahler = zahler+1
  1301.  
  1302.     if comparing(xp, yp, x, y) then
  1303.         Check(Horse)
  1304.     else
  1305.         if (Check_Feld(xp, yp) ~= f) and (math.abs(xp-x)+math.abs(yp-y) == 3) and ((math.abs(xp-x) == 1) or (math.abs(xp-x) == 2)) then
  1306.             Pos[1] = Ply:GetX()
  1307.             Pos[2] = Ply:GetY()
  1308.             Pos[3] = xp-x
  1309.             Pos[4] = yp-y
  1310.             Horse:RegisterEvent("GoTo_Timer", 900, 1)
  1311.         end
  1312.     end
  1313.  
  1314.     if (Schachspiel == 0) and (zahler == Schachbrett[x][y][3][2]) then
  1315.         Schachspiel = f
  1316.         Horse:RemoveEvents()
  1317.         Horse:RegisterEvent("Check", 500, 0)
  1318.     end
  1319. end
  1320.  
  1321. function Bishop_GoTo(Bishop)
  1322.     if not Pcheck(Ply) then return end
  1323.     local x,y,f = xyi_Output(Bishop)
  1324.     local xp,yp = xyi_Output(Ply)
  1325.     zahler = zahler+1
  1326.  
  1327.     if comparing(xp, yp, x, y) then
  1328.         Check(Bishop)
  1329.     else
  1330.         if (xp < x) then
  1331.             xort = -1
  1332.         else
  1333.             xort = 1
  1334.         end
  1335.  
  1336.         if (yp < y) then
  1337.             yort = -1
  1338.         else
  1339.             yort = 1
  1340.         end
  1341.  
  1342.         for i=1,7,1 do
  1343.             if (Check_Feld(x+(i*xort),y+(i*yort)) ~= f) and (comparing(x+(i*xort),y+(i*yort), xp, yp)) then
  1344.                 Pos[1] = Ply:GetX()
  1345.                 Pos[2] = Ply:GetY()
  1346.                 Pos[3] = i*xort
  1347.                 Pos[4] = i*yort
  1348.                 Bishop:RegisterEvent("GoTo_Timer", 900, 1)
  1349.                 break
  1350.             elseif (Check_Feld(x+(i*xort),y+(i*yort)) ~= 0) then
  1351.                 break
  1352.             end
  1353.         end
  1354.     end
  1355.  
  1356.     if (Schachspiel == 0) and (zahler == Schachbrett[x][y][3][2]) then
  1357.         Schachspiel = f
  1358.         Bishop:RemoveEvents()
  1359.         Bishop:RegisterEvent("Check", 500, 0)
  1360.     end
  1361. end
  1362.  
  1363. function King_GoTo(King)
  1364.     if not Pcheck(Ply) then return end
  1365.     local x,y,f = xyi_Output(King)
  1366.     local xp,yp = xyi_Output(Ply)
  1367.     zahler = zahler+1
  1368.  
  1369.     if comparing(xp, yp, x, y) then
  1370.         Check(King)
  1371.     else
  1372.         for i=-1,1,1 do
  1373.             for o=-1,1,1 do
  1374.                 if (Check_Feld(x+i,y+o) ~= f) and (comparing(x+i,y+o, xp, yp)) then
  1375.                     Pos[1] = Ply:GetX()
  1376.                     Pos[2] = Ply:GetY()
  1377.                     Pos[3] = i
  1378.                     Pos[4] = o
  1379.                     King:RegisterEvent("GoTo_Timer", 900, 1)
  1380.                     break
  1381.                 end
  1382.             end
  1383.         end
  1384.  
  1385.         if (Schachbrett[x][y][4]) then
  1386.             if (f == 1) then
  1387.                 a = 8
  1388.                 b = 2
  1389.             else
  1390.                 a = 1
  1391.                 b = 8
  1392.             end
  1393.  
  1394.             if (Schachbrett[xp][yp][2] == b) then
  1395.                 if (xp == 8) then
  1396.                     if (Schachbrett[5][a][1] == 0) and (Schachbrett[6][a][1] == 0) and (Schachbrett[7][a][1] == 0) then
  1397.                         Pos[1] = Ply:GetX()
  1398.                         Pos[2] = Ply:GetY()
  1399.                         Pos[3] = 2
  1400.                         Pos[4] = 0
  1401.  
  1402.                         Pos2 = {}
  1403.                         Pos2[1] = Ply:GetX()
  1404.                         Pos2[2] = Ply:GetY()
  1405.                         Pos2[3] = -3
  1406.                         Pos2[4] = 0
  1407.                         Pos2[5] = Schachbrett[xp][yp]
  1408.  
  1409.                         King:RegisterEvent("GoTo_Timer", 950, 1)
  1410.                         King:RegisterEvent("Rochade", 900, 1)
  1411.                     end
  1412.                 else
  1413.                     if (Schachbrett[3][a][1] == 0) and (Schachbrett[2][a][1] == 0) then
  1414.                         Pos[1] = Ply:GetX()
  1415.                         Pos[2] = Ply:GetY()
  1416.                         Pos[3] = -2
  1417.                         Pos[4] = 0
  1418.  
  1419.                         Pos2 = {}
  1420.                         Pos2[1] = Ply:GetX()
  1421.                         Pos2[2] = Ply:GetY()
  1422.                         Pos2[3] = 2
  1423.                         Pos2[4] = 0
  1424.                         Pos2[5] = Schachbrett[xp][yp]
  1425.  
  1426.                         King:RegisterEvent("GoTo_Timer", 950, 1)
  1427.                         King:RegisterEvent("Rochade", 900, 1)
  1428.                     end
  1429.                 end
  1430.             end
  1431.         end
  1432.     end
  1433.  
  1434.     if (Schachspiel == 0) and (zahler == Schachbrett[x][y][3][2]) then
  1435.         Schachspiel = f
  1436.         King:RemoveEvents()
  1437.         King:RegisterEvent("Check", 500, 0)
  1438.     end
  1439. end
  1440.  
  1441. function Rochade(Unit)
  1442.     if not Pcheck(Ply) then return end
  1443.     local xp,yp = xyi_Output(Ply)
  1444.     local x,y = xyi_Output(Schachbrett[xp][yp][1])
  1445.  
  1446.     if comparing(Ply:GetX(), Ply:GetY(), Pos2[1], Pos2[2]) then
  1447.  
  1448.         xmove = Schachbrett[xp][yp][1]:GetX()+(Pos2[3]*size)
  1449.         ymove = Schachbrett[xp][yp][1]:GetY()+(Pos2[4]*size)
  1450.         Schachbrett[xp][yp][1]:MoveTo(xmove, ymove, Schachbrett[xp][yp][1]:GetZ(), Schachbrett[xp][yp][1]:GetO())
  1451.  
  1452.         Schachbrett[x+Pos2[3]][y+Pos2[4]] = {Schachbrett[x][y][1], Schachbrett[x][y][2], Schachbrett[x][y][3], false}
  1453.         Schachbrett[x][y] = {0,0}
  1454.     end
  1455. end
  1456.  
  1457. function Allianz_Konig_Dead(Unit)
  1458.     if not Pcheck(Ply) then return end
  1459.  
  1460.     math.randomseed(os.time())
  1461.     Ply:AddItem(Reward_Items[math.random(1,table.getn(Reward_Items))],1)
  1462.     Schachbrett[9][4]:SendChatMessage(14, 0, horde_win)
  1463.     Schachbrett[9][4]:RemoveEvents()
  1464.     Schachspiel = -1
  1465.     Game_over_timer = 30
  1466.     Schachbrett[9][4]:RegisterEvent("game_over", 1000, 0)
  1467. end
  1468.  
  1469. function Horde_Konig_Dead(Unit)
  1470.     if not Pcheck(Ply) then return end
  1471.  
  1472.     math.randomseed(os.time())
  1473.     Ply:AddItem(Reward_Items[math.random(1,table.getn(Reward_Items))],1)
  1474.     Schachbrett[9][4]:SendChatMessage(14, 0, ally_win)
  1475.     Schachbrett[9][4]:RemoveEvents()
  1476.     Schachspiel = -1
  1477.     Game_over_timer = 30
  1478.     Schachbrett[9][4]:RegisterEvent("game_over", 1000, 0)
  1479. end
  1480.  
  1481. --------------------------------------------------
  1482. ------          Unit Registration   Anfang  ------
  1483. --------------------------------------------------
  1484.  
  1485. -- Unit, SimpleID, {"xx_GoTo", xx_Timer}, true, selectemote, attacktype, spell/emote, targetspell, deathspell, deathspellid, KI_function
  1486.  
  1487. function Horde_Bauer(Unit)
  1488.     RegUnit(Unit, {Unit, 1, {"Pawn_GoTo", Pawn_Timer}, true, {2,66}, 1, {35,36,37,38,54,60}, 0, false, 0, Pawn_KI})
  1489. end
  1490.  
  1491. function Horde_Turm(Unit)
  1492.     RegUnit(Unit, {Unit, 2, {"Castle_GoTo", Castle_Timer}, true, {15,66} , 1, 51, 0, false, 0, Castle_KI})
  1493. end
  1494.  
  1495. function Horde_Pferd(Unit)
  1496.     RegUnit(Unit, {Unit, 3, {"Horse_GoTo", Horse_Timer}, false, 1, 1, 51, 0, false, 0, Horse_KI})
  1497. end
  1498.  
  1499. function Horde_Laufer(Unit)
  1500.     RegUnit(Unit, {Unit, 4, {"Bishop_GoTo", Bishop_Timer}, false, {2, 66}, 1, {51,60}, 0, false, 0, Bishop_KI})
  1501. end
  1502.  
  1503. function Horde_Konigin(Unit)
  1504.     RegUnit(Unit, {Unit, 5, {"Queen_GoTo", Queen_Timer}, false, {2, 66}, 1, {51,60}, 0, false, 0, Queen_KI})
  1505. end
  1506.  
  1507. function Horde_Konig(Unit)
  1508.     RegUnit(Unit, {Unit, 6, {"King_GoTo", King_Timer}, true, {2, 66}, 1, {51,60}, 0, true, 38006, King_KI})
  1509. end
  1510.  
  1511. function Allianz_Bauer(Unit)
  1512.     RegUnit(Unit, {Unit, 7, {"Pawn_GoTo", Pawn_Timer}, true, {2, 66}, 1, {51,60,54}, 0, false, 0, Pawn_KI})
  1513. end
  1514.  
  1515. function Allianz_Turm(Unit)
  1516.     RegUnit(Unit, {Unit, 8, {"Castle_GoTo", Castle_Timer}, true, 1, 1, {35,36,37}, 0, false, 0, Castle_KI})
  1517. end
  1518.  
  1519. function Allianz_Pferd(Unit)
  1520.     RegUnit(Unit, {Unit, 9, {"Horse_GoTo", Horse_Timer}, false, 1, 1, 51, 0, false, 0, Horse_KI})
  1521. end
  1522.  
  1523. function Allianz_Laufer(Unit)
  1524.     RegUnit(Unit, {Unit, 10, {"Bishop_GoTo", Bishop_Timer}, false, {2, 66}, 1, {51,60,54}, 0, false, 0, Bishop_KI})
  1525. end
  1526.  
  1527. function Allianz_Konigin(Unit)
  1528.     RegUnit(Unit, {Unit, 11, {"Queen_GoTo", Queen_Timer}, false, {2, 66}, 1, {51,60,54}, 0, false, 0, Queen_KI})
  1529. end
  1530.  
  1531. function Allianz_Konig(Unit)
  1532.     RegUnit(Unit, {Unit, 12, {"King_GoTo", King_Timer}, true, {2, 66}, 1, {51,60,54}, 0, true, 38006, King_KI})
  1533. end
  1534.  
  1535. --------------------------------------------------
  1536. ------          Unit Registration   Ende    ------
  1537. --------------------------------------------------
  1538.  
  1539.     --------------
  1540. ---|              |-------------------------------
  1541. ---| KI Steuerung |--   Build 1  -----------------
  1542. ---|              |-------------------------------
  1543.     --------------
  1544.  
  1545.  
  1546. function KI_Start(Unit)
  1547.     Schachspiel = Schachspiel * -1
  1548.  
  1549.     local KI_Chess = {}
  1550.  
  1551.     for x=1,8,1 do
  1552.         KI_Chess[x] = {}
  1553.     for y=1,8,1 do
  1554.         KI_Chess[x][y] = {}
  1555.         if (Schachbrett[x][y][2] == 0) then
  1556.             KI_Chess[x][y] = 0
  1557.         else
  1558.             KI_Chess[x][y] = Schachbrett[x][y][2]
  1559.         end
  1560.     end
  1561.     end
  1562.  
  1563.     for x=1,8,1 do
  1564.     for y=1,8,1 do
  1565.         if (Check_Feld_AR(x,y,KI_Chess) == math.abs(Schachspiel-3)) then
  1566.             KI_Chess[x][y] = {}
  1567.             KI_Chess[x][y][9] = {}
  1568.             KI_Chess[x][y][9][1] = 0
  1569.  
  1570.             for x1=1,8,1 do
  1571.             for y1=1,8,1 do
  1572.                 value = Schachbrett[x][y][11](x, y, x1, y1, math.abs(Schachspiel-3))
  1573.                 if type(value) == "number" then KI_Chess[x1][y1][9][1] = value end
  1574.             end
  1575.             end
  1576.         end
  1577.     end
  1578.     end
  1579.  
  1580.     for x=1,8,1 do
  1581.     for y=1,8,1 do
  1582.         if (Check_Feld_AR(x,y,KI_Chess) == Schachspiel) then
  1583.             KI_Chess[x][y] = {}
  1584.             for x1=1,8,1 do
  1585.                 KI_Chess[x][y][x1] = {}
  1586.             for y1=1,8,1 do
  1587.                 KI_Chess[x][y][x1][y1] = {}
  1588.                 KI_Chess[x][y][x1][y1] = Schachbrett[x][y][11](x, y, x1, y1, Schachspiel) + KI_Chess[x][y][9][1]
  1589.                 table.insert(KI_Wert, {KI_Chess[x][y][x1][y1], x, y, x1, y1})
  1590.             end
  1591.             end
  1592.         end
  1593.     end
  1594.     end
  1595.  
  1596.     local fav = {{0}}
  1597.     for i,k in pairs(KI_Wert) do
  1598.         if (fav[1][1] == k[1]) then
  1599.             table.insert(fav, k)
  1600.         elseif (k[1] > fav[1][1]) then
  1601.             fav[1] = k
  1602.         end
  1603.     end
  1604.  
  1605.     fav = fav[math.random(1, table.getn(fav))]
  1606.  
  1607.     Pos[1] = fav[4]
  1608.     Pos[2] = fav[5]
  1609.     Pos[3] = fav[4]-fav[2]
  1610.     Pos[4] = fav[5]-fav[3]
  1611.  
  1612.     if ((Schachbrett[fav[1]][fav[2]][2] == 1) or (Schachbrett[fav[1]][fav[2]][2] == 7)) and (math.abs(Pos[4]) == 2) then
  1613.         Pos[5] = "en passant input"
  1614.         Pos[6] = fav[4]
  1615.         Pos[7] = fav[5]-(Pos[4]/2)
  1616.     end
  1617.     if comparing(xp, yp, en_passant[1], en_passant[2]) and (comparing(xp, yp, x-1, y+i) or comparing(xp, yp, x-1, y+i)) then
  1618.         Pos[5] = "en passant input"
  1619.     end
  1620.  
  1621.     GoTo_Timer(Schachbrett[fav[1]][fav[2]][1])
  1622. end
  1623.  
  1624. function Queen_KI(x, y, xp, yp, f)
  1625.     local xort = 0
  1626.     local yort = 0
  1627.  
  1628.     if (xp < x) then
  1629.         xort = -1
  1630.     elseif (xp > x) then
  1631.         xort = 1
  1632.     end
  1633.  
  1634.     if (yp < y) then
  1635.         yort = -1
  1636.     elseif (yp > y) then
  1637.         yort = 1
  1638.     end
  1639.  
  1640.     for i=1,7,1 do
  1641.         local Feld = Check_Feld(i*xort+x, i*yort+y)
  1642.         if (Feld == f) then
  1643.             return 0
  1644.         elseif (Feld == math.abs(f-3)) and comparing(i*xort+x, i*yort+y, xp, yp) then
  1645.             return Wertigkeit[Schachbrett[xp][yp][2]]
  1646.         elseif (Feld == 0) and comparing(i*xort+x, i*yort+y, xp, yp) then
  1647.             return 1
  1648.         end
  1649.     end
  1650.  
  1651.     return 0
  1652. end
  1653.  
  1654. function Pawn_KI(x, y, xp, yp, f)
  1655.     zahler = zahler+1
  1656.  
  1657.     i = f-(2*(1/f))
  1658.  
  1659.     if  (comparing(xp, yp, x-1, y+i) or comparing(xp, yp, x+1, y+i)) and (Check_Feld(xp, y+i) == 2*(1/f)) then
  1660.         return Wertigkeit[Schachbrett[xp][yp][2]]
  1661.     elseif comparing(xp, yp, x, y+i) and (Check_Feld(x, y+i) == 0) then
  1662.         return 1
  1663.     elseif (Schachbrett[x][y][4]) and comparing(xp, yp, x, y+2*i) and (Check_Feld(x, y+2*i) == 0) then
  1664.         return 1
  1665.     elseif (en_passant ~= {}) then
  1666.         if comparing(xp, yp, en_passant[1], en_passant[2]) and comparing(xp, yp, x-1, y+i) then
  1667.             return 2
  1668.         elseif comparing(xp, yp, en_passant[1], en_passant[2]) and comparing(xp, yp, x+1, y+i) then
  1669.             return 2
  1670.         end
  1671.     end
  1672.  
  1673.     return 0
  1674. end
  1675.  
  1676. function Castle_KI(x, y, xp, yp, f)
  1677.     local xort = 0
  1678.     local yort = 0
  1679.  
  1680.     if (x == xp) then
  1681.         if (yp < y) then
  1682.             yort = -1
  1683.         else
  1684.             yort = 1
  1685.         end
  1686.     elseif (yp == y) then
  1687.         if (xp < x) then
  1688.             xort = -1
  1689.         else
  1690.             xort = 1
  1691.         end
  1692.     end
  1693.  
  1694.     for i=1,7,1 do
  1695.         local Feld = Check_Feld(i*xort+x, i*yort+y)
  1696.         if (Feld == f) then
  1697.             return 0
  1698.         elseif (Feld == math.abs(f-3)) and comparing(i*xort+x, i*yort+y, xp, yp) then
  1699.             return Wertigkeit[Schachbrett[xp][yp][2]]
  1700.         elseif (Feld == 0) and comparing(i*xort+x, i*yort+y, xp, yp) then
  1701.             return 1
  1702.         end
  1703.     end
  1704.  
  1705.     return 0
  1706. end
  1707.  
  1708. function Horse_KI(x, y, xp, yp, f)
  1709.     if (Check_Feld(x+1, y-2) == 0) and (math.abs(xp-x)+math.abs(yp-y) == 3) and ((math.abs(xp-x) == 1) or (math.abs(xp-x) == 2)) then
  1710.         return 1
  1711.     elseif (Check_Feld(x+1, y-2) == math.abs(f-3)) and (math.abs(xp-x)+math.abs(yp-y) == 3) and ((math.abs(xp-x) == 1) or (math.abs(xp-x) == 2)) then
  1712.         return Wertigkeit[Schachbrett[xp][yp][2]]
  1713.     end
  1714.  
  1715.     return 0
  1716. end
  1717.  
  1718. function Bishop_KI(x, y, xp, yp, f)
  1719.     local xort = 0
  1720.     local yort = 0
  1721.  
  1722.     if (xp < x) then
  1723.         xort = -1
  1724.     else
  1725.         xort = 1
  1726.     end
  1727.  
  1728.     if (yp < y) then
  1729.         yort = -1
  1730.     else
  1731.         yort = 1
  1732.     end
  1733.  
  1734.     for i=1,7,1 do
  1735.         local Feld = Check_Feld(i*xort+x, i*yort+y)
  1736.         if (Feld == f) then
  1737.             return 0
  1738.         elseif (Feld == math.abs(f-3)) and comparing(i*xort+x, i*yort+y, xp, yp) then
  1739.             return Wertigkeit[Schachbrett[xp][yp][2]]
  1740.         elseif (Feld == 0) and comparing(i*xort+x, i*yort+y, xp, yp) then
  1741.             return 1
  1742.         end
  1743.     end
  1744.  
  1745.     return 0
  1746. end
  1747.  
  1748. function King_KI(x, y, xp, yp, f)
  1749.     for i=-1,1,1 do
  1750.         for o=-1,1,1 do
  1751.             if (Check_Feld(x+i,y+o) == math.abs(f-3)) and (comparing(x+i,y+o, xp, yp)) then
  1752.                 return Wertigkeit[Schachbrett[xp][yp][2]]
  1753.             elseif (Check_Feld(x+i,y+o) == 0) and (comparing(x+i,y+o, xp, yp)) then
  1754.                 return 1
  1755.             end
  1756.         end
  1757.     end
  1758.  
  1759.     return 0
  1760. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement