init.lua 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. local modpath = minetest.get_modpath(minetest.get_current_modname())
  2. if minetest.global_exists("armor") and armor.attributes then
  3. table.insert(armor.attributes, "bullet_res")
  4. end
  5. if minetest.global_exists("armor") and armor.attributes then
  6. table.insert(armor.attributes, "ammo_save")
  7. end
  8. if minetest.global_exists("armor") and armor.attributes then
  9. table.insert(armor.attributes, "ranged_dmg")
  10. end
  11. minetest.register_node("rangedweapons:antigun_block", {
  12. description = "" ..core.colorize("#35cdff","Anti-gun block\n")..core.colorize("#FFFFFF", "Prevents people from using guns, in 10 node radius to each side from this block"),
  13. tiles = {"rangedweapons_antigun_block.png"},
  14. groups = {choppy = 3, oddly_breakable_by_hand = 3},
  15. sounds = default.node_sound_wood_defaults(),
  16. })
  17. ----
  18. ---- gun_funcs
  19. ----
  20. make_sparks = function(pos)
  21. minetest.sound_play("rengedweapons_ricochet", {pos, gain = 0.75})
  22. for i=1,9 do
  23. minetest.add_particle({
  24. pos = pos,
  25. velocity = {x=math.random(-6.0,6.0), y=math.random(-10.0,15.0), z=math.random(-6.0,6.0)},
  26. acceleration = {x=math.random(-9.0,9.0), y=math.random(-15.0,-3.0), z=math.random(-9.0,9.0)},
  27. expirationtime = 1.0,
  28. size = math.random(1,2),
  29. collisiondetection = true,
  30. vertical = false,
  31. texture = "rangedweapons_spark.png",
  32. glow = 25,
  33. })
  34. end
  35. end
  36. rangedweapons_gain_skill = function(player,skill,chance)
  37. if math.random(1, chance) == 1 then
  38. local p_meta = player:get_meta()
  39. local skill_num = p_meta:get_int(skill)
  40. if skill_num < rweapons_max_gun_efficiency then
  41. p_meta:set_int(skill, skill_num + 1)
  42. minetest.chat_send_player(player:get_player_name(), "" ..core.colorize("#25c200","You've improved your skill with this type of gun!"))
  43. end
  44. end
  45. end
  46. rangedweapons_reload_gun = function(itemstack, player)
  47. GunCaps = itemstack:get_definition().RW_gun_capabilities
  48. if GunCaps ~= nil then
  49. gun_unload_sound = GunCaps.gun_unload_sound or ""
  50. end
  51. minetest.sound_play(gun_unload_sound, {player})
  52. local gun_reload = 0.25
  53. if GunCaps ~= nil then
  54. gun_reload = GunCaps.gun_reload or 0.25
  55. end
  56. local playerMeta = player:get_meta()
  57. local gunMeta = itemstack:get_meta()
  58. gunMeta:set_float("RW_reload_delay",gun_reload)
  59. playerMeta:set_float("rw_cooldown",gun_reload)
  60. local player_has_ammo = 0
  61. local clipSize = 0
  62. local reload_ammo = ""
  63. if GunCaps.suitable_ammo ~= nil then
  64. local inv = player:get_inventory()
  65. for i = 1,inv:get_size("main") do
  66. for _, ammo in pairs(GunCaps.suitable_ammo) do
  67. if inv:get_stack("main",i):get_name() == ammo[1] then
  68. reload_ammo = inv:get_stack("main",i)
  69. clipSize = ammo[2]
  70. player_has_ammo = 1
  71. break
  72. end end
  73. if player_has_ammo == 1 then
  74. break
  75. end
  76. end end
  77. if player_has_ammo == 1 then
  78. local gun_icon = "rangedweapons_emergency_gun_icon.png"
  79. if GunCaps.gun_icon ~= nil then
  80. gun_icon = GunCaps.gun_icon
  81. end
  82. local ammo_icon = "rangedweapons_emergency_ammo_icon.png"
  83. if reload_ammo:get_definition().inventory_image ~= nil then
  84. ammo_icon = reload_ammo:get_definition().inventory_image
  85. end
  86. player:hud_change(gunimg, "text", gun_icon)
  87. player:hud_change(ammoimg, "text", ammo_icon)
  88. local gunMeta = itemstack:get_meta()
  89. local ammoCount = gunMeta:get_int("RW_bullets")
  90. local ammoName = gunMeta:get_string("RW_ammo_name")
  91. local inv = player:get_inventory()
  92. inv:add_item("main",ammoName.." "..ammoCount)
  93. if inv:contains_item("main",reload_ammo:get_name().." "..clipSize) then
  94. inv:remove_item("main",reload_ammo:get_name().." "..clipSize)
  95. gunMeta:set_int("RW_bullets",clipSize)
  96. else
  97. gunMeta:set_int("RW_bullets",reload_ammo:get_count())
  98. inv:remove_item("main",reload_ammo:get_name().." "..reload_ammo:get_count())
  99. end
  100. gunMeta:set_string("RW_ammo_name",reload_ammo:get_name())
  101. player:hud_change(gunammo, "text", gunMeta:get_int("RW_bullets"))
  102. if GunCaps.gun_magazine ~= nil then
  103. local pos = player:get_pos()
  104. local dir = player:get_look_dir()
  105. local yaw = player:get_look_yaw()
  106. if pos and dir and yaw then
  107. pos.y = pos.y + 1.4
  108. local obj = minetest.add_entity(pos,"rangedweapons:mag")
  109. if obj then
  110. obj:set_properties({textures = {GunCaps.gun_magazine}})
  111. obj:set_velocity({x=dir.x*2, y=dir.y*2, z=dir.z*2})
  112. obj:set_acceleration({x=0, y=-5, z=0})
  113. obj:set_rotation({x=0,y=yaw+math.pi,z=0})
  114. end end end
  115. if GunCaps.gun_unloaded ~= nil then
  116. itemstack:set_name(GunCaps.gun_unloaded)
  117. end
  118. end
  119. end
  120. rangedweapons_single_load_gun = function(itemstack, player)
  121. GunCaps = itemstack:get_definition().RW_gun_capabilities
  122. if GunCaps ~= nil then
  123. gun_unload_sound = GunCaps.gun_unload_sound or ""
  124. end
  125. minetest.sound_play(gun_unload_sound, {player})
  126. local gun_reload = 0.25
  127. if GunCaps ~= nil then
  128. gun_reload = GunCaps.gun_reload or 0.25
  129. end
  130. local playerMeta = player:get_meta()
  131. local gunMeta = itemstack:get_meta()
  132. gunMeta:set_float("RW_reload_delay",gun_reload)
  133. playerMeta:set_float("rw_cooldown",gun_reload)
  134. local player_has_ammo = 0
  135. local clipSize = 0
  136. local reload_ammo = ""
  137. if GunCaps.suitable_ammo ~= nil then
  138. local inv = player:get_inventory()
  139. for i = 1,inv:get_size("main") do
  140. for _, ammo in pairs(GunCaps.suitable_ammo) do
  141. if inv:get_stack("main",i):get_name() == ammo[1] then
  142. reload_ammo = inv:get_stack("main",i)
  143. clipSize = ammo[2]
  144. player_has_ammo = 1
  145. break
  146. end end
  147. if player_has_ammo == 1 then
  148. break
  149. end
  150. end end
  151. if player_has_ammo == 1 then
  152. local gun_icon = "rangedweapons_emergency_gun_icon.png"
  153. if GunCaps.gun_icon ~= nil then
  154. gun_icon = GunCaps.gun_icon
  155. end
  156. local ammo_icon = "rangedweapons_emergency_ammo_icon.png"
  157. if reload_ammo:get_definition().inventory_image ~= nil then
  158. ammo_icon = reload_ammo:get_definition().inventory_image
  159. end
  160. player:hud_change(gunimg, "text", gun_icon)
  161. player:hud_change(ammoimg, "text", ammo_icon)
  162. local gunMeta = itemstack:get_meta()
  163. local ammoCount = gunMeta:get_int("RW_bullets")
  164. local ammoName = gunMeta:get_string("RW_ammo_name")
  165. local inv = player:get_inventory()
  166. if ammoName ~= reload_ammo:get_name() then
  167. inv:add_item("main",ammoName.." "..ammoCount)
  168. gunMeta:set_int("RW_bullets",0)
  169. end
  170. if inv:contains_item("main",reload_ammo:get_name()) and
  171. gunMeta:get_int("RW_bullets") < clipSize then
  172. inv:remove_item("main",reload_ammo:get_name())
  173. gunMeta:set_int("RW_bullets",gunMeta:get_int("RW_bullets")+1)
  174. end
  175. gunMeta:set_string("RW_ammo_name",reload_ammo:get_name())
  176. player:hud_change(gunammo, "text", gunMeta:get_int("RW_bullets"))
  177. if GunCaps.gun_unloaded ~= nil then
  178. itemstack:set_name(GunCaps.gun_unloaded)
  179. end
  180. end
  181. end
  182. rangedweapons_yeet = function(itemstack, player)
  183. if minetest.find_node_near(player:getpos(), 10,"rangedweapons:antigun_block")
  184. then
  185. minetest.chat_send_player(player:get_player_name(), "" ..core.colorize("#ff0000","throwable weapons are prohibited in this area!"))
  186. else
  187. ThrowCaps = itemstack:get_definition().RW_throw_capabilities
  188. playerMeta = player:get_meta()
  189. if ThrowCaps ~= nil then
  190. throw_cooldown = ThrowCaps.throw_cooldown or 0
  191. end
  192. if playerMeta:get_float("rw_cooldown") <= 0 then
  193. playerMeta:set_float("rw_cooldown", throw_cooldown)
  194. local throw_damage = {fleshy=1}
  195. local throw_sound = "rangedweapons_throw"
  196. local throw_velocity = 20
  197. local throw_accuracy = 100
  198. local throw_cooling = 0
  199. local throw_crit = 0
  200. local throw_critEffc = 1
  201. local throw_mobPen = 0
  202. local throw_nodePen = 0
  203. local throw_dps = 0
  204. local throw_gravity = 0
  205. local throw_door_breaking = 0
  206. local throw_skill = ""
  207. local throw_skillChance = 0
  208. local throw_smokeSize = 0
  209. local throw_ent = "rangedweapons:shot_bullet"
  210. local throw_visual = "wielditem"
  211. local throw_texture = "rangedweapons:shot_bullet_visual"
  212. local throw_glass_breaking = 0
  213. local throw_particles = {}
  214. local throw_sparks = 0
  215. local throw_bomb_ignite = 0
  216. local throw_size = 0
  217. local throw_glow = 0
  218. if ThrowCaps ~= nil then
  219. throw_damage = ThrowCaps.throw_damage or {fleshy=1}
  220. throw_sound = ThrowCaps.throw_sound or "rangedweapons_glock"
  221. throw_velocity = ThrowCaps.throw_velocity or 20
  222. throw_accuracy = ThrowCaps.throw_accuracy or 100
  223. throw_cooling = ThrowCaps.throw_cooling or itemstack:get_name()
  224. throw_crit = ThrowCaps.throw_crit or 0
  225. throw_critEffc = ThrowCaps.throw_critEffc or 1
  226. throw_projectiles = ThrowCaps.throw_projectiles or 1
  227. throw_mobPen = ThrowCaps.throw_mob_penetration or 0
  228. throw_nodePen = ThrowCaps.throw_node_penetration or 0
  229. throw_dps = ThrowCaps.throw_dps or 0
  230. throw_gravity = ThrowCaps.throw_gravity or 0
  231. throw_door_breaking = ThrowCaps.throw_door_breaking or 0
  232. throw_ent = ThrowCaps.throw_entity or "rangedweapons:shot_bullet"
  233. throw_visual = ThrowCaps.throw_visual or "wielditem"
  234. throw_texture = ThrowCaps.throw_texture or "rangedweapons:shot_bullet_visual"
  235. throw_glass_breaking = ThrowCaps.throw_glass_breaking or 0
  236. throw_particles = ThrowCaps.throw_particles or nil
  237. throw_sparks = ThrowCaps.throw_sparks or 0
  238. throw_bomb_ignite = ThrowCaps.ignites_explosives or 0
  239. throw_size = ThrowCaps.throw_projectile_size or 0
  240. throw_glow = ThrowCaps.throw_projectile_glow or 0
  241. OnCollision = ThrowCaps.OnCollision or function()end
  242. if ThrowCaps.throw_skill ~= nil then
  243. throw_skill = ThrowCaps.throw_skill[1] or ""
  244. throw_skillChance = ThrowCaps.throw_skill[2] or 0
  245. else
  246. throw_skill = ""
  247. throw_skillChance = 0
  248. end
  249. end
  250. if throw_skillChance > 0 and throw_skill ~= "" then
  251. rangedweapons_gain_skill(player,throw_skill,throw_skillChance)
  252. end
  253. if throw_skill ~= "" then
  254. skill_value = playerMeta:get_int(throw_skill)/100
  255. else
  256. skill_value = 1
  257. end
  258. rangedweapons_launch_projectile(player,throw_projectiles,throw_damage,throw_ent,throw_visual,throw_texture,throw_sound,throw_velocity,throw_accuracy,skill_value,OnCollision,throw_crit,throw_critEffc,throw_mobPen,throw_nodePen,0,"","","",throw_dps,throw_gravity,throw_door_breaking,throw_glass_breaking,throw_particles,throw_sparks,throw_bomb_ignite,throw_size,0,itemstack:get_wear(),throw_glow)
  259. itemstack:take_item()
  260. end
  261. end
  262. end
  263. rangedweapons_shoot_gun = function(itemstack, player)
  264. if minetest.find_node_near(player:getpos(), 10,"rangedweapons:antigun_block")
  265. then
  266. minetest.sound_play("rangedweapons_empty", {player})
  267. minetest.chat_send_player(player:get_player_name(), "" ..core.colorize("#ff0000","Guns are prohibited in this area!"))
  268. else
  269. local gun_cooldown = 0
  270. local GunCaps = itemstack:get_definition().RW_gun_capabilities
  271. local gun_ammo_save = 0
  272. if GunCaps ~= nil then
  273. gun_cooldown = GunCaps.gun_cooldown or 0
  274. gun_ammo_save = GunCaps.ammo_saving or 0
  275. end
  276. local gunMeta = itemstack:get_meta()
  277. local playerMeta = player:get_meta()
  278. if gunMeta:get_int("RW_bullets") > 0 and
  279. playerMeta:get_float("rw_cooldown") <= 0 then
  280. playerMeta:set_float("rw_cooldown", gun_cooldown)
  281. if math.random(1,100) > gun_ammo_save then
  282. gunMeta:set_int("RW_bullets",gunMeta:get_int("RW_bullets")-1)
  283. end
  284. player:hud_change(gunammo, "text", gunMeta:get_int("RW_bullets"))
  285. local gun_icon = "rangedweapons_emergency_gun_icon.png"
  286. if GunCaps.gun_icon ~= nil then
  287. gun_icon = GunCaps.gun_icon
  288. end
  289. player:hud_change(gunimg, "text", gun_icon)
  290. local OnCollision = function() end
  291. local bulletStack = ItemStack({name = gunMeta:get_string("RW_ammo_name")})
  292. AmmoCaps = bulletStack:get_definition().RW_ammo_capabilities
  293. local gun_damage = {fleshy=1}
  294. local gun_sound = "rangedweapons_glock"
  295. local gun_velocity = 20
  296. local gun_accuracy = 100
  297. local gun_cooling = 0
  298. local gun_crit = 0
  299. local gun_critEffc = 1
  300. local gun_mobPen = 0
  301. local gun_nodePen = 0
  302. local gun_shell = 0
  303. local gun_durability = 0
  304. local gun_dps = 0
  305. local gun_gravity = 0
  306. local gun_door_breaking = 0
  307. local gun_skill = ""
  308. local gun_skillChance = 0
  309. local gun_smokeSize = 0
  310. local bullet_damage = {fleshy=0}
  311. local bullet_velocity = 0
  312. local bullet_ent = "rangedweapons:shot_bullet"
  313. local bullet_visual = "wielditem"
  314. local bullet_texture = "rangedweapons:shot_bullet_visual"
  315. local bullet_crit = 0
  316. local bullet_critEffc = 0
  317. local bullet_projMult = 1
  318. local bullet_mobPen = 0
  319. local bullet_nodePen = 0
  320. local bullet_shell_ent = "rangedweapons:empty_shell"
  321. local bullet_shell_visual = "wielditem"
  322. local bullet_shell_texture = "rangedweapons:shelldrop"
  323. local bullet_dps = 0
  324. local bullet_gravity = 0
  325. local bullet_glass_breaking = 0
  326. local bullet_particles = {}
  327. local bullet_sparks = 0
  328. local bullet_bomb_ignite = 0
  329. local bullet_size = 0
  330. local bullet_glow = 20
  331. if GunCaps ~= nil then
  332. gun_damage = GunCaps.gun_damage or {fleshy=1}
  333. gun_sound = GunCaps.gun_sound or "rangedweapons_glock"
  334. gun_velocity = GunCaps.gun_velocity or 20
  335. gun_accuracy = GunCaps.gun_accuracy or 100
  336. gun_cooling = GunCaps.gun_cooling or itemstack:get_name()
  337. gun_crit = GunCaps.gun_crit or 0
  338. gun_critEffc = GunCaps.gun_critEffc or 1
  339. gun_projectiles = GunCaps.gun_projectiles or 1
  340. gun_mobPen = GunCaps.gun_mob_penetration or 0
  341. gun_nodePen = GunCaps.gun_node_penetration or 0
  342. gun_shell = GunCaps.has_shell or 0
  343. gun_durability = GunCaps.gun_durability or 0
  344. gun_dps = GunCaps.gun_dps or 0
  345. gun_ammo_save = GunCaps.ammo_saving or 0
  346. gun_gravity = GunCaps.gun_gravity or 0
  347. gun_door_breaking = GunCaps.gun_door_breaking or 0
  348. gun_smokeSize = GunCaps.gun_smokeSize or 0
  349. if GunCaps.gun_skill ~= nil then
  350. gun_skill = GunCaps.gun_skill[1] or ""
  351. gun_skillChance = GunCaps.gun_skill[2] or 0
  352. else
  353. gun_skill = ""
  354. gun_skillChance = 0
  355. end
  356. end
  357. if gun_skillChance > 0 and gun_skill ~= "" then
  358. rangedweapons_gain_skill(player,gun_skill,gun_skillChance)
  359. end
  360. local ammo_icon = "rangedweapons_emergency_ammo_icon.png"
  361. if bulletStack:get_definition().inventory_image ~= nil then
  362. ammo_icon = bulletStack:get_definition().inventory_image
  363. end
  364. player:hud_change(ammoimg, "text", ammo_icon)
  365. if AmmoCaps ~= nil then
  366. OnCollision = AmmoCaps.OnCollision or function()end
  367. bullet_damage = AmmoCaps.ammo_damage or {fleshy=1}
  368. bullet_velocity = AmmoCaps.ammo_velocity or 0
  369. bullet_ent = AmmoCaps.ammo_entity or "rangedweapons:shot_bullet"
  370. bullet_visual = AmmoCaps.ammo_visual or "wielditem"
  371. bullet_texture = AmmoCaps.ammo_texture or "rangedweapons:shot_bullet_visual"
  372. bullet_crit = AmmoCaps.ammo_crit or 0
  373. bullet_critEffc = AmmoCaps.ammo_critEffc or 0
  374. bullet_projMult = AmmoCaps.ammo_projectile_multiplier or 1
  375. bullet_mobPen = AmmoCaps.ammo_mob_penetration or 0
  376. bullet_nodePen = AmmoCaps.ammo_node_penetration or 0
  377. bullet_shell_ent = AmmoCaps.shell_entity or "rangedweapons:empty_shell"
  378. bullet_shell_visual = AmmoCaps.shell_visual or "wielditem"
  379. bullet_shell_texture = AmmoCaps.shell_texture or "rangedweapons:shelldrop"
  380. bullet_dps = AmmoCaps.ammo_dps or 0
  381. bullet_gravity = AmmoCaps.ammo_gravity or 0
  382. bullet_glass_breaking = AmmoCaps.ammo_glass_breaking or 0
  383. bullet_particles = AmmoCaps.ammo_particles or nil
  384. bullet_sparks = AmmoCaps.has_sparks or 0
  385. bullet_bomb_ignite = AmmoCaps.ignites_explosives or 0
  386. bullet_size = AmmoCaps.ammo_projectile_size or 0.0025
  387. bullet_glow = AmmoCaps.ammo_projectile_glow or 20
  388. end
  389. local combined_crit = gun_crit + bullet_crit
  390. local combined_critEffc = gun_critEffc + bullet_critEffc
  391. local combined_velocity = gun_velocity + bullet_velocity
  392. local combined_projNum = math.ceil(gun_projectiles * bullet_projMult)
  393. local combined_mobPen = gun_mobPen + bullet_mobPen
  394. local combined_nodePen = gun_nodePen + bullet_nodePen
  395. local combined_dps = gun_dps + bullet_dps
  396. local combined_dmg = {}
  397. local combined_gravity = gun_gravity + bullet_gravity
  398. for _, gunDmg in pairs(gun_damage) do
  399. if bullet_damage[_] ~= nil then
  400. combined_dmg[_] = gun_damage[_] + bullet_damage[_]
  401. else
  402. combined_dmg[_] = gun_damage[_]
  403. end
  404. end
  405. for _, bulletDmg in pairs(bullet_damage) do
  406. if gun_damage[_] == nil then
  407. combined_dmg[_] = bullet_damage[_]
  408. end
  409. end
  410. --minetest.chat_send_all(minetest.serialize(combined_dmg))
  411. if gun_skill ~= "" then
  412. skill_value = playerMeta:get_int(gun_skill)/100
  413. else
  414. skill_value = 1
  415. end
  416. rangedweapons_launch_projectile(player,combined_projNum,combined_dmg,bullet_ent,bullet_visual,bullet_texture,gun_sound,combined_velocity,gun_accuracy,skill_value,OnCollision,combined_crit,combined_critEffc,combined_mobPen,combined_nodePen,gun_shell,bullet_shell_ent,bullet_shell_texture,bullet_shell_visual,combined_dps,combined_gravity,gun_door_breaking,bullet_glass_breaking,bullet_particles,bullet_sparks,bullet_bomb_ignite,bullet_size,gun_smokeSize,0,bullet_glow)
  417. if rweapons_gun_durability == "true" then
  418. itemstack:add_wear(65535/gun_durability)
  419. end
  420. itemstack:set_name(gun_cooling)
  421. end
  422. end end
  423. rangedweapons_shoot_powergun = function(itemstack, player)
  424. if minetest.find_node_near(player:getpos(), 10,"rangedweapons:antigun_block")
  425. then
  426. minetest.sound_play("rangedweapons_empty", {player})
  427. minetest.chat_send_player(player:get_player_name(), "" ..core.colorize("#ff0000","Guns are prohibited in this area!"))
  428. else
  429. local power_cooldown = 0
  430. PowerCaps = itemstack:get_definition().RW_powergun_capabilities
  431. if PowerCaps ~= nil then
  432. power_cooldown = PowerCaps.power_cooldown or 0
  433. power_consumption = PowerCaps.power_consumption or 0
  434. end
  435. local inv = player:get_inventory()
  436. local playerMeta = player:get_meta()
  437. if inv:contains_item("main", "rangedweapons:power_particle "..PowerCaps.power_consumption) and
  438. playerMeta:get_float("rw_cooldown") <= 0 then
  439. playerMeta:set_float("rw_cooldown", power_cooldown)
  440. local OnCollision = function() end
  441. local power_damage = {fleshy=1}
  442. local power_sound = "rangedweapons_laser"
  443. local power_velocity = 20
  444. local power_accuracy = 100
  445. local power_cooling = 0
  446. local power_crit = 0
  447. local power_critEffc = 1
  448. local power_mobPen = 0
  449. local power_nodePen = 0
  450. local power_durability = 0
  451. local power_dps = 0
  452. local power_gravity = 0
  453. local power_door_breaking = 0
  454. local power_skill = ""
  455. local power_skillChance = 0
  456. local power_ent = "rangedweapons:shot_bullet"
  457. local power_visual = "wielditem"
  458. local power_texture = "rangedweapons:shot_bullet_visual"
  459. local power_glass_breaking = 0
  460. local power_particles = {}
  461. local power_sparks = 0
  462. local power_bomb_ignite = 0
  463. local power_size = 0
  464. local power_glow = 20
  465. if PowerCaps ~= nil then
  466. power_damage = PowerCaps.power_damage or {fleshy=1}
  467. power_sound = PowerCaps.power_sound or "rangedweapons_glock"
  468. power_velocity = PowerCaps.power_velocity or 20
  469. power_accuracy = PowerCaps.power_accuracy or 100
  470. power_cooling = PowerCaps.power_cooling or itemstack:get_name()
  471. power_crit = PowerCaps.power_crit or 0
  472. power_critEffc = PowerCaps.power_critEffc or 1
  473. power_projectiles = PowerCaps.power_projectiles or 1
  474. power_mobPen = PowerCaps.power_mob_penetration or 0
  475. power_nodePen = PowerCaps.power_node_penetration or 0
  476. power_durability = PowerCaps.power_durability or 0
  477. power_dps = PowerCaps.power_dps or 0
  478. power_gravity = PowerCaps.power_gravity or 0
  479. power_door_breaking = PowerCaps.power_door_breaking or 0
  480. OnCollision = PowerCaps.OnCollision or function()end
  481. power_ent = PowerCaps.power_entity or "rangedweapons:shot_bullet"
  482. power_visual = PowerCaps.power_visual or "wielditem"
  483. power_texture = PowerCaps.power_texture or "rangedweapons:shot_bullet_visual"
  484. power_glass_breaking = PowerCaps.power_glass_breaking or 0
  485. power_particles = PowerCaps.power_particles or nil
  486. power_sparks = PowerCaps.has_sparks or 0
  487. power_bomb_ignite = PowerCaps.ignites_explosives or 0
  488. power_size = PowerCaps.power_projectile_size or 0.0025
  489. power_glow = PowerCaps.power_projectile_glow or 20
  490. if PowerCaps.power_skill ~= nil then
  491. power_skill = PowerCaps.power_skill[1] or ""
  492. power_skillChance = PowerCaps.power_skill[2] or 0
  493. else
  494. power_skill = ""
  495. power_skillChance = 0
  496. end
  497. end
  498. if power_skillChance > 0 and power_skill ~= "" then
  499. rangedweapons_gain_skill(player,power_skill,power_skillChance)
  500. end
  501. if power_skill ~= "" then
  502. skill_value = playerMeta:get_int(power_skill)/100
  503. else
  504. skill_value = 1
  505. end
  506. rangedweapons_launch_projectile(player,power_projectiles,power_damage,power_ent,power_visual,power_texture,power_sound,power_velocity,power_accuracy,skill_value,OnCollision,power_crit,power_critEffc,power_mobPen,power_nodePen,0,"","","",power_dps,power_gravity,power_door_breaking,power_glass_breaking,power_particles,power_sparks,power_bomb_ignite,power_size,0,0,power_glow)
  507. if rweapons_gun_durability == "true" then
  508. itemstack:add_wear(65535/power_durability)
  509. end
  510. itemstack:set_name(power_cooling)
  511. inv:remove_item("main", "rangedweapons:power_particle "..PowerCaps.power_consumption)
  512. end
  513. end end
  514. rangedweapons_launch_projectile = function(player,projNum,projDmg,projEnt,visualType,texture,shoot_sound,combined_velocity,accuracy,skill_value,ColResult,projCrit,projCritEffc,mobPen,nodePen,has_shell,shellEnt,shellTexture,shellVisual,dps,gravity,door_break,glass_break,bullet_particles,sparks,ignite,size,smokeSize,proj_wear,proj_glow)
  515. --minetest.chat_send_all(accuracy)
  516. ----------------------------------
  517. local pos = player:get_pos()
  518. local dir = player:get_look_dir()
  519. local yaw = player:get_look_yaw()
  520. local svertical = player:get_look_vertical()
  521. if pos and dir and yaw then
  522. minetest.sound_play(shoot_sound, {player})
  523. pos.y = pos.y + 1.45
  524. if has_shell > 0 then
  525. local shl = minetest.add_entity(pos, shellEnt)
  526. shl:setvelocity({x=dir.x * -10, y=dir.y * -10, z=dir.z * -10})
  527. shl:setacceleration({x=dir.x * -5, y= -10, z=dir.z * -5})
  528. shl:set_rotation({x=0,y=yaw + math.pi,z=-svertical})
  529. shl:set_properties({
  530. textures = {shellTexture},
  531. visual = shellVisual,})
  532. end
  533. if smokeSize > 0 then
  534. minetest.add_particle({
  535. pos = pos,
  536. velocity = {x=(dir.x*3)+(math.random(-10,10)/10), y=(dir.y*3)+(math.random(-10,10)/10), z=(dir.z*3)+(math.random(-10,10)/10)} ,
  537. acceleration = {x=dir.x * -3, y=2, z=dir.z * -3},
  538. expirationtime = math.random(5,10)/10,
  539. size = smokeSize,
  540. collisiondetection = false,
  541. vertical = false,
  542. texture = "tnt_smoke.png",
  543. glow = 5,
  544. })
  545. end
  546. projectiles = projNum or 1
  547. for i=1,projectiles do
  548. rndacc = (100 - accuracy) or 0
  549. local spawnpos_x = pos.x + (math.random(-rndacc,rndacc)/100)
  550. local spawnpos_y = pos.y + (math.random(-rndacc,rndacc)/100)
  551. local spawnpos_z = pos.z + (math.random(-rndacc,rndacc)/100)
  552. local obj = minetest.add_entity({x=spawnpos_x,y=spawnpos_y,z=spawnpos_z}, projEnt)
  553. local ent = obj:get_luaentity()
  554. obj:set_properties(
  555. {textures = {texture},
  556. visual = visualType,
  557. collisionbox = {-size,-size,-size, size,size,size},
  558. glow = proj_glow,}
  559. )
  560. ent.owner = player:get_player_name()
  561. if obj then
  562. ent.damage = projDmg
  563. ent.crit = projCrit
  564. ent.critEffc = projCritEffc
  565. ent.OnCollision = ColResult
  566. ent.mobPen = mobPen
  567. ent.nodePen = nodePen
  568. ent.dps = dps
  569. ent.door_break = door_break
  570. ent.glass_break = glass_break
  571. ent.skill_value = skill_value
  572. ent.bullet_particles = bullet_particles
  573. ent.sparks = sparks
  574. ent.ignite = ignite
  575. ent.size = size
  576. ent.timer = 0 + (combined_velocity/2000)
  577. ent.wear = proj_wear
  578. obj:set_velocity({x=dir.x * combined_velocity, y=dir.y * combined_velocity, z=dir.z * combined_velocity})
  579. acc = ((( 100 - accuracy ) / 10) / skill_value ) or 0
  580. obj:set_acceleration({x=math.random(-acc,acc), y=math.random(-acc,acc)-gravity, z=math.random(-acc,acc)})
  581. obj:set_rotation({x=0,y=yaw + math.pi,z=-svertical})
  582. end end end
  583. end
  584. eject_shell = function(itemstack,player,rld_item,rld_time,rldsound,shell)
  585. itemstack:set_name(rld_item)
  586. local meta = player:get_meta()
  587. meta:set_float("rw_cooldown", rld_time)
  588. local gunMeta = itemstack:get_meta()
  589. local bulletStack = ItemStack({name = gunMeta:get_string("RW_ammo_name")})
  590. minetest.sound_play(rldsound, {player})
  591. local pos = player:get_pos()
  592. local dir = player:get_look_dir()
  593. local yaw = player:get_look_yaw()
  594. if pos and dir and yaw then
  595. pos.y = pos.y + 1.6
  596. local obj = minetest.add_entity(pos, "rangedweapons:empty_shell")
  597. if AmmoCaps and bulletStack ~= "" then
  598. AmmoCaps = bulletStack:get_definition().RW_ammo_capabilities
  599. local bullet_shell_visual = "wielditem"
  600. local bullet_shell_texture = "rangedweapons:shelldrop"
  601. bullet_shell_visual = AmmoCaps.shell_visual or "wielditem"
  602. bullet_shell_texture = AmmoCaps.shell_texture or "rangedweapons:shelldrop"
  603. obj:set_properties({textures = {bullet_shell_texture}})
  604. obj:set_properties({visual = bullet_shell_visual})
  605. end
  606. if obj then
  607. obj:set_velocity({x=dir.x*-10, y=dir.y*-10, z=dir.z*-10})
  608. obj:set_acceleration({x=dir.x*-5, y=-10, z=dir.z*-5})
  609. obj:set_yaw(yaw + math.pi)
  610. end end end
  611. ---------------------------------------------------
  612. dofile(modpath.."/settings.lua")
  613. dofile(modpath.."/cooldown_stuff.lua")
  614. dofile(modpath.."/skills.lua")
  615. dofile(modpath.."/misc.lua")
  616. dofile(modpath.."/bullet_knockback.lua")
  617. dofile(modpath.."/ammo.lua")
  618. dofile(modpath.."/crafting.lua")
  619. if rweapons_shurikens == "true" then
  620. dofile(modpath.."/shurikens.lua")
  621. end
  622. if rweapons_handguns == "true" then
  623. dofile(modpath.."/makarov.lua")
  624. dofile(modpath.."/luger.lua")
  625. dofile(modpath.."/beretta.lua")
  626. dofile(modpath.."/m1991.lua")
  627. dofile(modpath.."/glock17.lua")
  628. dofile(modpath.."/deagle.lua")
  629. end
  630. if rweapons_forceguns == "true" then
  631. dofile(modpath.."/forcegun.lua")
  632. end
  633. if rweapons_javelins == "true" then
  634. dofile(modpath.."/javelin.lua")
  635. end
  636. if rweapons_power_weapons == "true" then
  637. dofile(modpath.."/generator.lua")
  638. dofile(modpath.."/laser_blaster.lua")
  639. dofile(modpath.."/laser_rifle.lua")
  640. dofile(modpath.."/laser_shotgun.lua")
  641. end
  642. if rweapons_machine_pistols == "true" then
  643. dofile(modpath.."/tmp.lua")
  644. dofile(modpath.."/tec9.lua")
  645. dofile(modpath.."/uzi.lua")
  646. dofile(modpath.."/kriss_sv.lua")
  647. end
  648. if rweapons_shotguns == "true" then
  649. dofile(modpath.."/remington.lua")
  650. dofile(modpath.."/spas12.lua")
  651. dofile(modpath.."/benelli.lua")
  652. end
  653. if rweapons_auto_shotguns == "true" then
  654. dofile(modpath.."/jackhammer.lua")
  655. dofile(modpath.."/aa12.lua")
  656. end
  657. if rweapons_smgs == "true" then
  658. dofile(modpath.."/mp5.lua")
  659. dofile(modpath.."/ump.lua")
  660. dofile(modpath.."/mp40.lua")
  661. dofile(modpath.."/thompson.lua")
  662. end
  663. if rweapons_rifles == "true" then
  664. dofile(modpath.."/awp.lua")
  665. dofile(modpath.."/svd.lua")
  666. dofile(modpath.."/m200.lua")
  667. end
  668. if rweapons_heavy_machineguns == "true" then
  669. dofile(modpath.."/m60.lua")
  670. dofile(modpath.."/rpk.lua")
  671. dofile(modpath.."/minigun.lua")
  672. end
  673. if rweapons_revolvers == "true" then
  674. dofile(modpath.."/python.lua")
  675. dofile(modpath.."/taurus.lua")
  676. end
  677. if rweapons_assault_rifles == "true" then
  678. dofile(modpath.."/m16.lua")
  679. dofile(modpath.."/g36.lua")
  680. dofile(modpath.."/ak47.lua")
  681. dofile(modpath.."/scar.lua")
  682. end
  683. if rweapons_explosives == "true" then
  684. dofile(modpath.."/explosives.lua")
  685. dofile(modpath.."/m79.lua")
  686. dofile(modpath.."/milkor.lua")
  687. dofile(modpath.."/rpg.lua")
  688. dofile(modpath.."/hand_grenade.lua")
  689. end
  690. if rweapons_glass_breaking == "true" then
  691. dofile(modpath.."/glass_breaking.lua")
  692. end
  693. if not minetest.settings:get_bool("glass_breaking") then
  694. minetest.register_abm({
  695. nodenames = {"rangedweapons:broken_glass"},
  696. interval = 1,
  697. chance = 1,
  698. action = function(pos, node)
  699. if minetest.get_node(pos).name == "rangedweapons:broken_glass" then
  700. node.name = "default:glass"
  701. minetest.set_node(pos, node)
  702. end
  703. end
  704. })
  705. end
  706. local rangedweapons_empty_shell = {
  707. physical = false,
  708. timer = 0,
  709. visual = "wielditem",
  710. visual_size = {x=0.3, y=0.3},
  711. textures = {"rangedweapons:shelldrop"},
  712. lastpos= {},
  713. collisionbox = {0, 0, 0, 0, 0, 0},
  714. }
  715. rangedweapons_empty_shell.on_step = function(self, dtime, pos)
  716. self.timer = self.timer + dtime
  717. local pos = self.object:get_pos()
  718. local node = minetest.get_node(pos)
  719. if self.lastpos.y ~= nil then
  720. if minetest.registered_nodes[node.name]~= nil then
  721. if minetest.registered_nodes[node.name].walkable then
  722. local vel = self.object:get_velocity()
  723. local acc = self.object:get_acceleration()
  724. self.object:set_velocity({x=vel.x*-0.3, y=vel.y*-0.75, z=vel.z*-0.3})
  725. minetest.sound_play("rangedweapons_shellhit", {pos = self.lastpos, gain = 0.8})
  726. self.object:set_acceleration({x=acc.x, y=acc.y, z=acc.z})
  727. end end
  728. end
  729. if self.timer > 1.69 then
  730. minetest.sound_play("rangedweapons_bulletdrop", {pos = self.lastpos, gain = 0.8})
  731. self.object:remove()
  732. end
  733. self.lastpos= {x = pos.x, y = pos.y, z = pos.z}
  734. end
  735. minetest.register_entity("rangedweapons:empty_shell", rangedweapons_empty_shell )
  736. minetest.register_abm({
  737. nodenames = {"doors:hidden"},
  738. interval = 1,
  739. chance = 1,
  740. action = function(pos, node)
  741. pos.y = pos.y-1
  742. if minetest.get_node(pos).name == "air" then
  743. pos.y = pos.y+1
  744. node.name = "air"
  745. minetest.set_node(pos, node)
  746. end
  747. end
  748. })
  749. minetest.register_on_joinplayer(function(player)
  750. gunammo =
  751. player:hud_add({
  752. hud_elem_type = "text",
  753. name = "gunammo",
  754. text = "",
  755. number = 0xFFFFFF,
  756. scale = {x = 100, y = 20},
  757. position = {x = 0.7, y = 0.1},
  758. offset = {x = 30, y = 100},
  759. alignment = {x = 0, y = -1}
  760. })
  761. gunimg =
  762. player:hud_add({
  763. hud_elem_type = "image",
  764. text = "rangedweapons_empty_icon.png",
  765. scale = {x = 2, y = 2},
  766. position = {x = 0.7, y = 0.065},
  767. offset = {x = 30, y = 100},
  768. alignment = {x = 0, y = -1}
  769. })
  770. ammoimg =
  771. player:hud_add({
  772. hud_elem_type = "image",
  773. text = "rangedweapons_empty_icon.png",
  774. scale = {x = 1.5, y = 1.5},
  775. position = {x = 0.725, y = 0.1},
  776. offset = {x = 30, y = 100},
  777. alignment = {x = 0, y = -1}
  778. })
  779. hit =
  780. player:hud_add({
  781. hud_elem_type = "image",
  782. text = "rangedweapons_empty_icon.png",
  783. scale = {x = 2, y = 2},
  784. position = {x = 0.5, y = 0.5},
  785. offset = {x = 0, y = 0},
  786. alignment = {x = 0, y = 0}
  787. })
  788. scope_hud =
  789. player:hud_add({
  790. hud_elem_type = "image",
  791. position = { x=0.5, y=0.5 },
  792. scale = { x=-100, y=-100 },
  793. text = "rangedweapons_empty_icon.png",
  794. })
  795. end)
  796. local timer = 0
  797. minetest.register_globalstep(function(dtime, player)
  798. timer = timer + dtime;
  799. if timer >= 1.0 then
  800. for _, player in pairs(minetest.get_connected_players()) do
  801. player:hud_change(hit, "text", "rangedweapons_empty_icon.png")
  802. timer = 0
  803. end
  804. end
  805. end)