Post your code!

Sokomine
Member
Posts: 4107
Joined: Sun Sep 09, 2012 17:31
GitHub: Sokomine

Re: Post your code!

by Sokomine » Post

sorcerykid wrote: A player on my sever recently notified me that the screwdriver can be used to rotate chests sideways. So I wrote a command-line Lua script to locate all such incorrectly oriented chests, furnaces, bookshelves, and vessels shelves within the world.
Don't change these things! There's got to be enough intresting stuff for lga's intresting museums :-)
A list of my mods can be found here.

User avatar
sorcerykid
Member
Posts: 1421
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: Post your code!

by sorcerykid » Post

I developed a raycast function for efficiently computing line of sight between two points in 3d space. It is loosely based on the nearest neighbor line-drawing algorithm, and thus focuses on speed rather than accuracy.

raytrace_simple( pos1, pos2, on_plot )
  • pos1 - the starting position of the ray as a vector
  • pos2 - the ending position of the ray as a vector
  • on_plot - the callback function for traversing the ray
The callback function will receive both the 1-based index and position of each successive node along the ray from pos1 through pos2. When an obstacle is encountered, the function should return false or nil to abort the raytrace.

Code: Select all

local function raytrace_simple( pos1, pos2, on_plot )
        local p1 = vector.round( pos1 )
        local p2 = vector.round( pos2 )
        local ax = abs( p2.x - p1.x )
        local ay = abs( p2.y - p1.y )
        local az = abs( p2.z - p1.z )
        local step = max( ax, ay, az )
        local dx = ( p2.x - p1.x ) / step;
        local dy = ( p2.y - p1.y ) / step;
        local dz = ( p2.z - p1.z ) / step;
        local x = p1.x
        local y = p1.y
        local z = p1.z

        for i = 1, step + 1 do
                if not on_plot( i, { x = floor( x + 0.5 ), y = floor( y + 0.5 ), z = floor( z + 0.5 ) } ) then
                        return false
                end

                x = x + dx
                y = y + dy
                z = z + dz
        end

        return true
end
Using the function above, a more flexible alternative to minetest.line_of_sight() can be devised:

Code: Select all

line_of_sight = function ( source_pos, target_pos )
        local unknown_ndef = { groups = { }, walkable = false }

        return raytrace_simple( source_pos, target_pos, function ( idx, pos )
                local node = minetest.get_node( pos )
                local ndef = core.registered_nodes[ node.name ] or unknown_ndef     -- account for unknown nodes
                local is_airlike = not ndef.walkable

                return is_airlike
        end )
end
Notice that the return value of raytrace_simple() reflects whether the callback succeeded or failed while traversing the ray.

User avatar
sorcerykid
Member
Posts: 1421
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: Post your code!

by sorcerykid » Post

This is a very simple Auth Redux ruleset for limiting new accounts to certain times of day, unless the server operator is online. Of course the range of hours can be changed by editing the pattern /8^19:?/t accordingly.

Code: Select all

try "Our server is only open to new players between 8:00-19:59 or when $owner is online"
fail all
if $is_new eq $true
unless $clock is /8^19:?/t
unless $owner in $users_list
continue

User avatar
Linuxdirk
Member
Posts: 2587
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: Post your code!

by Linuxdirk » Post

A function to get a configuration value as string from either ./minetest.conf, a world-specific ./worlds/worldname/modname.conf or a given default value.

It is also possible to mark values as not changeable via configuration options (which could be done simply by not using the function then, but it’s all about options, right?).

Simply add this to your configuration Lua file or add it to your mod’s global table’s function section (fits within the 80 characters rule, of course).

Code: Select all

local get_config_option = function (key_name, default_value, changeable)
    local mod = minetest.get_current_modname()
    local config_option = mod..'_'..key_name
    local value = default_value

    if changeable ~= false then
        local wc = Settings(minetest.get_worldpath()..DIR_DELIM..mod..'.conf')
        local global_setting = minetest.settings:get(config_option)
        local world_setting = wc:get(config_option)
        value = world_setting or global_setting or default_value or ''
    end

    return value
end
And use it in your code like this:

Code: Select all

local my_option = get_config_option('my_option', 'foo')
local my_other_option = get_config_option('my_other_option', 'bar')
local and_so_on = get_config_option('and_so_on', 'baz', false)
This automatically allows users to configure things to their liking using the following parameters (assuming mymod is your mod’s name).

Code: Select all

mymod_my_option = foo
mymod_my_other_option = bar
If you later decide to have the third option also being configurable, simply remove the false parameter and players can set mymod_and_so_on, too.

User avatar
sorcerykid
Member
Posts: 1421
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: Post your code!

by sorcerykid » Post

Here is a new Minetest API function to make including libraries and submodules easier at long last.

Before:
local helpers = dofile( minetest.get_modpath( "othermod" ) .. "/helpers.lua" )

After:
local helpers = minetest.include( "/othermod/helpers.lua" )

Before:
loadfile( minetest.get_modpath( "thismod" ) .. "/gui.lua" )( config, player_list )

After:
minetest.include( "gui.lua", config, player_list )

The minetest.include() function includes a file. It accepts a file specifier, which may be either a path relative to the currently loading mod or else relative to another mod with a leading slash. Any additional parameters are passed directly to the loaded script, which may be retrieved via local var1, var2, var3 = ... at the head of the included file.

Code: Select all

function minetest.include( filespec, ... )
        local mod_name, file_name = string.match( filespec, "^[/\](.-)[/\](.+)" )
        if not mod_name then
                mod_name = minetest.get_current_modname( )
                file_name = filespec
        end
        return loadfile( minetest.get_modpath( mod_name ) .. "/" .. file_name )( ... )
end

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest