From dd4ebe5aea4e5d8d83025460f5c0aac48b55db9c Mon Sep 17 00:00:00 2001 From: danielmartin0 Date: Thu, 3 Oct 2024 23:23:22 +0100 Subject: [PATCH] utils/math pt2 --- control.lua | 1 + maps/pirates/math.lua | 21 +++++++------- utils/core.lua | 7 ----- utils/functions/loot_raffle.lua | 3 +- utils/gui/poll.lua | 4 +-- utils/math/geometry.lua | 5 ++++ utils/table.lua | 50 ++------------------------------- utils/utils.lua | 50 --------------------------------- 8 files changed, 22 insertions(+), 119 deletions(-) diff --git a/control.lua b/control.lua index a9487049..791d922d 100644 --- a/control.lua +++ b/control.lua @@ -8,6 +8,7 @@ require 'utils.server' require 'utils.server_commands' require 'utils.command_handler' require 'utils.utils' +require 'utils.math.math' require 'utils.pause_game' require 'utils.table' require 'utils.whisper_notice' diff --git a/maps/pirates/math.lua b/maps/pirates/math.lua index 7d8cb9b0..d618bb65 100644 --- a/maps/pirates/math.lua +++ b/maps/pirates/math.lua @@ -1,5 +1,7 @@ -- This file is part of thesixthroc's Pirate Ship softmod, licensed under GPLv3 and stored at https://github.com/ComfyFactory/ComfyFactorio and https://github.com/danielmartin0/ComfyFactorio-Pirates. +local Geometry = require 'utils.math.geometry' + local Public = {} Public.random = math.random @@ -164,18 +166,15 @@ function Public.shuffle(tbl) return tbl end -local function is_closer(pos1, pos2, pos) - return ((pos1.x - pos.x) ^ 2 + (pos1.y - pos.y) ^ 2) < ((pos2.x - pos.x) ^ 2 + (pos2.y - pos.y) ^ 2) -end function Public.shuffle_distancebiased(tbl, position) - local size = #tbl - for i = size, 1, -1 do - local rand = Public.random(i) - if is_closer(tbl[i].position, tbl[rand].position, position) and i > rand then - tbl[i], tbl[rand] = tbl[rand], tbl[i] - end - end - return tbl + local size = #tbl + for i = size, 1, -1 do + local rand = Public.random(i) + if Geometry.is_closer(position, tbl[i].position, tbl[rand].position) and i > rand then + tbl[i], tbl[rand] = tbl[rand], tbl[i] + end + end + return tbl end Public.points_in_m20t20_squared_sorted_by_distance_to_origin = { diff --git a/utils/core.lua b/utils/core.lua index 8c15db24..cc4297c1 100644 --- a/utils/core.lua +++ b/utils/core.lua @@ -23,13 +23,6 @@ local ticks_to_hours = 1 / hours_to_ticks -- local vars local Public = {} ---- Measures distance between pos1 and pos2 -function Public.distance(pos1, pos2) - local dx = pos2.x - pos1.x - local dy = pos2.y - pos1.y - return sqrt(dx * dx + dy * dy) -end - --- Takes msg and prints it to all players except provided player -- @param msg table if locale is used -- @param player the player not to send the message to diff --git a/utils/functions/loot_raffle.lua b/utils/functions/loot_raffle.lua index 30cc4afe..81d210c6 100644 --- a/utils/functions/loot_raffle.lua +++ b/utils/functions/loot_raffle.lua @@ -1,3 +1,4 @@ +local Math = require 'utils.math.math' --[[ roll(budget, max_slots, blacklist) returns a table with item-stacks budget - the total value of the item stacks combined @@ -535,7 +536,7 @@ function Public.get_tech_blacklist(tier) local blacklist = {} local size_of_tech_tier_list = #tech_tier_list - tier = math.clamp(tier, 0, 1) + tier = Math.clamp(tier, 0, 1) local min_index = math_floor(size_of_tech_tier_list * tier) + 1 for i = size_of_tech_tier_list, min_index, -1 do blacklist[tech_tier_list[i]] = true diff --git a/utils/gui/poll.lua b/utils/gui/poll.lua index 02ac4794..87227e00 100644 --- a/utils/gui/poll.lua +++ b/utils/gui/poll.lua @@ -5,7 +5,7 @@ local Server = require 'utils.server' local session = require 'utils.datastore.session_data' local Config = require 'utils.gui.config' local SpamProtection = require 'utils.spam_protection' - +local Math = require 'utils.math.math' local Public = {} local insert = table.insert @@ -274,7 +274,7 @@ local function update_poll_viewer(data) if #polls == 0 then poll_index = 0 else - poll_index = math.clamp(poll_index, 1, #polls) + poll_index = Math.clamp(poll_index, 1, #polls) end data.poll_index = poll_index diff --git a/utils/math/geometry.lua b/utils/math/geometry.lua index 4141df48..8790cb39 100644 --- a/utils/math/geometry.lua +++ b/utils/math/geometry.lua @@ -67,4 +67,9 @@ function Public.vector_norm(vec) return { x = vec_copy.x, y = vec_copy.y } end +--- Returns true if position is closer to pos1 than to pos2 +function Public.is_closer(position, pos1, pos2) + return Public.vector_distance(pos1, position) < Public.vector_distance(pos2, position) +end + return Public diff --git a/utils/table.lua b/utils/table.lua index e3bdda55..76fd9d48 100644 --- a/utils/table.lua +++ b/utils/table.lua @@ -1,7 +1,7 @@ ---@diagnostic disable: undefined-field --luacheck: globals table local Stats = require 'utils.math.stats' -local Utils = require 'utils.utils' +local Geometry = require 'utils.math.geometry' local random = math.random local floor = math.floor local remove = table.remove @@ -169,52 +169,6 @@ function table.get_random_weighted(weighted_table, item_index, weight_index) end end - ----Returns one random value based on the supplied weights. Form {[a] = A, [b] = B, ...} and {[a] = a_weight, [b] = b_weight, ...} or just {a,b,c,...} and {1,2,1,...}. ----Both tables have to be the same size. ----@param values table ----@param weights table ----@return any -function table.get_random_weighted_t(values, weights) - local total_weight = 0 - for k, w in pairs(weights) do - assert(values[k], 'Weighted_raffle: A weight is missing value!') - if w > 0 then - -- negative weights treated as zero - total_weight = total_weight + w - end - end - assert(total_weight > 0, 'Total weight of weighted_raffle needs to be bigger than zero!') - - local cumulative_probability = 0 - local rng = random() - for k, v in pairs(values) do - assert(weights[k], 'Weighted_raffle: A value is missing weight!') - cumulative_probability = cumulative_probability + (weights[k] / total_weight) - if rng <= cumulative_probability then - return v - end - end -end - - ----Returns a table with % chance values for each item of a weighted_table ----@param weighted_table table of tables with items and their weights ----@param weight_index number of the index of the weights, defaults to 2 ----@return table -function table.get_random_weighted_chances(weighted_table, weight_index) - local total_weight = 0 - weight_index = weight_index or 2 - for _, v in pairs(weighted_table) do - total_weight = total_weight + v[weight_index] - end - local chance_table = {} - for k, v in pairs(weighted_table) do - chance_table[k] = v[weight_index] / total_weight - end - return chance_table -end - ---Creates a fisher-yates shuffle of a sequential number-indexed table. ---Because this uses math.random, it cannot be used outside of events if no rng is supplied ---from: http://www.sdknews.com/cross-platform/corona/tutorial-how-to-shuffle-table-items @@ -248,7 +202,7 @@ function table.shuffle_by_distance(tbl, position) local size = #tbl for i = size, 1, -1 do local rand = random(size) - if Utils.is_closer(pos(tbl[i]), pos(tbl[rand]), position) and i > rand then + if Geometry.is_closer(pos(tbl[i]), pos(tbl[rand]), position) and i > rand then tbl[i], tbl[rand] = tbl[rand], tbl[i] end end diff --git a/utils/utils.lua b/utils/utils.lua index 40e8426d..db192217 100644 --- a/utils/utils.lua +++ b/utils/utils.lua @@ -1,55 +1,5 @@ local Module = {} ----Returns cartesian distance between pos1 and pos2 ----@param pos1 MapPosition|{x:double,y:double} ----@param pos2 MapPosition|{x:double,y:double} ----@return number -function Module.distance(pos1, pos2) - local dx = pos2.x - pos1.x - local dy = pos2.y - pos1.y - return math.sqrt(dx * dx + dy * dy) -end - ----Returns true if position is closer to pos1 than to pos2 ----@param pos1 MapPosition|{x:double,y:double} ----@param pos2 MapPosition|{x:double,y:double} ----@param position MapPosition|{x:double,y:double} ----@return boolean -function Module.is_closer(position, pos1, pos2) - return Module.distance(pos1, position) < Module.distance(pos2, position) -end - ----Returns true if the position is inside the area ----@param position MapPosition|{x:double,y:double}|nil ----@param area BoundingBox|{left_top:MapPosition, right_bottom:MapPosition} ----@return boolean -function Module.inside(position, area) - if not position then - return false - end - - local lt = area.left_top - local rb = area.right_bottom - - return position.x >= lt.x and position.y >= lt.y and position.x <= rb.x and position.y <= rb.y -end - ----rounds number (num) to certain number of decimal places (idp) -function math.round(num, idp) - local mult = 10 ^ (idp or 0) - return math.floor(num * mult + 0.5) / mult -end - -function math.clamp(num, min, max) - if num < min then - return min - elseif num > max then - return max - else - return num - end -end - function Module.print_except(msg, player) for _, p in pairs(game.players) do if p.connected and p ~= player then