1--       _________ __                 __
2--      /   _____//  |_____________ _/  |______     ____  __ __  ______
3--      \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
4--      /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
5--     /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
6--             \/                  \/          \//_____/            \/
7--  ______________________                           ______________________
8--                        T H E   W A R   B E G I N S
9--         Stratagus - A free fantasy real time strategy game engine
10--
11--	(c) Copyright 2013 by Kyran Jackson
12--
13--      This program is free software; you can redistribute it and/or modify
14--      it under the terms of the GNU General Public License as published by
15--      the Free Software Foundation; either version 2 of the License, or
16--      (at your option) any later version.
17--
18--      This program is distributed in the hope that it will be useful,
19--      but WITHOUT ANY WARRANTY; without even the implied warranty of
20--      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21--      GNU General Public License for more details.
22--
23--      You should have received a copy of the GNU General Public License
24--      along with this program; if not, write to the Free Software
25--      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26--
27
28OldRunSinglePlayerGameMenu = RunSinglePlayerGameMenu
29
30function CleanCustomGame_Lua()
31	RunSinglePlayerGameMenu = OldRunSinglePlayerGameMenu
32
33	GameDefinitionSetup("initialise")
34
35	for i=0,15 do
36		timers[i] = nil
37		SetMapTeams(i, nil, nil)
38		ftm_choice[i] = nil
39		ftm_gold[i] = nil
40		ftm_wood[i] = nil
41		ftm_oil[i] = nil
42		aiftm_index[i] = nil
43		aiftm_terminate[i] = nil
44		aiftm_loop[i] = nil
45		aiftm_mana[i] = nil
46		ftm_faction[i] = nil
47		ftm_team[i] = nil
48		ftm_team_tempx[i] = nil
49		ftm_team_tempy[i] = nil
50		ftm_team_x1[i] = nil
51		ftm_team_y1[i] = nil
52		ftm_team_x2[i] = nil
53		ftm_team_y2[i] = nil
54		ftm_team_startx[i] = nil
55		ftm_team_starty[i] = nil
56		ftm_team_orderx[i] = nil
57		ftm_team_ordery[i] = nil
58		for j = 0, 15 do
59			aiftm_action[i][j] = nil
60			aiftm_unit[i][j] = nil
61			aiftm_quantity[i][j] = nil
62			aiftm_x_to[i][j] = nil
63			aiftm_y_to[i][j] = nil
64			aiftm_x_from[i][j] = nil
65			aiftm_y_from[i][j] = nil
66			aiftm_unit_option1[i][j] = nil
67			aiftm_unit_option2[i][j] = nil
68			aiftm_unit2[i][j] = nil
69			aiftm_unit2_option1[i][j] = nil
70			aiftm_unit2_option2[i][j] = nil
71			aiftm_unit3[i][j] = nil
72			aiftm_unit3_option1[i][j] = nil
73			aiftm_unit3_option2[i][j] = nil
74		end
75	end
76
77	BlueLeader = nil
78	UnitFootmanNum = nil
79	UnitArcherNum = nil
80	UnitRangerNum = nil
81	UnitBallistaNum = nil
82	UnitPaladinNum = nil
83	UnitKnightNum = nil
84	UnitMageNum = nil
85	UnitDwarvesNum = nil
86
87	Blue2Temp_x = nil
88	Blue2Temp_y = nil
89	Blue2Step = nil
90	Blue2Mana = nil
91	Blue1Temp_x = nil
92	Blue1Temp_y = nil
93	Blue1Step = nil
94	Blue1Mana = nil
95	BlueTemp = nil
96	BlueTeam1 = nil
97	BlueTeam2 = nil
98	BlueTemp_x1 = nil
99	BlueTemp_y1 = nil
100	BlueTemp_x2 = nil
101	BlueTemp_y2 = nil
102	BlueTeam1_x1 = nil
103	BlueTeam1_y1 = nil
104	BlueTeam1_x2 = nil
105	BlueTeam1_y2 = nil
106	BlueTeam2_x1 = nil
107	BlueTeam2_y1 = nil
108	BlueTeam2_x2 = nil
109	BlueTeam2_y2 = nil
110	blueribbon_stepping = nil
111	BlueBarracks1_x = nil
112	BlueBarracks1_y = nil
113	BlueBarracks2_x = nil
114	BlueBarracks2_y = nil
115	BlueBarracks3_x = nil
116	BlueBarracks3_y = nil
117	BlueBarracks4_x = nil
118	BlueBarracks4_y = nil
119	BlueInventor_x = nil
120	BlueInventor_y = nil
121	BlueMageTower_x = nil
122	BlueMageTower_y = nil
123
124	UnitGruntNum = nil
125	UnitAxethrowerNum = nil
126	UnitBerserkerNum = nil
127	UnitCatapultNum = nil
128	UnitOgreMageNum = nil
129	UnitOgreNum = nil
130	UnitDeathKnightNum = nil
131	UnitGoblinSappersNum = nil
132
133	redribbon_stepping = nil
134
135	RedLeader = nil
136	Red1Mana = nil
137	Red1Mana = nil
138
139	RedTemp = nil
140	RedTemp_x1 = nil
141	RedTemp_y1 = nil
142	RedTemp_x2 = nil
143	RedTemp_y2 = nil
144	Red2Temp_x = nil
145	Red2Temp_y = nil
146	Red2Step = nil
147	Red2Mana = nil
148	Red1Temp_x = nil
149	Red1Temp_y = nil
150	Red1Step = nil
151	Red1Mana = nil
152	RedTeam1 = nil
153	RedTeam2 = nil
154	RedTeam1_x1 = nil
155	RedTeam1_y1 = nil
156	RedTeam1_x2 = nil
157	RedTeam1_y2 = nil
158	RedTeam2_x1 = nil
159	RedTeam2_y1 = nil
160	RedTeam2_x2 = nil
161	RedTeam2_y2 = nil
162
163	RedBarracks1_x = nil
164	RedBarracks1_y = nil
165	RedBarracks2_x = nil
166	RedBarracks2_y = nil
167	RedBarracks3_x = nil
168	RedBarracks3_y = nil
169	RedBarracks4_x = nil
170	RedBarracks4_y = nil
171	RedInventor_x = nil
172	RedInventor_y = nil
173	RedMageTower_x = nil
174	RedMageTower_y = nil
175
176	-- Resetting the default map.
177	mapname = "maps/skirmish/(2)timeless-isle.smp.gz"
178	local buttonStatut = 0 -- 0:not initialised, 1: Ok, 2: Cancel
179	mapinfo = {
180		playertypes = {nil, nil, nil, nil, nil, nil, nil, nil},
181		description = "",
182		nplayers = 1,
183		w = 32,
184		h = 32,
185		id = 0
186}
187-- Resetting the default map directory.
188function RunSelectScenarioMenu()
189  buttonStatut = 0
190  local menu = WarMenu(nil, panel(5), false)
191  menu:setSize(352, 352)
192  menu:setPosition((Video.Width - 352) / 2, (Video.Height - 352) / 2)
193  menu:setDrawMenusUnder(true)
194
195  menu:addLabel(_("Select scenario"), 176, 8)
196
197  local browser = menu:addBrowser("maps/skirmish", "^.*%.smp%.?g?z?$",
198    24, 140, 300, 108, mapname)
199
200  local l = menu:addLabel(browser:getSelectedItem(), 24, 260, Fonts["game"], false)
201
202  local function cb(s)
203    l:setCaption(browser:getSelectedItem())
204    l:adjustSize()
205  end
206  browser:setActionCallback(cb)
207
208  menu:addHalfButton("~!OK", "o", 48, 318,
209    function()
210      local cap = l:getCaption()
211
212      if (browser:getSelected() < 0) then
213        return
214      end
215      buttonStatut = 1
216      mapname = browser.path .. cap
217      menu:stop()
218    end)
219  menu:addHalfButton(_("~!Cancel"), "c", 198, 318,
220    function() buttonStatut = 2; menu:stop() end)
221
222  menu:run()
223end
224end
225
226function StartCustomGame_Lua()
227	RunSinglePlayerGameMenu = NewRunSinglePlayerGameMenu
228
229	-- Selecting the default map.
230	mapname = "maps/ftm/(4)beethoven-day.smp"
231	local buttonStatut = 0 -- 0:not initialised, 1: Ok, 2: Cancel
232	mapinfo = {
233		playertypes = {nil, nil, nil, nil, nil, nil, nil, nil},
234		description = "",
235		nplayers = 1,
236		w = 32,
237		h = 32,
238		id = 0
239}
240-- Selecting the map directory.
241function RunSelectScenarioMenu()
242  buttonStatut = 0
243  local menu = WarMenu(nil, panel(5), false)
244  menu:setSize(352, 352)
245  menu:setPosition((Video.Width - 352) / 2, (Video.Height - 352) / 2)
246  menu:setDrawMenusUnder(true)
247
248  menu:addLabel(_("Select scenario"), 176, 8)
249
250  local browser = menu:addBrowser("maps/ftm", "^.*%.smp%.?g?z?$",
251    24, 140, 300, 108, mapname)
252
253  local l = menu:addLabel(browser:getSelectedItem(), 24, 260, Fonts["game"], false)
254
255  local function cb(s)
256    l:setCaption(browser:getSelectedItem())
257    l:adjustSize()
258  end
259  browser:setActionCallback(cb)
260
261  menu:addHalfButton("~!OK", "o", 48, 318,
262    function()
263      local cap = l:getCaption()
264
265      if (browser:getSelected() < 0) then
266        return
267      end
268      buttonStatut = 1
269      mapname = browser.path .. cap
270      menu:stop()
271    end)
272  menu:addHalfButton(_("~!Cancel"), "c", 198, 318,
273    function() buttonStatut = 2; menu:stop() end)
274
275  menu:run()
276end
277end
278
279function NewRunSinglePlayerGameMenu()
280  local menu = WarMenu()
281  local offx = (Video.Width - 640) / 2
282  local offy = (Video.Height - 480) / 2
283  local d
284  local race
285  local resources
286  local opponents
287  local numunits
288  local gametype
289  local mapl
290  local descriptionl
291  local tilesetdd
292
293  menu:addLabel(wargus.Name .. " V" .. wargus.Version .. ", " .. wargus.Copyright, offx + 320, (Video.Height - 90) + 18*4, Fonts["small"]) -- Copyright information.
294
295  menu:addLabel(_("Scenario:"), offx + 16, offy + 360, Fonts["game"], false)
296  mapl = menu:addLabel(string.sub(mapname, 6), offx + 16, offy + 360 + 24, Fonts["game"], false)
297  descriptionl = menu:addLabel("descriptionl", offx + 16 + 70, offy + 360, Fonts["game"], false)
298
299  menu:addLabel(_("~<For the Motherland~>"), (Video.Width / 2), offy + 192, nil, true)
300  menu:addFullButton(_("S~!elect Scenario"), "e", offx + 640 - 224 - 16, offy + 360 + 36*0,
301    function()
302      local oldmapname = mapname
303	  menu:stop()
304      RunSelectScenarioMenu()
305      if (mapname ~= oldmapname) then
306		for player=0,15 do
307			SetMapTeams(player, nil, nil)
308		end
309        GetMapInfo(mapname)
310        MapChanged()
311      end
312	  NewRunSinglePlayerGameMenu()
313    end)
314  menu:addFullButton(_("~!Start Game"), "s", offx + 640 - 224 - 16, offy + 360 + 36*1,
315    function()
316
317	for player=0,15 do
318		if (GameDefinition["Map"][player]["Team"] == 1) then
319			if (GameDefinition["Map"][player]["Player"] == 1) then
320				if (t1p1char:getSelected() == 1) then GameDefinition["Player"][1][1]["Name"] = "Aya Kalang" elseif (t1p1char:getSelected() == 2) then GameDefinition["Player"][1][1]["Name"] = "Shane Wolfe" 	elseif (t1p1char:getSelected() == 3) then GameDefinition["Player"][1][1]["Name"] = "Sandria Fields" end
321				if (t1p1race:getSelected() == 1) then GameDefinition["Player"][1][1]["Race"] = "human" 		elseif (t1p1race:getSelected() == 2) then GameDefinition["Player"][1][1]["Race"] = "orc" 			end
322			elseif (GameDefinition["Map"][player]["Player"] == 2) then
323				if (t1p2char:getSelected() == 1) then GameDefinition["Player"][1][2]["Name"] = "Aya Kalang" elseif (t1p2char:getSelected() == 2) then GameDefinition["Player"][1][2]["Name"] = "Shane Wolfe" 	elseif (t1p1char:getSelected() == 3) then GameDefinition["Player"][1][2]["Name"] = "Sandria Fields" end
324				if (t1p2race:getSelected() == 1) then GameDefinition["Player"][1][2]["Race"] = "human" 		elseif (t1p2race:getSelected() == 2) then GameDefinition["Player"][1][2]["Race"] = "orc" 			end
325			elseif (GameDefinition["Map"][player]["Player"] == 3) then
326				if (t1p3char:getSelected() == 1) then GameDefinition["Player"][1][3]["Name"] = "Aya Kalang" elseif (t1p3char:getSelected() == 2) then GameDefinition["Player"][1][3]["Name"] = "Shane Wolfe" 	elseif (t1p3char:getSelected() == 3) then GameDefinition["Player"][1][3]["Name"] = "Sandria Fields" end
327				if (t1p3race:getSelected() == 1) then GameDefinition["Player"][1][3]["Race"] = "human" 		elseif (t1p3race:getSelected() == 2) then GameDefinition["Player"][1][3]["Race"] = "orc" 			end
328			elseif (GameDefinition["Map"][player]["Player"] == 4) then
329				if (t1p4char:getSelected() == 1) then GameDefinition["Player"][1][4]["Name"] = "Aya Kalang" elseif (t1p4char:getSelected() == 2) then GameDefinition["Player"][1][4]["Name"] = "Shane Wolfe" 	elseif (t1p4char:getSelected() == 3) then GameDefinition["Player"][1][4]["Name"] = "Sandria Fields" end
330				if (t1p4race:getSelected() == 1) then GameDefinition["Player"][1][4]["Race"] = "human" 		elseif (t1p4race:getSelected() == 2) then GameDefinition["Player"][1][4]["Race"] = "orc" 			end
331			end
332		elseif (GameDefinition["Map"][player]["Team"] == 2) then
333			if (GameDefinition["Map"][player]["Player"] == 1) then
334				if (t2p1char:getSelected() == 1) then GameDefinition["Player"][2][1]["Name"] = "Aya Kalang" elseif (t2p1char:getSelected() == 2) then GameDefinition["Player"][2][1]["Name"] = "Shane Wolfe" 	elseif (t2p1char:getSelected() == 3) then GameDefinition["Player"][2][1]["Name"] = "Sandria Fields" end
335				if (t2p1race:getSelected() == 1) then GameDefinition["Player"][2][1]["Race"] = "human" 		elseif (t2p1race:getSelected() == 2) then GameDefinition["Player"][2][1]["Race"] = "orc" 			end
336			elseif (GameDefinition["Map"][player]["Player"] == 2) then
337				if (t2p2char:getSelected() == 1) then GameDefinition["Player"][2][2]["Name"] = "Aya Kalang" elseif (t2p2char:getSelected() == 2) then GameDefinition["Player"][2][2]["Name"] = "Shane Wolfe" 	elseif (t2p1char:getSelected() == 3) then GameDefinition["Player"][2][2]["Name"] = "Sandria Fields" end
338				if (t2p2race:getSelected() == 1) then GameDefinition["Player"][2][2]["Race"] = "human" 		elseif (t2p2race:getSelected() == 2) then GameDefinition["Player"][2][2]["Race"] = "orc" 			end
339			elseif (GameDefinition["Map"][player]["Player"] == 3) then
340				if (t2p3char:getSelected() == 1) then GameDefinition["Player"][2][3]["Name"] = "Aya Kalang" elseif (t1p3char:getSelected() == 2) then GameDefinition["Player"][1][3]["Name"] = "Shane Wolfe" 	elseif (t1p3char:getSelected() == 3) then GameDefinition["Player"][1][3]["Name"] = "Sandria Fields" end
341				if (t2p3race:getSelected() == 1) then GameDefinition["Player"][2][3]["Race"] = "human" 		elseif (t1p3race:getSelected() == 2) then GameDefinition["Player"][1][3]["Race"] = "orc" 			end
342			elseif (GameDefinition["Map"][player]["Player"] == 4) then
343				if (t2p4char:getSelected() == 1) then GameDefinition["Player"][2][4]["Name"] = "Aya Kalang" elseif (t2p4char:getSelected() == 2) then GameDefinition["Player"][2][4]["Name"] = "Shane Wolfe" 	elseif (t2p4char:getSelected() == 3) then GameDefinition["Player"][2][4]["Name"] = "Sandria Fields" end
344				if (t2p4race:getSelected() == 1) then GameDefinition["Player"][2][4]["Race"] = "human" 		elseif (t2p4race:getSelected() == 2) then GameDefinition["Player"][2][4]["Race"] = "orc" 			end
345			end
346		end
347	end
348      RunMap(mapname, nil, wc2.preferences.FogOfWar, 0)
349      menu:stop()
350    end)
351	--TODO: Cancel Game button doesn't work.
352  menu:addFullButton(_("~!Cancel Game"), "c", offx + 640 - 224 - 16, offy + 360 + 36*2, function() menu:stop(1); RunSinglePlayerTypeMenu(); CleanCustomGame_Lua(); end)
353
354  local team1x = (Video.Width / 2) - 175
355  local team1y = offy + 10 + 240
356  local team2x = (Video.Width / 2) + 175
357  local team2y = offy + 10 + 240
358  local team3x = offx
359  local team3y = offy + (10 + 240) - 20 + 400
360  local team4x = offx + 40 + 300
361  local team4y = offy + (10 + 240) - 20 + 400
362
363  for player=0,15 do
364	if (GameDefinition["Map"][player]["Team"] ~= nil) then
365		if (GameDefinition["Map"][player]["Team"] == 1) then
366			if (GameDefinition["Map"][player]["Player"] == 1) then
367				menu:addLabel(_("~<Team 1~>"), team1x, team1y - 25, Fonts["game"], true)
368				t1p1char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team1x - 152, team1y + 25*0,
369					function(dd) end)
370				t1p1char:setSize(152, 20)
371
372				t1p1race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team1x, team1y + 25*0,
373					function(dd) end)
374				t1p1race:setSize(152, 20)
375			elseif (GameDefinition["Map"][player]["Player"] == 2) then
376				t1p2char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team1x - 152, team1y + 25*1,
377					function(dd) end)
378				t1p2char:setSize(152, 20)
379
380				t1p2race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team1x, team1y + 25*1,
381					function(dd) end)
382				t1p2race:setSize(152, 20)
383			elseif (GameDefinition["Map"][player]["Player"] == 3) then
384				menu:addLabel(_("~<Team 1~>"), team1x, team1y - 25, Fonts["game"], true)
385				t1p3char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team1x - 152, team1y + 25*2,
386					function(dd) end)
387				t1p3char:setSize(152, 20)
388
389				t1p3race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team1x, team1y + 25*2,
390					function(dd) end)
391				t1p3race:setSize(152, 20)
392			elseif (GameDefinition["Map"][player]["Player"] == 4) then
393				t1p4char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team1x - 152, team1y + 25*3,
394					function(dd) end)
395				t1p4char:setSize(152, 20)
396
397				t1p4race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team1x, team1y + 25*3,
398					function(dd) end)
399				t1p4race:setSize(152, 20)
400			end
401		elseif (GameDefinition["Map"][player]["Team"] == 2) then
402			if (GameDefinition["Map"][player]["Player"] == 1) then
403				menu:addLabel(_("~<Team 2~>"), team2x, team2y - 25, Fonts["game"], true)
404				t2p1char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team2x - 152, team2y + 25*0,
405					function(dd) end)
406				t2p1char:setSize(152, 20)
407
408				t2p1race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team2x, team2y + 25*0,
409					function(dd) end)
410				t2p1race:setSize(152, 20)
411			elseif (GameDefinition["Map"][player]["Player"] == 2) then
412				t2p2char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team2x - 152, team2y + 25*1,
413					function(dd) end)
414				t2p2char:setSize(152, 20)
415
416				t2p2race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team2x, team2y + 25*1,
417					function(dd) end)
418				t2p2race:setSize(152, 20)
419			elseif (GameDefinition["Map"][player]["Player"] == 3) then
420				menu:addLabel(_("~<Team 1~>"), team1x, team1y - 25, Fonts["game"], true)
421				t2p3char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team2x - 152, team2y + 25*2,
422					function(dd) end)
423				t2p3char:setSize(152, 20)
424
425				t2p3race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team2x, team2y + 25*2,
426					function(dd) end)
427				t2p3race:setSize(152, 20)
428			elseif (GameDefinition["Map"][player]["Player"] == 4) then
429				t2p4char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team2x - 152, team2y + 25*3,
430					function(dd) end)
431				t2p4char:setSize(152, 20)
432
433				t2p4race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team2x, team2y + 25*3,
434					function(dd) end)
435				t2p4race:setSize(152, 20)
436			end
437		end
438	end
439  end
440
441  function MapChanged()
442    mapl:setCaption(string.sub(mapname, 6))
443    mapl:adjustSize()
444
445    descriptionl:setCaption(mapinfo.description ..
446      " (" .. mapinfo.w .. " x " .. mapinfo.h .. ")")
447    descriptionl:adjustSize()
448
449  end
450
451  GetMapInfo(mapname)
452  MapChanged()
453
454  menu:run()
455end
456
457
458StartCustomGame_Lua()
459
460RunSinglePlayerGameMenu()
461