globals.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. wormball.version = "03.11.2021.1"
  2. wormball.player_texture_save = {}
  3. wormball.storage = minetest.get_mod_storage()
  4. --lookup tables
  5. --colors: a list of color codes, indexed by name
  6. wormball.colors = {yellow = "fcba0388",
  7. orangered = "fc280388",
  8. darkred = "a1000088",
  9. lightgreen = "80ff0088",
  10. aqua = "00ff8488",
  11. lightblue = "0084ff88",
  12. darkblue = "0d00ff88",
  13. purple = "8000ff88",
  14. lightpurple = "ee00ff88",
  15. pink = "ff006f88"}
  16. --color_names: a list of color names, indexed by number
  17. wormball.color_names = {}
  18. local i=1
  19. for color,code in pairs(wormball.colors) do
  20. wormball.color_names[i] = color
  21. i=i+1
  22. end
  23. --reference tables for placing node rotations properly
  24. wormball.straight = {pxpx = 1,pzpz=0,pypy=4,
  25. nxnx = 1,nznz=0,nyny=4,
  26. nxpx = 1,nzpz=0,nypy=4,
  27. pxnx = 1,pznz=0,pyny=4, } --format: axis
  28. --format: sign,axis to sign,axis
  29. wormball.corner = { --from, to
  30. nynx=13,pynx=15,pznx=12,
  31. nypz=10,pypz=2,pzpx=7,
  32. nypx=21,pypx=17,nzpx=11,
  33. nynz=20,pynz=0 ,nznx=9,
  34. nxny=17,nxpy=19,nxpz=18,
  35. pzny=0,pzpy=20,pxpz=9,
  36. pxny=15,pxpy=13,pxnz=12,
  37. nzny=2,nzpy=10 ,nxnz=7,
  38. }
  39. --format: dir pitch(h,u,d)for horiz, up or down ; dir facing
  40. wormball.head = {
  41. hpz=0,hpx=1,hnz=2,hnx=3,
  42. dpz=4,dpx=13,dnz=10,dnx=19,
  43. upz=8,upx=17,unz=6,unx=15,}
  44. -- timer: a counter for keeping track of global step stuff
  45. wormball.timer = 0
  46. --place_node: a custom function for placing the head and second segment every movement. Uses rotation lookup tables above
  47. --when I do a place_node, I place the head in the facing dir and the pitch, and I update where the head (last )
  48. function wormball.place_node(nodes,dir,old_dir,look_dir,color) --dir should be:{x=1[-1],y=1[-1],z=1[-1]}
  49. --lookdir should be: 'px','nx','pz',or 'nz'
  50. if not look_dir then look_dir = 'py' end
  51. local dircode =''
  52. local old_dircode=''
  53. local type = 'straight' --type will be: straight, corner
  54. local axis = ''
  55. local old_axis = ''
  56. --dircode should ALWAYS be set, unless for some reason dir = {0,0,0}, which it shouldnt. Check this
  57. if dir.x ~= 0 then
  58. axis = 'x'
  59. if dir.x > 0 then
  60. dircode = 'px'
  61. else
  62. dircode = 'nx'
  63. end
  64. elseif dir.y ~= 0 then
  65. axis = 'y'
  66. if dir.y >0 then
  67. dircode = 'py'
  68. else
  69. dircode = 'ny'
  70. end
  71. elseif dir.z ~= 0 then
  72. axis = 'z'
  73. if dir.z > 0 then
  74. dircode = 'pz'
  75. else
  76. dircode = 'nz'
  77. end
  78. end
  79. if old_dir then
  80. if old_dir.x ~= 0 then
  81. old_axis = 'x'
  82. if old_dir.x > 0 then
  83. old_dircode = 'px'
  84. else
  85. old_dircode = 'nx'
  86. end
  87. elseif old_dir.y ~= 0 then
  88. old_axis = 'y'
  89. if old_dir.y > 0 then
  90. old_dircode = 'py'
  91. else
  92. old_dircode = 'ny'
  93. end
  94. elseif old_dir.z ~= 0 then
  95. old_axis = 'z'
  96. if old_dir.z > 0 then
  97. old_dircode = 'pz'
  98. else
  99. old_dircode = 'nz'
  100. end
  101. end
  102. if axis == old_axis then
  103. type = 'straight'
  104. else
  105. type = 'corner'
  106. end
  107. local full_dircode = old_dircode..dircode --from dir..to dir
  108. if type == 'straight' and #nodes > 1 then
  109. minetest.set_node(nodes[2], {name="wormball:straight_"..color, param2 = wormball.straight[full_dircode]})
  110. elseif type == 'corner' and #nodes > 1 then
  111. minetest.set_node(nodes[2], {name="wormball:corner_"..color, param2 = wormball.corner[full_dircode]})
  112. end
  113. end
  114. local p_dir = 'h' --p, for pitch
  115. if dir.y == 1 then
  116. p_dir = 'u'
  117. elseif dir.y == -1 then
  118. p_dir = 'd'
  119. end
  120. local head_dir = p_dir..look_dir
  121. minetest.set_node(nodes[1], {name="wormball:head_"..color, param2 = wormball.head[head_dir]})
  122. end
  123. --get_look_dir: a custom function that returns a simplified player look direction in the form of a direction code, that place_node uses
  124. function wormball.get_look_dir(arena,player)
  125. local yaw = player:get_look_horizontal()
  126. local look_dir
  127. --get look_dir
  128. if yaw < (3.14*.25) or yaw > (3.14 *(7/4)) then -- if we are looking in the +z direction,
  129. look_dir = 'pz'
  130. elseif yaw > (3.14*(1/4)) and yaw < (3.14 *(3/4)) then -- if we are looking in the -x direction
  131. look_dir = 'nx'
  132. elseif yaw > (3.14*(3/4)) and yaw < (3.14 *(5/4)) then -- if we are looking in the -z direction
  133. look_dir = 'nz'
  134. elseif yaw > (3.14*(5/4)) and yaw < (3.14 *(7/4)) then -- if we are looking in the +x direction
  135. look_dir = 'px'
  136. end
  137. return look_dir
  138. end
  139. -- detaches player, resets texture
  140. wormball.detach = function(p_name)
  141. local player = minetest.get_player_by_name(p_name) or nil
  142. --set texture back to normal... dont worry about disconnects... player_api handles setting textures on_join
  143. if player and wormball.player_texture_save[p_name] ~= nil then --nil checks
  144. player:set_properties({textures = wormball.player_texture_save[p_name]})
  145. end
  146. --detach and remove attachment entity, play losing sound
  147. if player then
  148. local att = player:get_attach()
  149. player:set_detach()
  150. if att then att:remove() end
  151. end
  152. end