Recent Posts

Pages: 1 ... 8 9 [10]
91
Your scripts / Fractals !
« Last post by PhoenixII54 on September 15, 2017, 11:10:58 am »
No, you didn't misread, what i'm about to give you is a series of scripts normally used in mathematics.
They don't desserve any porpose other than having fun with the engine, so enjoy !

1. the Mandelbrot set
Code: Lua
  1. --[[
  2. A useless-thus-indispensable Mandelbrot set renderer
  3.  
  4. How it works :
  5. the Mandelbrod set consists of a 2d grid of complex numbers,
  6. which are an extension of real numbers and are written in, the form :
  7. a+b*i, where a and b are two real numbers, and i is the square root of -1,
  8. also called the imaginary nuimber.
  9.  
  10. For each point of the grid, let(s call it c, we gererate a new complex, number called z by applying the following formula :
  11. initial z value : 0
  12. new z = (z to the power of <whatever you want>) + c
  13. then we compare the new value to (2 to the power of <the same exponent as before>
  14. then, we continue in one of the wollowing cases :
  15.    if z is lower than the powered 2, then we do the above steps again, unless we reached the maximum number if iterations
  16.    if, however, we are equal or higher than the powered 2,
  17.    then we stop iterating since the next calculated numbers wil keep growing forever, namely, we escaped the 2-range stability area
  18.  
  19. Anyway, once we have finished calculatins the final value for a given number, we color it according to it's behaviour :
  20. -if we escaped, then we color the corresponding point using the number of cleared iterations to index the color from a palette
  21. taking the mod from table size
  22. -else, we color it pitch black
  23.  
  24. this process goes on and on until all visible points are rendered.
  25.  
  26. For more information, feel free to check on the Wiki or any math article
  27.  
  28. How to use :
  29. 1. create a map of the size of your choice
  30. 2. copy/paste this whole code in the map script.
  31. 3. setup the config table with your own parameters
  32. 4. Run the map !
  33. --]]
  34.  
  35. local map = ...
  36. local game = map:get_game()
  37.  
  38.  
  39. -- the configuration table
  40. local config={
  41.   -- Size of the rendering area in game pixels.
  42.   -- Must be lower than the map size to render properly
  43.   grid={
  44.     width=401,
  45.     height=401
  46.   },
  47.   -- the maximum number of iterations for each point.
  48.   maxIterations=1000,
  49.   -- the calculation power. Defaulr : 2
  50.   powerExponent=2,
  51.   -- the starting complex numbers boundaries
  52.   numberBoundaries={
  53.     x={
  54.       min=-2,
  55.       max=2,
  56.     },
  57.     y={
  58.       min=-2,
  59.       max=2,
  60.     },
  61.   },
  62.   step={},
  63. }
  64.  
  65. local start_time
  66. -- returns the module of any complex number, aka the length of the vector represented by this number
  67. local function get_module(c)
  68.   return c.real*c.real+c.imag*c.imag
  69. end
  70.  
  71. --calculates z^N with z=real+i*imag and N>0
  72. local function complex_power(c, exponent)
  73.   local squaredModule=get_module(c)
  74.   local newModule=math.pow(squaredModule,exponent/2)
  75.   local argument=math.atan2(c.imag,c.real)
  76.   return {
  77.     real=math.cos(exponent*argument)*newModule,
  78.     imag=math.sin(exponent*argument)*newModule,
  79.   }
  80. end
  81.  
  82. local currentNumber={
  83.   real=config.numberBoundaries.x.min,
  84.   imag=config.numberBoundaries.y.max,
  85. }
  86.  
  87. local currentPoint={
  88.   x=0,
  89.   y=0,
  90. }
  91. -- a 1*1 surface used as a pixel, to compensate the lack of proper API pixel manipulatinf functions
  92. local pixel=sol.surface.create(1,1)
  93.  
  94. local fractalRender=sol.surface.create(config.grid.width,config.grid.height)
  95. -- the actual color palette. Feel free to modify it with your own colors (any size will work)
  96. local color_palette={
  97.   {255,255,0},
  98.   {255,223,31},
  99.   {255,191,63},
  100.   {255,159,95},
  101.   {255,127,127},
  102.   {255,95,159},
  103.   {255,63,191},
  104.   {255,31,223},
  105.   {223,0,255},
  106.   {255,31,255},
  107.   {191,63,255},
  108.   {159,95,255},
  109.   {127,127,255},
  110.   {95,159,255},
  111.   {63,191,255},
  112.   {31,223,255},
  113.   {0,255,255},
  114.   {31,255,223},
  115.   {63,255,191},
  116.   {95,255,159},
  117.   {127,255,127},
  118.   {159,255,95},
  119.   {191,255,63},
  120.   {223,255,31},
  121. }
  122. -- the rendering call
  123. local function draw_point(x,y,i)
  124.   local color
  125.   if i==config.maxIterations then
  126.     color={0,0,0}
  127.   else
  128.     local index=(i%#color_palette)+1
  129.     color=color_palette[index]
  130.   end
  131.   pixel:fill_color(color)
  132.   pixel:draw(fractalRender,x,y)
  133. end
  134. -- the core functions. Called once per map update.
  135. -- i don't use for loops to get each point, but increase indices at the end of this function instead.
  136. -- to save the engine from lagging/crashing
  137. local function calculateNextPoint()
  138.   local z={
  139.     real=0,
  140.     imag=0,
  141.   }
  142.   local real=currentNumber.real
  143.   local imag=currentNumber.imag
  144.   local i=0
  145.   local e=config.powerExponent
  146.   --The core calculation loop. see the explaination at the beginning of the script
  147.   while (i<config.maxIterations and get_module(z)<=math.pow(2,e)) do
  148.     local temp=complex_power(z,e)
  149.     z.real=temp.real+real
  150.     z.imag=temp.imag+imag
  151.     i=i+1
  152.   end
  153.   --render point
  154.   draw_point(currentPoint.x,currentPoint.y,i)
  155.   --Prepare for next loop
  156.   currentPoint.x=currentPoint.x+1
  157.   currentNumber.real = real + config.step.x
  158.  
  159.   --End of horizontal line
  160.   if real > config.numberBoundaries.x.max then
  161.     currentNumber.real = config.numberBoundaries.x.min
  162.     currentNumber.imag = imag-config.step.y
  163.     currentPoint.x=0
  164.     currentPoint.y=currentPoint.y+1
  165.   end
  166.  
  167.   if imag<config.numberBoundaries.y.min then
  168.   --All points have been processed. We can stop the function from starting again
  169.  
  170.     --Debug lines for render time benchmarking? uncomment the next two lines to activate
  171.     --print("done !")
  172.     --print("time elapsed :"..os.clock()-start_time .."s")
  173.  
  174.     return false
  175.   end
  176.   return true
  177. end
  178.  
  179. function map:on_started()
  180.   map:get_hero():set_visible(false)
  181. end
  182.  
  183. function map:on_finished()
  184.   map:get_hero():set_visible(true)
  185. end
  186. -- starting the whole process
  187. function map:on_opening_transition_finished()
  188.   config.step.x=(config.numberBoundaries.x.max-config.numberBoundaries.x.min)/(config.grid.width-1)
  189.   config.step.y=(config.numberBoundaries.y.max-config.numberBoundaries.y.min)/(config.grid.height-1)
  190.   --print("step X="..config.step.x..",step Y="..config.step.y)
  191.  
  192.   --getting the current time for later usage, if you want to benchmark the rendering duration, just uncomment the next line
  193.   --start_time=os.clock()
  194.  
  195.   sol.timer.start(1, calculateNextPoint)
  196. end
  197. function map:on_draw(dst)
  198.   local camera=map:get_camera()
  199.   local x,y=camera:get_position()
  200.   local w,h=sol.video.get_quest_size()
  201.   fractalRender:draw_region(x,y,w,h,dst)
  202. end
Here is a screenshot of a render using the default exponent (2) :

2. the Sierpinsky Triangle
Code: Lua
  1. --[[
  2. The Sierpinsky triangle.
  3. On of the well known fractals, often used as a demonstration for calculators programming
  4. When rendered, it shows a Triforce-like shape, with each triangle being a raduction of the base figure.
  5. like this
  6.  
  7.        /\
  8.       /--\
  9.      /\  /\
  10.     /__\/__\
  11.    /\      /\
  12.   /__\    /__\
  13.  /\  /\  /\  /\
  14. /__\/__\/__\/__\
  15.  
  16. For more information, feel free to check on the Wiki or any math article
  17.  
  18. How to use :
  19. 1. create a map of the size of your choice
  20. 2. copy/paste this whole code in the map script.
  21. 3. setup the config variables with your own parameters
  22. 4. Run the map !
  23. --]]
  24.  
  25. local map = ...
  26.  
  27. -- Must be lower or equel to map size to render properly
  28. local grid={
  29.   width=200,
  30.   height=200
  31. }
  32. local maxIterations=10000
  33. -- the outer triangle summits
  34. local summits={
  35.    {0,0},
  36.    {grid.width,0},
  37.    {grid.width/2,grid.height},
  38. }
  39. local point
  40.  
  41. -- initial position
  42. local x=grid.width/2
  43. local y=grid.height/2
  44.  
  45. local iter
  46. local fractalRender= sol.surface.create(grid.width,grid.height)
  47. local pixel=sol.surface.create(1,1)
  48. pixel:fill_color({0,0,0})
  49. local game = map:get_game()
  50.  
  51. -- Event called at initialization time, as soon as this map becomes is loaded.
  52. function map:on_started()
  53.   map:get_hero():set_visible(false)
  54.   iter=0
  55.   --print("initial point : X="..x..", Y="..y)
  56.  
  57. end
  58.  
  59. function map:on_finished()
  60.   map:get_hero():set_visible(true)
  61. end
  62.  
  63. function map:on_update()
  64.  
  65.   if iter <maxIterations then
  66.     --these three lines are all the calculation. Yes, this fractal is very easy to setup.
  67.     --Note, they directly come from internet, so feel free to change to your own.
  68.     point=summits[math.random(3)]
  69.     x=(x+point[1])/2
  70.     y=(y+point[2])/2
  71.  
  72.     --[[
  73.  
  74.     -- my calculator's example script from the instruction book. Kept here for historical reasons, feel free to remove.
  75.     random=math.random(999)
  76.     --local random
  77.     --print("random number : "..random)
  78.     if random<=333 then
  79.       x=x/2
  80.       y=y/2
  81.     elseif random >333 and random <=666 then
  82.       x=(x+(base_size/2))/2
  83.       y=(y+base_size)/2
  84.     else
  85.       x=(x+base_size)/2
  86.       y=y/2
  87.     end    
  88.   --]]
  89.   --print("new point on cycle "..count..": X="..x..", Y="..y)
  90.   pixel:draw(fractalRender, x,grid.height-y)
  91.   iter=iter+1
  92.   end
  93.  
  94.  
  95. end
  96.  
  97. function map:on_draw(dst)
  98.   fractalRender:draw(dst)
  99. end
Screenshot of the render :


That's all for now! More to come later.... maybe !
Feel free to test, but be warned : it can, and will, take some time to get a full render, depending of your computer power and the parameters you chose.
if you have suggestions for improvements, feel free to.
Enjoy !

Edit (2017/09/21) : Added screenshots of the renders.
92
Bugs & Feature requests / Re: Straight movement bug with set_max_distance?
« Last post by llamazing on September 08, 2017, 02:56:26 pm »
Issue 1075 created
93
Bugs & Feature requests / Re: Straight movement bug with set_max_distance?
« Last post by Christopho on September 08, 2017, 08:58:35 am »
It looks like a bug to me. Can you report it on https://github.com/solarus-games/solarus/issues/new?
I will fix this for 1.6.
94
Bugs & Feature requests / Straight movement bug with set_max_distance?
« Last post by llamazing on September 08, 2017, 07:31:41 am »
I'm trying to use a straight movement, and the straight_movement:set_max_distance(max_distance) function is not working the way that I'd expect.

Below is simplified code of what I am trying to do. At 4 seconds the table obj is moved 100 pixels to the right, and then 3 seconds after that it is moved 100 pixels to the left to return it to its original position.

The problem is that the set_max_distance function appears to be calculating the distance from (0,0), when I'd expect it to calculate the distance from what the coordinates were at the start of the movement. The result is that the second movement ends up being a 200 pixel movement to the left instead of the 100 pixels that I wanted (the object stops when x=-100, which is 100 pixels from (0,0)).

Code: Lua
  1. local obj = {x=0, y=0}
  2. sol.timer.start(4000, function()
  3.   print(obj.x) --0 at 4 seconds
  4.  
  5.   local movt1 = sol.movement.create"straight"
  6.   movt1:set_speed(100)
  7.   movt1:set_angle(0)
  8.   movt1:set_max_distance(100)
  9.   movt1:start(obj)
  10.  
  11.   function movt1:on_finished() print(obj.x) end --obj.x=100 at 5 seconds
  12.  
  13.   sol.timer.start(3000, function()
  14.     print(obj.x) --100 at 7 seconds
  15.    
  16.     local movt2 = sol.movement.create"straight"
  17.     movt2:set_speed(100)
  18.     movt2:set_angle(math.pi)
  19.     movt2:set_max_distance(100)
  20.     movt2:start(obj)
  21.    
  22.     function movt2:on_finished() print(obj.x) end --obj.x=-100 at 9 seconds; expected obj.x=0 at 8 seconds
  23.   end)
  24. end)
95
Game art & music / Re: Original art
« Last post by Diarandor on September 07, 2017, 08:30:45 pm »
Comparison of the art of different artists. Just an image:
96
Your scripts / Re: Turtle Rock (Alttp) Pipe system
« Last post by PhoenixII54 on September 07, 2017, 02:00:34 pm »
Actually this is a reminiscence of an experimental way where i used the name to code the output direction when at the end of a pipe, which caused problems since i could''t get the next entity if i was at the very start of the chain, but since then i found that using the direction of the actual movement is way easier, so you can replace it by
Code: Lua
  1. map:get_entity(target)
since  the sensors now use the same name convention.

edit : code is now updated to apply the modification.
97
Your scripts / Re: Turtle Rock (Alttp) Pipe system
« Last post by Christopho on September 07, 2017, 10:52:23 am »
Hum, map:get_entities(prefix) returns an iterator so you can indeed get its first value by calling the result:
Code: Lua
  1. local entity = map:get_entities(prefix)()
  2.  
But this is obscure to read, and the parameter 1 is useless and has no effect.
98
Your scripts / Re: Turtle Rock (Alttp) Pipe system
« Last post by MetalZelda on September 07, 2017, 10:01:33 am »
Code: [Select]
movement:set_target(map:get_entities(target)(1))
Wait such thing is possible ?
99
Your scripts / Re: Turtle Rock (Alttp) Pipe system
« Last post by PhoenixII54 on September 06, 2017, 05:28:15 pm »
You know what ? that's what i did in a first version with custom entities instead of the sensors, it worked well too, but wanted something "universal", as in just place any number of sensors carefully named and voila!

Anyways, thanks!
100
Development / Re: Diagonales avec le mouvement de type "straight"
« Last post by PhoenixII54 on September 06, 2017, 05:24:26 pm »
Mea culpa, j'avais pas vu que j'avais laissĂ© le "=" dans mon code. En tout cas, ravi d'avoir pu ĂȘtre utile
Pages: 1 ... 8 9 [10]