From ebee85db7e176392e8dcd67f7db8958aec0d2926 Mon Sep 17 00:00:00 2001 From: kno10 Date: Sat, 7 Sep 2024 14:58:12 +0200 Subject: [PATCH] Fix incorrect usages of math.random (#4621) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit random() does not support float arguments Reviewed-on: https://git.minetest.land/VoxeLibre/VoxeLibre/pulls/4621 Reviewed-by: Mikita Wiśniewski Co-authored-by: kno10 Co-committed-by: kno10 --- mods/ENTITIES/mcl_mobs/movement.lua | 12 ++++++------ mods/ENTITIES/mobs_mc/blaze.lua | 12 ++++++------ mods/ENTITIES/mobs_mc/cod.lua | 2 +- mods/ENTITIES/mobs_mc/dolphin.lua | 6 +++--- mods/ENTITIES/mobs_mc/slime+magma_cube.lua | 2 +- mods/ENVIRONMENT/lightning/init.lua | 2 +- mods/ITEMS/mcl_cherry_blossom/growth.lua | 12 ++++++------ mods/ITEMS/mcl_mobspawners/init.lua | 3 +-- mods/PLAYER/mcl_playerplus/init.lua | 2 +- 9 files changed, 26 insertions(+), 27 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/movement.lua b/mods/ENTITIES/mcl_mobs/movement.lua index ff9a38f56..dc4dcd272 100644 --- a/mods/ENTITIES/mcl_mobs/movement.lua +++ b/mods/ENTITIES/mcl_mobs/movement.lua @@ -362,7 +362,7 @@ function mob_class:env_danger_movement_checks(player_in_active_range) self.state = "stand" self:set_animation( "stand") end - yaw = yaw + math.random(-0.5, 0.5) + yaw = yaw + math.random() - 0.5 yaw = self:set_yaw( yaw, 8) return end @@ -788,9 +788,9 @@ function mob_class:flop() if self.object:get_velocity().y < 0.1 then self:mob_sound("flop") self.object:set_velocity({ - x = math.random(-FLOP_HOR_SPEED, FLOP_HOR_SPEED), + x = (math.random()-0.5) * 2 * FLOP_HOR_SPEED, y = FLOP_HEIGHT, - z = math.random(-FLOP_HOR_SPEED, FLOP_HOR_SPEED), + z = (math.random()-0.5) * 2 * FLOP_HOR_SPEED, }) end end @@ -920,7 +920,7 @@ function mob_class:do_states_walk() -- Randomly turn if math.random(1, 100) <= 30 then - yaw = yaw + math.random(-0.5, 0.5) + yaw = yaw + math.random() - 0.5 yaw = self:set_yaw( yaw, 8) end end @@ -929,7 +929,7 @@ function mob_class:do_states_walk() -- otherwise randomly turn elseif math.random(1, 100) <= 30 then - yaw = yaw + math.random(-0.5, 0.5) + yaw = yaw + math.random() - 0.5 yaw = self:set_yaw( yaw, 8) end @@ -989,7 +989,7 @@ function mob_class:do_states_stand(player_in_active_range) if lp.x > s.x then yaw = yaw +math.pi end else - yaw = yaw + math.random(-0.5, 0.5) + yaw = yaw + math.random() - 0.5 end yaw = self:set_yaw( yaw, 8) diff --git a/mods/ENTITIES/mobs_mc/blaze.lua b/mods/ENTITIES/mobs_mc/blaze.lua index bbc47df94..f5dd40470 100644 --- a/mods/ENTITIES/mobs_mc/blaze.lua +++ b/mods/ENTITIES/mobs_mc/blaze.lua @@ -95,8 +95,8 @@ mcl_mobs.register_mob("mobs_mc:blaze", { end local pos = self.object:get_pos() minetest.add_particle({ - pos = {x=pos.x+math.random(-0.7,0.7)*math.random()/2,y=pos.y+math.random(0.7,1.2),z=pos.z+math.random(-0.7,0.7)*math.random()/2}, - velocity = {x=0, y=math.random(1,1), z=0}, + pos = {x=pos.x+(math.random()*0.7-0.35)*math.random(),y=pos.y+0.7+math.random()*0.5,z=pos.z+(math.random()*0.7-0.35)*math.random()}, + velocity = {x=0, y=1, z=0}, expirationtime = math.random(), size = math.random(1, 4), collisiondetection = true, @@ -110,8 +110,8 @@ mcl_mobs.register_mob("mobs_mc:blaze", { }, }) minetest.add_particle({ - pos = {x=pos.x+math.random(-0.7,0.7)*math.random()/2,y=pos.y+math.random(0.7,1.2),z=pos.z+math.random(-0.7,0.7)*math.random()/2}, - velocity = {x=0, y=math.random(1,1), z=0}, + pos = {x=pos.x+(math.random()*0.7-0.35)*math.random(),y=pos.y+0.7+math.random()*0.5,z=pos.z+(math.random()*0.7-0.35)*math.random()}, + velocity = {x=0, y=1, z=0}, expirationtime = math.random(), size = math.random(1, 4), collisiondetection = true, @@ -125,8 +125,8 @@ mcl_mobs.register_mob("mobs_mc:blaze", { }, }) minetest.add_particle({ - pos = {x=pos.x+math.random(-0.7,0.7)*math.random()/2,y=pos.y+math.random(0.7,1.2),z=pos.z+math.random(-0.7,0.7)*math.random()/2}, - velocity = {x=0, y=math.random(1,1), z=0}, + pos = {x=pos.x+(math.random()*0.7-0.35)*math.random(),y=pos.y+0.7+math.random()*0.5,z=pos.z+(math.random()*0.7-0.35)*math.random()}, + velocity = {x=0, y=1, z=0}, expirationtime = math.random(), size = math.random(1, 4), collisiondetection = true, diff --git a/mods/ENTITIES/mobs_mc/cod.lua b/mods/ENTITIES/mobs_mc/cod.lua index a2aa2eadf..fba769694 100644 --- a/mods/ENTITIES/mobs_mc/cod.lua +++ b/mods/ENTITIES/mobs_mc/cod.lua @@ -84,7 +84,7 @@ local cod = { self.object:set_bone_position("body", vector.new(0,1,0), vector.new(degrees(dir_to_pitch(self.object:get_velocity())) * -1 + 90,0,0)) if minetest.get_item_group(self.standing_in, "water") ~= 0 then if self.object:get_velocity().y < 5 then - self.object:add_velocity({ x = 0 , y = math.random(-.007, .007), z = 0 }) + self.object:add_velocity({ x = 0 , y = math.random()*.014-.007, z = 0 }) end end --]] diff --git a/mods/ENTITIES/mobs_mc/dolphin.lua b/mods/ENTITIES/mobs_mc/dolphin.lua index 0db7647b9..45753ff7f 100644 --- a/mods/ENTITIES/mobs_mc/dolphin.lua +++ b/mods/ENTITIES/mobs_mc/dolphin.lua @@ -81,16 +81,16 @@ local dolphin = { reach = 2, damage = 2.5, attack_type = "dogfight", + --[[ this is supposed to make them jump out the water but doesn't appear to work very well do_custom = function(self,dtime) - --[[ this is supposed to make them jump out the water but doesn't appear to work very well self.object:set_bone_position("body", vector.new(0,1,0), vector.new(degrees(dir_to_pitch(self.object:get_velocity())) * -1 + 90,0,0)) if minetest.get_item_group(self.standing_in, "water") ~= 0 then if self.object:get_velocity().y < 5 then - self.object:add_velocity({ x = 0 , y = math.random(-.007, .007), z = 0 }) + self.object:add_velocity({ x = 0 , y = math.random()*.014-.007, z = 0 }) end end - --]] end, + --]] } mcl_mobs.register_mob("mobs_mc:dolphin", dolphin) diff --git a/mods/ENTITIES/mobs_mc/slime+magma_cube.lua b/mods/ENTITIES/mobs_mc/slime+magma_cube.lua index d56b6e2a2..8a4c8919f 100644 --- a/mods/ENTITIES/mobs_mc/slime+magma_cube.lua +++ b/mods/ENTITIES/mobs_mc/slime+magma_cube.lua @@ -122,7 +122,7 @@ local spawn_children_on_die = function(child_mob, spawn_distance, eject_speed) end local mndef = minetest.registered_nodes[minetest.get_node(pos).name] local mother_stuck = mndef and mndef.walkable - local angle = math.random(0, math.pi*2) + local angle = math.random() * math.pi * 2 local children = {} local spawn_count = math.random(2, 4) for i = 1, spawn_count do diff --git a/mods/ENVIRONMENT/lightning/init.lua b/mods/ENVIRONMENT/lightning/init.lua index 41d6f458c..079c9cb38 100644 --- a/mods/ENVIRONMENT/lightning/init.lua +++ b/mods/ENVIRONMENT/lightning/init.lua @@ -177,7 +177,7 @@ function lightning.strike_func(pos, pos2, objects) add_entity(pos2, "mobs_mc:skeleton_horse") local angle, posadd - angle = math.random(0, math.pi*2) + angle = math.random() * math.pi * 2 for i=1,3 do posadd = { x=math.cos(angle),y=0,z=math.sin(angle) } posadd = vector.normalize(posadd) diff --git a/mods/ITEMS/mcl_cherry_blossom/growth.lua b/mods/ITEMS/mcl_cherry_blossom/growth.lua index bca926539..c7bbf3044 100644 --- a/mods/ITEMS/mcl_cherry_blossom/growth.lua +++ b/mods/ITEMS/mcl_cherry_blossom/growth.lua @@ -30,7 +30,7 @@ minetest.register_abm({ local cherry_particle = { velocity = vector.zero(), acceleration = vector.new(0,-1,0), - size = math.random(1.3,2.5), + size = 1.3 + math.random() * 1.2, texture = "mcl_cherry_blossom_particle_" .. math.random(1, 12) .. ".png", animation = { type = "vertical_frames", @@ -45,8 +45,8 @@ local cherry_particle = { local wind_direction -- vector local time_changed -- 0 - afternoon; 1 - evening; 2 - morning local function change_wind_direction() - local east_west = math.random(-0.5,0.5) - local north_south = math.random(-0.5,0.5) + local east_west = math.random() - 0.5 + local north_south = math.random() - 0.5 wind_direction = vector.new(east_west, 0, north_south) end change_wind_direction() @@ -57,10 +57,10 @@ minetest.register_abm({ interval = 5, chance = 10, action = function(pos, node) - minetest.after(math.random(0.1,1.5),function() + minetest.after(0.1 + math.random() * 1.4,function() local pt = table.copy(cherry_particle) - pt.pos = vector.offset(pos,math.random(-0.5,0.5),-0.51,math.random(-0.5,0.5)) - pt.expirationtime = math.random(1.2,4.5) + pt.pos = vector.offset(pos,math.random()-0.5,-0.51,math.random()-0.5) + pt.expirationtime = 1.2 + math.random() * 3.3 pt.texture = "mcl_cherry_blossom_particle_" .. math.random(1, 12) .. ".png" local time = minetest.get_timeofday() if time_changed ~= 0 and time > 0.6 and time < 0.605 then diff --git a/mods/ITEMS/mcl_mobspawners/init.lua b/mods/ITEMS/mcl_mobspawners/init.lua index 83c03804e..8b2c73d8c 100644 --- a/mods/ITEMS/mcl_mobspawners/init.lua +++ b/mods/ITEMS/mcl_mobspawners/init.lua @@ -259,8 +259,7 @@ local function spawn_mobs(pos, elapsed) end -- Spawn attempt done. Next spawn attempt much later - timer:start(math.random(10, 39.95)) - + timer:start(math.random() * 29.95 + 10) end -- The mob spawner node. diff --git a/mods/PLAYER/mcl_playerplus/init.lua b/mods/PLAYER/mcl_playerplus/init.lua index 709d1d0fb..e090e6df7 100644 --- a/mods/PLAYER/mcl_playerplus/init.lua +++ b/mods/PLAYER/mcl_playerplus/init.lua @@ -280,7 +280,7 @@ minetest.register_globalstep(function(dtime) pos = fly_pos, velocity = vector.zero(), acceleration = vector.zero(), - expirationtime = math.random(0.3, 0.5), + expirationtime = 0.3 + math.random() * 0.2, size = math.random(1, 2), collisiondetection = false, vertical = false,