Advertisement
xerpi

3DnRaya beta

Jun 28th, 2011
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.31 KB | None | 0 0
  1. functions={}
  2.  
  3. n=2
  4.  
  5. scenery={}
  6. scenery[1] = {mode = "2D",n = 3,turn = 1,x = 100,y = 50, w = 20, h = 20, sel = {image = image.create(20, 20, color.new(255,255,255,125) ), x = 1, y = 1},J={}}
  7. scenery[1].table = {
  8. {"","",""},
  9. {"","",""},
  10. {"","",""}
  11. }
  12. scenery[1].J[1] = {string = "x", image = image.create(scenery[1].w, scenery[1].h, color.new(255,0,0) ) }
  13. scenery[1].J[2] = {string = "o", image = image.create(scenery[1].w, scenery[1].h, color.new(0,0,255) ) }
  14. -----------------------------------------------------------------------
  15.  
  16. scenery[2] = {mode = "3D",difx=0,dify=0,n = 3,turn = 1,ang = 0,pos={x = 1,y = -6,z=-43},rot={x=33,y=0,z=0}, w = 10, h = 1.64, sel = {object = model.load("models/selec.obj"), x = 1, y = 1},J={}}
  17.     scenery[2].table={
  18.         {"o","x",""},
  19.         {"x","o","o"},
  20.         {"o","o","x"}
  21.         }
  22. scenery[2].pos.x = 0- scenery[2].w-scenery[2].difx
  23. scenery[2].J[1] = {string = "x", object = model.load("models/cross.obj") }
  24. scenery[2].J[2] = {string = "o", object = model.load("models/circle.obj") }
  25.  
  26.  
  27. function functions.draw_scenery(array)
  28.     for y = 1, #array.table do
  29.         for x = 1, #array.table[y] do
  30.             for i = 1, #array.J do
  31.                 if array.mode == "2D" then
  32.                     if array.table[y][x] == array.J[i].string then
  33.                         array.J[i].image:blit(array.x + (x-1)*array.w, array.y + (y-1)*array.h)
  34.                     end
  35.                 elseif array.mode == "3D" then
  36.                     if array.table[y][x] == array.J[i].string then
  37.                         array.J[i].object:position(array.pos.x +((x-1)*array.w +(x-1)*array.difx), array.pos.y +math.sin(math.rad(-array.rot.x))*((y-1)*array.w + (y-1)*array.dify),array.pos.z + math.cos(math.rad(-array.rot.x))*((y-1)*array.w+ (y-1)*array.dify))
  38.                         array.J[i].object:rotation(math.rad(array.rot.x), math.rad(array.rot.y) ,math.rad(array.rot.z))
  39.                         array.J[i].object:blit()
  40.                     end            
  41.                 end
  42.             end
  43.         end
  44.     end
  45. end
  46.  
  47. function functions.draw_scenery_contorn(array,colore)
  48.     if array.mode == "2D" then
  49.         draw.rect( array.x, array.y , #array.table * array.w, #array.table[1] * array.h, ( array.colore or color.new(255,255,255) ) )
  50.     elseif array.mode == "3D" then
  51. draw.rect(array.pos.x, array.pos.y, array.pos.y +math.sin(math.rad(-array.rot.x))*((#array.table-1)*array.w + (#array.table-1)*array.dify) , array.pos.z + math.cos(math.rad(-array.rot.x))*((#array.table[1]-1)*array.w+ (#array.table[1]-1)*array.dify), color.new(255,0,0) )    
  52.     end
  53. end
  54.  
  55. function functions.print_selector(array)
  56.     if array.mode == "2D" then
  57.         array.sel.image:blit( array.x + (array.sel.x - 1)*array.w, array.y + (array.sel.y - 1)*array.h)
  58.     elseif array.mode == "3D" then
  59.         array.sel.object:position(array.pos.x +((array.sel.x-1)*array.w +(array.sel.x-1)*array.difx), array.pos.y +math.sin(math.rad(-array.rot.x))*((array.sel.y-1)*array.w + (array.sel.y-1)*array.dify),array.pos.z + math.cos(math.rad(-array.rot.x))*((array.sel.y-1)*array.w+ (array.sel.y-1)*array.dify))
  60.         array.sel.object:rotation(math.rad(array.rot.x), math.rad(array.rot.y) ,math.rad(array.rot.z))
  61.         array.sel.object:blit()
  62.     end
  63. end
  64.  
  65. function functions.next_turn(array)
  66.     array.turn = array.turn + 1 if array.turn > #array.J then array.turn = 1 end
  67. end
  68.  
  69. function functions.move_selector(array)
  70.     if controls.press("up") then
  71.         array.sel.y = array.sel.y - 1; if array.sel.y < 1 then array.sel.y = #array.table end
  72.     end
  73.     if controls.press("down") then
  74.         array.sel.y = array.sel.y + 1; if array.sel.y > #array.table then array.sel.y = 1 end
  75.     end
  76.     if controls.press("left") then
  77.         array.sel.x = array.sel.x - 1; if array.sel.x < 1 then array.sel.x = #array.table[1] end
  78.     end
  79.     if controls.press("right") then
  80.         array.sel.x = array.sel.x + 1; if array.sel.x > #array.table[1] then array.sel.x = 1 end
  81.     end
  82. end
  83.  
  84. function fb(array,x,y)
  85.     if  x <= #array.table[1] and y <= #array.table
  86.     and x >= 1 and y >= 1 then return true else return false end
  87. end
  88.  
  89. function functions.check(array)
  90.     local cont = {hor_right = 1,hor_left = 1,ver_up = 1,ver_down = 1,dia_up_inc = 1,dia_up_dec = 1,dia_down_inc = 1,dia_down_dec = 1};
  91.     local completed = true;
  92.     local s = "";
  93.     for y = 1 , #array.table do
  94.         for x = 1, #array.table[y] do
  95.             if array.table[y][x] == "" then completed = false; end --board completed
  96.             if array.table[y][x] != "" then            
  97.                 s = array.table[y][x]
  98.                 for i = 1, array.n do
  99.                     if fb(array,x+i,y) and array.table[y][x+i] == s then cont.hor_right = cont.hor_right+1 end
  100.                     if fb(array,x,y+i) and array.table[y+i][x] == s then cont.ver_down = cont.ver_down+1 end
  101.                     if fb(array,x-i,y) and array.table[y][x-i] == s then cont.hor_left = cont.hor_left+1 end
  102.                     if fb(array,x,y-i) and array.table[y-i][x] == s then cont.ver_up = cont.ver_up+1 end
  103.                     if fb(array,x+i,y-i) and array.table[y-i][x+i] == s then cont.dia_up_dec = cont.dia_up_dec + 1 end
  104.                     if fb(array,x-i,y-i) and array.table[y-i][x-i] == s then cont.dia_up_inc = cont.dia_up_inc + 1 end
  105.                     if fb(array,x+i,y+i) and array.table[y+i][x+i] == s then cont.dia_down_inc = cont.dia_down_inc + 1 end
  106.                     if fb(array,x-i,y+i) and array.table[y+i][x-i] == s then cont.dia_down_dec = cont.dia_down_dec+1 end                   
  107.                 end            
  108.                 if cont.hor_right >= array.n then return s end cont.hor_right = 1;
  109.                 if cont.hor_left >= array.n then return s end cont.hor_left = 1;                   
  110.                 if cont.ver_up >= array.n then return s end cont.ver_up = 1;
  111.                 if cont.ver_down >= array.n then return s end cont.ver_down = 1;                   
  112.                 if cont.dia_up_inc >= array.n then return s end cont.dia_up_inc = 1;               
  113.                 if cont.dia_down_inc >= array.n then return s end cont.dia_down_inc = 1;   
  114.                 if cont.dia_up_dec >= array.n then return s end cont.dia_up_dec = 1;               
  115.                 if cont.dia_down_dec >= array.n then return s end cont.dia_down_dec = 1;
  116.                 cont = {hor_right = 1,hor_left = 1,ver_up = 1,ver_down = 1,dia_up_inc = 1,dia_up_dec = 1,dia_down_inc = 1,dia_down_dec = 1};
  117.                
  118.             end
  119.         end
  120.     end
  121.     return completed;
  122. end
  123.  
  124. function functions.check_winner(s,array)
  125. for i = 1, #array.J do
  126.     if array.J[i].string == s then return i end
  127. end
  128. return false
  129. end
  130.  
  131. function functions.fill_scenery(array)
  132.     local winner = functions.check(array)
  133.     if  winner != nil then
  134.         if type(winner) == "boolean" and winner then
  135.             os.message("Tie.")
  136.             reset()
  137.         elseif type(winner) == "string" then
  138.             os.message("Player "..functions.check_winner(winner,array).." wins.")
  139.             reset()
  140.         end
  141.     end
  142.     if controls.press("cross") then
  143.         if array.table[array.sel.y][array.sel.x] == "" then
  144.             array.table[array.sel.y][array.sel.x] = array.J[array.turn].string
  145.             functions.next_turn(array)
  146.         end            
  147.     end
  148. end
  149. ------
  150. function reset()
  151.     scenery[n].table = {
  152.         {"","",""},
  153.         {"","",""},
  154.         {"","",""}
  155.         }
  156.     scenery[2].table={
  157.         {"o","x",""},
  158.         {"x","o","o"},
  159.         {"o","o","x"}
  160.         }
  161. end
  162.  
  163. while true do
  164. controls.read()
  165.  
  166. functions.draw_scenery(scenery[n])
  167. functions.draw_scenery_contorn(scenery[n], color.new(0,255,0))
  168. functions.move_selector(scenery[n])
  169. functions.print_selector(scenery[n])
  170. ----------
  171. if scenery[n].mode == "3D" then
  172.     if controls.r() then scenery[n].pos.z = scenery[n].pos.z-1 end
  173.     if controls.l() then scenery[n].pos.z = scenery[n].pos.z+1 end
  174.         if controls.start() then
  175.             if controls.down() then scenery[n].pos.y = scenery[n].pos.y-1 end
  176.             if controls.up() then scenery[n].pos.y = scenery[n].pos.y+1 end
  177.             if controls.right() then scenery[n].pos.x = scenery[n].pos.x+1 end
  178.             if controls.left() then scenery[n].pos.x = scenery[n].pos.x-1 end                  
  179.             if math.abs(controls.analogy()) > 40 then scenery[n].rot.x = scenery[n].rot.x + controls.analogy()/40 end
  180.             --if math.abs(controls.analogx()) > 40 then scenery[n].rot.y = scenery[n].rot.y + controls.analogx()/40 end        
  181.         end
  182. else
  183.     if math.abs(controls.analogx()) > 40 then scenery[n].x = scenery[n].x + controls.analogx()/40 end
  184.     if math.abs(controls.analogy()) > 40 then scenery[n].y = scenery[n].y + controls.analogy()/40 end
  185. end
  186.  
  187.  
  188. if controls.start() and controls.r() and controls.l() then
  189.     reset()
  190.     scenery[2].pos={x = 1,y = -6,z=-43} scenery[2].rot={x=0,y=0,z=0}
  191. end
  192.  
  193. if controls.l() and controls.r() and controls.right() then n = 2 end
  194. if controls.l() and controls.r() and controls.left() then n = 1 end
  195.  
  196.  
  197. if controls.select() then a() end
  198. screen.print(5,5,"Turn: "..scenery[n].turn.." S: "..n.."  Mode: "..scenery[n].mode)
  199. screen.print(5,25,"Pos.x: "..scenery[2].pos.x.."  Pos.y: "..scenery[2].pos.y.."  Pos.z: "..scenery[2].pos.z.."    rot.x"..scenery[2].rot.x)
  200. screen.flip()
  201. functions.fill_scenery(scenery[n])
  202. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement