Replacement of Predetermined Particles

  • billion57
    2nd Aug 2013 Member 0 Permalink

    Can somebody write a script that takes particles of a certain type (SAND, PLNT, etc.) and converts them to a random particle-- from a weighted list: certain chances for certain particles.

    In Lua, of course.

     

    Here's how I would code it in Python, if it helps (what I don't understand is in braces):

     

    def randreplace(part,weightdict):

      for z in {particles}:

        if {z.type}==part:

          {z.type}==weightedrandom(dict) #A weighted random choice algorithm

     

    That's it, I think.

    {particles}: Going through all the particles. For simplicity, I assume it's a list.

    {z.type}: I assume particles are classes, and type is the element- sand, plnt... Thus, changing z.type would change the particle.

     

    Could someone do this (and tell me how I would run it), and explain to me the things in braces?

     

    (Also, I love how I make titles sound like scholarly articles)

    Edited once by billion57. Last: 2nd Aug 2013
  • boxmein
    7th Aug 2013 Former Staff 0 Permalink
    function magic (part, choices)
    if type(part) == "number" then part = tpt.element(part) end
    tpt.start_getPartIndex()
    while tpt.next_getPartIndex() do
    i = tpt.getPartIndex()
    if sim.partProperty(i, "type") == part then
    -- TODO improve randomness
    sim.partChangeType(i, choices[math.random(#choices)])
    end
    end
    end
    -- a script that takes particles of a certain type (SAND, PLNT, etc.) and
    -- converts them to a random particle
    -- lolboxes
    Edited 5 times by boxmein. Last: 7th Aug 2013
  • billion57
    8th Aug 2013 Member 0 Permalink

    I thought nobody would respond.

     

    I'll try this out, as soon as I figure out how to.

  • boxmein
    8th Aug 2013 Former Staff 0 Permalink
    @billion57 (View Post)
    Copy this into a text document and save it as
    magic.txt
    next to the Powder Toy program, then just enter
    dofile("magic.txt")
    into the console. After that you can invoke the script by writing
    magic("dust", {"vibr", "elec", "phot"})
    . Then dust will be replaced by either VIBR, ELEC or PHOT.
  • billion57
    9th Aug 2013 Member 0 Permalink

    So it takes strings, and Lua lists are in {}. Thanks!

  • boxmein
    10th Aug 2013 Former Staff 0 Permalink
    @billion57 (View Post)
    Lua has a weird thing called tables, which are a merger of lists and key/value arrays.
    http://www.lua.org/pil/2.5.html
    Edited 2 times by boxmein. Last: 10th Aug 2013
  • FeynmanLogomaker
    15th Aug 2013 Member 0 Permalink

    @boxmein:

    Instead of using a table for 'choices', couldn't you do this? 

     

    function magic (part, ...)
    choices = []

    for i,v in ipairs(arg) do

    choices[i] = tostring(v)

    end

    --etc

    end

     

    That way, instead of doing magic(dust, {"stuff", "more stuff", etc}), they could just do magic(dust, stuff, more stuff, etc).

     

    EDIT: What on earth is happening to my code boxes? Just ignore them for now.

    Edited 7 times by FeynmanLogomaker. Last: 19th Aug 2013
  • mniip
    15th Aug 2013 Developer 0 Permalink
    arg is derpecated
  • FeynmanLogomaker
    19th Aug 2013 Member 0 Permalink

    @mniip: Why?