allies.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. lottclasses.allies = {
  2. GAMEelf = {
  3. GAMEman = true,
  4. GAMEorc = false,
  5. GAMEhobbit = true,
  6. GAMEdwarf = true
  7. },
  8. GAMEman = {
  9. GAMEelf = true,
  10. GAMEorc = false,
  11. GAMEhobbit = true,
  12. GAMEdwarf = true
  13. },
  14. GAMEorc = {
  15. GAMEelf = false,
  16. GAMEman = false,
  17. GAMEhobbit = false,
  18. GAMEdwarf = false
  19. },
  20. GAMEhobbit = {
  21. GAMEelf = true,
  22. GAMEman = true,
  23. GAMEorc = false,
  24. GAMEdwarf = true
  25. },
  26. GAMEdwarf = {
  27. GAMEelf = true,
  28. GAMEman = true,
  29. GAMEorc = false,
  30. GAMEhobbit = true
  31. }
  32. }
  33. lottclasses.races = {
  34. "GAMEelf",
  35. "GAMEman",
  36. "GAMEorc",
  37. "GAMEhobbit",
  38. "GAMEdwarf"
  39. }
  40. lottclasses.races_prefix = {
  41. "Elven",
  42. "Human",
  43. "Orc",
  44. "Hobbit",
  45. "Dwarven"
  46. }
  47. lottclasses.races_pretty = {
  48. "Elves",
  49. "Men",
  50. "Orcs",
  51. "Hobbits",
  52. "Dwarves"
  53. }
  54. lottclasses.player_same_race_or_ally = function(player, race)
  55. local player_privs = minetest.get_player_privs(player:get_player_name())
  56. local player_race = nil
  57. for i, v in pairs(lottclasses.races) do
  58. player_race = nil
  59. if player_privs[v] then
  60. player_race = v
  61. end
  62. if player_race == race or lottclasses.allies[race][player_race] then
  63. return true
  64. end
  65. end
  66. return false
  67. end
  68. lottclasses.player_race_in_table = function(player, races)
  69. local player_privs = minetest.get_player_privs(player:get_player_name())
  70. for i, v in pairs(races) do
  71. if player_privs[i] then
  72. return true
  73. end
  74. end
  75. return false
  76. end
  77. lottclasses.lua_ent_same_race_or_ally = function(lua_ent, race)
  78. if not race or race == "" or not lua_ent.race or lua_ent.race == "" then
  79. return false
  80. end
  81. if race == "ents" then
  82. if lua_ent.race == "ents" then
  83. return true
  84. end
  85. return false
  86. end
  87. if lua_ent.type == "npc" and (lua_ent.race == "ents" or race == lua_ent.race or lottclasses.allies[race][lua_ent.race]) then
  88. return true
  89. end
  90. end
  91. lottclasses.npc_same_race_or_ally = function(npc, race)
  92. local lua_ent = npc:get_luaentity()
  93. if lua_ent then
  94. return lottclasses.lua_ent_same_race_or_ally(lua_ent, race)
  95. end
  96. return false
  97. end
  98. lottclasses.obj_same_race_or_ally = function(obj, race)
  99. local player_race = nil
  100. if obj:is_player() then
  101. return lottclasses.player_same_race_or_ally(obj, race)
  102. else
  103. return lottclasses.npc_same_race_or_ally(obj, race)
  104. end
  105. return false
  106. end
  107. local file = io.open(minetest.get_worldpath().."/"..SAVEDIR.."/allies.txt", "r")
  108. if file then
  109. lottclasses.allies = minetest.deserialize(file:read("*all"))
  110. file:close()
  111. end
  112. minetest.register_privilege("allies", {
  113. description = "Can change the allies",
  114. })
  115. minetest.register_chatcommand("allies", {
  116. params = "",
  117. description = "Change Allies",
  118. func = function(name, param)
  119. lottclasses.show_allies_formspec(name)
  120. end,
  121. })
  122. lottclasses.show_allies_formspec = function(player)
  123. local x, y = nil
  124. local player_privs = minetest.get_player_privs(player)
  125. if not player_privs.allies then return end
  126. local formspec = "size[8,12]label[0,0;Allies:]"
  127. y = 1
  128. for i = 1, 5, 1 do
  129. if player_privs.server or player_privs[lottclasses.races[i]] then
  130. formspec = formspec.."label[0,"..y..";"..lottclasses.races_pretty[i]..":]"
  131. x = 0
  132. for j = 1, 5, 1 do
  133. if lottclasses.races[i] ~= lottclasses.races[j] then
  134. formspec = formspec..
  135. "checkbox["..x..","..(y + 1)..";"..lottclasses.races[i].."_"..
  136. lottclasses.races[j]..";"..lottclasses.races_pretty[j]..";"..
  137. tostring(lottclasses.allies[lottclasses.races[i]][lottclasses.races[j]]).."]"
  138. x = x + 2
  139. end
  140. end
  141. y = y + 2
  142. end
  143. end
  144. formspec = formspec.."button_exit[0,11;2,1;exit_button;Proceed]"
  145. minetest.show_formspec(player, "ally_settings", formspec)
  146. end
  147. lottclasses.change_ally_settings = function(fields)
  148. local race1, race2, field_name = nil, nil, nil
  149. for i = 1, 5, 1 do
  150. for j = 1, 5, 1 do
  151. race1 = lottclasses.races[i]
  152. race2 = lottclasses.races[j]
  153. field_name = race1.."_"..race2
  154. if fields[field_name] == "true" then
  155. lottclasses.allies[race1][race2] = true
  156. elseif fields[field_name] == "false" then
  157. lottclasses.allies[race1][race2] = false
  158. end
  159. end
  160. end
  161. lottclasses.save_allies()
  162. end
  163. minetest.register_on_player_receive_fields(
  164. function(player, formname, fields)
  165. if formname == "ally_settings" then
  166. lottclasses.change_ally_settings(fields)
  167. end
  168. end
  169. )
  170. lottclasses.save_allies = function()
  171. minetest.mkdir(minetest.get_worldpath().."/"..SAVEDIR)
  172. local file = io.open(minetest.get_worldpath().."/"..SAVEDIR.."/allies.txt", "w")
  173. if (file) then
  174. file:write(minetest.serialize(lottclasses.allies))
  175. file:close()
  176. end
  177. end