Graphics = "datafiles/3D/"; local player; local myRoot; local passing; local point_p1; local point_p2; -- komi local ending = true; players = { LD.SYSTEM.Player:new(), LD.SYSTEM.Player:new() }; lastPlay = {nil, nil}; function main() mw = LD.GUI.MainWindow:new("[Prototype]Last Dungeon, the best game ever !", 700, 700, true); LD.GUI.Gui:setHandler("OnEvent"); device = mw:device(); createBackground(); color = LD.GUI.Color:new(0,0,255); mw:start("pvp", color); end function createBackground() bpvp = LD.GUI.Background:new("pvp"); bpvp:setCallbackFunction("OnEventPvP"); bpvp:setPrimaryCallbackFunction("OnPrimaryEventPvP"); bquit = LD.GUI.Background:new("quit"); bquit:setCallbackFunction("OnEventQuit"); end function OnEventQuit(args) standEvent = tolua.cast(args,"const LD::SYSTEM::StandardEvent"); if standEvent.name ~= "quit" then LD.GUI.MainWindow:instance():changeBackground(""); end end function OnEventPvP(args) standEvent = tolua.cast(args,"const LD::SYSTEM::StandardEvent"); if standEvent.name == "enter" then loadMap(9); loadGui(); init(); end if standEvent.name == "restart" then init(); end if ending then return; end if standEvent.name == "pass" then pass(); end if standEvent.name == "surrender" then surrender(); end end function OnPrimaryEventPvP(args) event = tolua.cast(args,"const LD::SYSTEM::Event"); if event:type() == "keyboard" then keyevent = tolua.cast(args,"const LD::SYSTEM::KeyboardEvent"); if keyevent.keydown ~= true then -- p is for pass turn if keyevent.key == 80 then sendEvent("pass"); return true; end -- a is for surrender if keyevent.key == 65 then sendEvent("surrender"); return true; end -- r is for restart if keyevent.key == 82 then sendEvent("restart"); return true; end end end if event:type() == "mouse" then mouseevent = tolua.cast(args,"const LD::SYSTEM::MouseEvent"); if mouseevent:isLeft() and mouseevent:isDoubleClic() then x = mouseevent.x; y = mouseevent.y; element = LD.I3D.Element3D:getElementAtPosition(x,y); if element and element:type() == "plinth" then plinth = LD.I3D.Plinth:toPlinth(element); element:type(); element:type(); playOnPlinth(plinth); end end end return false; end function sendEvent(event) back = LD.GUI.MainWindow:instance():background(); standevent = LD.SYSTEM.StandardEvent:new(back, event); back:postEvent(standevent); end function loadMap(size) ymax = 27 + 34; yoffset = 27; y = yoffset - (ymax - size*5)/2; xmax= 27 + 34; xoffset = 27; x = (xmax - size*5)/2 - xoffset; -- droite, haut, fond position = LD.I3D.V3Df(x,y,40); angle = LD.I3D.V3Df(270,0,270); socles = std.vector_LD__I3D__Plinth__:new(); socles:resize(size*size); num = 0; for i = 1, size, 1 do position.y = y; for j = 1, size, 1 do socle = createPlinth(position, angle); socles[num] = socle; num = num + 1; addNeightBourg( socles, socle, i, j, size); position.y = position.y - 5; end position.x = position.x + 5 end position:delete(); angle:delete(); end function createPlinth(position, angle) pl = LD.I3D.Plinth:new( Graphics .. "plinth.3ds", false); pl:setPosition( position ); pl:setRotation( angle ); return pl; end function addNeightBourg(socles, socle, i, j, size) if i ~= 1 then ti = i - 2; tj = j - 1; socle:addNeightBourg(socles[ti*size + tj]); end if j ~= 1 then ti = i - 1; tj = j - 2; socle:addNeightBourg(socles[ti*size + tj]); end end function loadGui() local schememan = CEGUI.SchemeManager:getSingleton(); local fontman = CEGUI.FontManager:getSingleton(); local wmgr = CEGUI.WindowManager:getSingleton(); schememan:create( "TaharezLook.scheme" ); fontman:create( "DejaVuSans-10.font" ); fontman:create( "Commonwealth-10.font" ); myRoot = wmgr:createWindow( "TaharezLook/FrameWindow", "root" ); CEGUI.System:getSingleton():setGUISheet( myRoot ); myRoot:setPosition( CEGUI.UVector2(CEGUI.UDim(0.2,0),CEGUI.UDim(0,-10))); myRoot:setSize( CEGUI.UVector2(CEGUI.UDim(0.6,0),CEGUI.UDim(0,20))); end function getNextPlayer() passing = false; if player == 1 then player = 2; else player = 1; end setText("Player", "Player " .. player.. ", it's your turn"); end do Score = ""; Action = ""; Player = ""; function setText(type, text) if type == "Score" then Score = text; end if type == "Action" then Action = text; end if type == "Player" then Player = text; end myRoot:setText(Score .. " " .. Action .. " " .. Player); end end function pass() lastPlay[player] = nil; setText("Action", "Player " .. player .. "pass."); if passing then removeUselessPawn(); endOfParty(); else getNextPlayer(); passing = true; end end function endOfParty() winner = 1; if point_p2 > point_p1 then winner = 2; end setText("Player", "Player " .. winner .. " win."); end function removeUselessPawn() -- todo end function init() LD.I3D.PawnGroup:removeAll(); player = 0; getNextPlayer(); point_p1 = 0; point_p2 = 7.5; passing = false; ending = false; setText("Score", point_p1 .. ":" .. point_p2); setText("Action", "Game start"); end function surrender() if player == 2 then point_p2 = 0; else point_p1 = 0; end setText("Action", "Player " .. player .. " surrender."); endOfParty(); end function OnEvent(args) event = tolua.cast(args,"const LD::SYSTEM::Event"); back = LD.GUI.MainWindow:instance():background(); return back:onEvent(event); end function playOnPlinth(plinth) -- droit de poser ? if not plinth:isEmpty() then return; end if lastPlay[player] ~= nil and lastPlay[player] == plinth then return; end neights = plinth:neigthbourg(); ok = false table = {} for i=0,neights:size()-1,1 do if neights[i]:isEmpty() then ok = true break; else if neights[i]:pawn():player() == players[player] then --même joueur if table[neights[i]:pawn():group()] == nil then if neights[i]:pawn():group():freedom() > neights:size() - i then ok = true; break; else table[neights[i]:pawn():group()] = neights[i]:pawn():group():freedom() - 1; end else if table[neights[i]:pawn():group()] > neights:size() - i then ok = true; break; else table[neights[i]:pawn():group()] = table[neights[i]:pawn():group()] - 1; end end else -- adversaire if table[neights[i]:pawn():group()] == nil then if neights[i]:pawn():group():freedom() <= 1 then ok = true; break; else table[neights[i]:pawn():group()] = neights[i]:pawn():group():freedom() - 1; end else if table[neights[i]:pawn():group()] <= 1 then ok = true; break; else table[neights[i]:pawn():group()] = table[neights[i]:pawn():group()] - 1; end end end end end if not ok then return end -- pose pawn = putPawn(plinth, player); -- add to groupe size = neights:size(); for i=0,size-1,1 do if neights[i]:pawn() then pGroup = neights[i]:pawn():group():firstPlayer(); pPawn = pawn:group():firstPlayer(); if pGroup == pPawn then pawn:group():fusion( neights[i]:pawn():group() ); else --destroy other group if neights[i]:pawn():group():freedom() <= 0 then point = pawn:group():eatGroup( neights[i]:pawn():group() ); if player == 1 then point_p1 = point_p1 + point; else point_p2 = point_p2 + point; end end end end end -- mise à jour points setText("Score", point_p1 .. ":" .. point_p2); setText("Action", "Player " .. player .. " put a pawn."); getNextPlayer(); end function putPawn(plinth, player) pawn = LD.I3D.Pawn:new(players[player], Graphics .. "pawn" .. player .. ".3ds"); pawn:putOnPlinth(plinth); pawn:setParentE(plinth); lastPlay[player] = plinth; return pawn; end main()