main.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. --[[
  2. local origPrint = print
  3. print = function(...)
  4. origPrint(...)
  5. local str = ""
  6. for i = 1, select('#', ...) do
  7. if i ~= 1 then
  8. str = str .. "\t"
  9. end
  10. str = str .. tostring(select(i, ...))
  11. end
  12. love.filesystem.append( "log.txt", str .. "\r\n" )
  13. end
  14. --]]
  15. local love = _G.love
  16. network = require( "network.network" )
  17. config = require( "config" )
  18. game = require( "game" )
  19. lobby = require( "lobby" )
  20. map = require( "map" )
  21. if not DEDICATED then
  22. stats = require( "stats" )
  23. ui = require( "lib/punchUI" )
  24. menu = require( "menu" )
  25. images = require( "images" ) -- loads all images.
  26. chat = require( "chat" )
  27. Sounds = require("sounds")
  28. end
  29. utility = require( "utility" ) -- helper functions
  30. require( "callbacks" ) -- helper functions
  31. server = nil
  32. client = nil
  33. STATE = "Menu"
  34. MAX_PLAYERS = 16
  35. PORT = 3410
  36. MAIN_SERVER_URL = "http://www.formauri.es/personal/AffairMainServer"
  37. GAME_ID = "GridCars"
  38. if not DEDICATED then
  39. function love.load( args )
  40. config.load()
  41. images:load() -- preload all images
  42. Sounds:load()
  43. chat:init()
  44. lobby:init()
  45. menu:init()
  46. game:init()
  47. map:load()
  48. menu:show()
  49. --[[local startServer = false
  50. local startClient = false
  51. if args[2] == "client" then
  52. startClient = true
  53. elseif args[2] == "server" then
  54. startServer = true
  55. end
  56. if startServer then
  57. -- Start a server with a maximum of 16 users.
  58. server = network:startServer( MAX_PLAYERS, PORT )
  59. -- Connect to the server.
  60. client = network:startClient( 'localhost', PLAYERNAME, PORT )
  61. -- set server callbacks:
  62. setServerCallbacks( server )
  63. -- set client callbacks:
  64. setClientCallbacks( client )
  65. lobby:show()
  66. elseif startClient then
  67. if args[3] then
  68. client = network:startClient( args[3], PLAYERNAME, PORT )
  69. setClientCallbacks( client )
  70. else
  71. print( "Error. To start as client, you should give the address as the argument after 'client'." )
  72. end
  73. end]]
  74. --love.graphics.setBackgroundColor(25,25,25,255)
  75. love.graphics.setBackgroundColor( 20,80,20,255)
  76. end
  77. function love.update( dt )
  78. network:update( dt )
  79. if STATE == "Game" then
  80. game:update( dt )
  81. chat:update( dt )
  82. elseif STATE == "Lobby" then
  83. lobby:update( dt )
  84. chat:update( dt )
  85. elseif STATE == "Menu" then
  86. menu:update( dt )
  87. end
  88. ui:update( dt )
  89. ui:mousemoved( love.mouse.getPosition() )
  90. end
  91. function love.keypressed( key, unicode )
  92. --chat:keypressed( key )
  93. if chat.active then
  94. chat:keypressed( key )
  95. elseif not ui:keypressed( key, unicode ) then
  96. map:keypressed( key )
  97. chat:keypressed( key )
  98. end
  99. end
  100. function love.textinput( letter )
  101. --chat:textinput( letter )
  102. if chat.active then
  103. chat:textinput( letter )
  104. elseif (not ui:textinput( letter )) then
  105. end
  106. end
  107. function love.mousepressed( x, y, button )
  108. if button == 1 then button = "l" end
  109. if button == 2 then button = "r" end
  110. if button == 3 then button = "m" end
  111. if ui:mousepressed( x, y, button ) then
  112. return
  113. end
  114. map:mousepressed( x, y, button )
  115. if STATE == "Game" then
  116. game:mousepressed( x, y, button )
  117. end
  118. end
  119. function love.wheelmoved(x, y)
  120. local mx, my = love.mouse.getPosition(x, y)
  121. local limit, btn = y, "wu"
  122. if y < 0 then
  123. limit, btn = -y, "wd"
  124. end
  125. for i = 1, limit do
  126. _G.love.mousepressed(mx, my, btn)
  127. end
  128. end
  129. function love.draw()
  130. if STATE == "Game" then
  131. game:draw()
  132. elseif STATE == "Lobby" then
  133. lobby:draw()
  134. elseif STATE == "Menu" then
  135. menu:draw()
  136. end
  137. if STATE == "Game" or STATE == "Lobby" then
  138. chat:draw()
  139. end
  140. ui:draw()
  141. end
  142. else
  143. -- Dedicated server
  144. local socket = require("socket")
  145. dedicated = {
  146. currentMapName = nil,
  147. mapCycleID = 0
  148. }
  149. config.load()
  150. --lobby:init()
  151. --game:init()
  152. map:load()
  153. function dedicated:startServer()
  154. local err
  155. server, err = network:startServer( MAX_PLAYERS, PORT )
  156. if server then
  157. -- set client callbacks:
  158. setServerCallbacks( server )
  159. lobby:show()
  160. updateAdvertisementInfo()
  161. network.advertise:setURL( MAIN_SERVER_URL )
  162. network.advertise:setID( GAME_ID )
  163. network.advertise:start( server, "both" )
  164. else
  165. -- If I can't start a server for some reason, let user know and exit:
  166. print("ERROR: Could not start server:")
  167. print( "\t", err)
  168. os.exit()
  169. end
  170. end
  171. function dedicated:update( dt )
  172. if STATE == "Lobby" then
  173. -- Wait for at least one user:
  174. if server:getNumUsers() > 0 then
  175. -- Check if all clients are ready and if so start game
  176. if not self.postMatchLocked then
  177. if lobby:attemptGameStart() then
  178. game:show()
  179. end
  180. end
  181. end
  182. elseif STATE == "Game" then
  183. -- Let's hope it never gets below 0... :P
  184. if game:getNumUsersPlaying() <= 0 then
  185. game:sendBackToLobby()
  186. end
  187. end
  188. end
  189. -- Show the old map for a set period of time.
  190. function dedicated:postMatchLock()
  191. self.postMatchLocked = true
  192. end
  193. function getDirectoryItems( dir )
  194. return love.filesystem.getDirectoryItems(dir)
  195. end
  196. function dedicated:loadMapCycle()
  197. MAP_CYCLE = config.getValue( "MAP_CYCLE" )
  198. MAP_CYCLE_TBL = {}
  199. if MAP_CYCLE then
  200. for file in MAP_CYCLE:gmatch( "([^%s,]+)%s*,?%s*" ) do
  201. table.insert( MAP_CYCLE_TBL, file )
  202. end
  203. end
  204. -- Look for invalid file names ...
  205. local invalidFiles = {}
  206. for k = #MAP_CYCLE_TBL,1,-1 do -- iterate backwards because of the table.remove call
  207. local filename = MAP_CYCLE_TBL[k]
  208. -- Try to open the file, to see if it exists:
  209. local f = love.filesystem.open( "maps/" .. filename )
  210. if not f then
  211. -- table.insert( invalidFiles, k )
  212. table.remove( MAP_CYCLE_TBL, k )
  213. else
  214. f:close()
  215. end
  216. end
  217. end
  218. function dedicated:chooseMap()
  219. self:loadMapCycle()
  220. local foundMapFromMapCycle = nil
  221. if #MAP_CYCLE_TBL > 0 then
  222. if MAP_CYCLE_TBL[self.mapCycleID+1] then
  223. self.currentMapName = MAP_CYCLE_TBL[self.mapCycleID+1]
  224. self.mapCycleID = self.mapCycleID + 1
  225. else
  226. self.currentMapName = MAP_CYCLE_TBL[1]
  227. self.mapCycleID = 1
  228. end
  229. foundMapFromMapCycle = true
  230. end
  231. if not foundMapFromMapCycle then
  232. print("No map cycle found. Will play all maps in maps/ subfolder:")
  233. local files = getDirectoryItems( "maps/" )
  234. if #files < 1 then
  235. return
  236. end
  237. -- first map?
  238. if self.currentMapName == nil then
  239. self.currentMapName = files[1]
  240. else
  241. for k, f in ipairs(files) do
  242. if f == self.currentMapName then
  243. if files[k+1] then
  244. self.currentMapName = files[k+1]
  245. else
  246. self.currentMapName = files[1]
  247. end
  248. break
  249. end
  250. end
  251. end
  252. else
  253. print("Map cycle:")
  254. for k, file in ipairs( MAP_CYCLE_TBL ) do
  255. if k == self.mapCycleID then
  256. print( "\t>" .. file )
  257. else
  258. print( "\t " .. file )
  259. end
  260. end
  261. end
  262. -- Choose this map, load it and send it to all clients:
  263. lobby:chooseMap( self.currentMapName )
  264. self.postMatchLocked = false
  265. end
  266. -- Call the function to start the server right at startup
  267. dedicated:startServer()
  268. function love.update(dt)
  269. network:update( dt )
  270. if STATE == "Game" then
  271. game:update( dt )
  272. elseif STATE == "Lobby" then
  273. lobby:update( dt )
  274. end
  275. dedicated:update( dt )
  276. love.timer.sleep( 0.05 )
  277. end
  278. function love.quit()
  279. if network and network.advertise then
  280. network.advertise:stop()
  281. love.timer.sleep(1)
  282. end
  283. end
  284. end
  285. love = require('compat')