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--      wc2.lua - WC2 compatibility level
12--
13--      (c) Copyright 2001-2016 by Lutz Sammer, Jimmy Salmon and Kyran Jackson.
14--
15--      This program is free software; you can redistribute it and/or modify
16--      it under the terms of the GNU General Public License as published by
17--      the Free Software Foundation; either version 2 of the License, or
18--      (at your option) any later version.
19--
20--      This program is distributed in the hope that it will be useful,
21--      but WITHOUT ANY WARRANTY; without even the implied warranty of
22--      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23--      GNU General Public License for more details.
24--
25--      You should have received a copy of the GNU General Public License
26--      along with this program; if not, write to the Free Software
27--      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28--
29
30DefineRaceNames(
31   "race", {
32      "name", "human",
33      "display", _("Human"),
34      "visible"},
35   "race", {
36      "name", "orc",
37      "display", _("Orc"),
38      "visible"},
39   "race", {
40      "name", "neutral",
41      "display", _("Neutral")
42})
43
44if (OldCreateUnit == nil) then
45  OldCreateUnit = CreateUnit
46
47  local t = {
48    {"unit-town-hall", "unit-great-hall"},
49    {"unit-keep", "unit-stronghold"},
50    {"unit-castle", "unit-fortress"},
51    {"unit-peasant", "unit-peon"},
52    {"unit-elven-lumber-mill", "unit-troll-lumber-mill"},
53    {"unit-human-blacksmith", "unit-orc-blacksmith"},
54    {"unit-inventor", "unit-alchemist"},
55    {"unit-stables", "unit-ogre-mound"},
56    {"unit-church", "unit-altar-of-storms"},
57    {"unit-mage-tower", "unit-temple-of-the-damned"},
58    {"unit-gryphon-aviary", "unit-dragon-roost"},
59    {"unit-human-barracks", "unit-orc-barracks"},
60    {"unit-farm", "unit-pig-farm"},
61    {"unit-yeoman", "unit-nomad"},
62    {"unit-footman", "unit-grunt"},
63    {"unit-archer", "unit-axethrower"},
64    {"unit-ranger", "unit-berserker"},
65    {"unit-knight", "unit-ogre"},
66    {"unit-paladin", "unit-ogre-mage"},
67    {"unit-mage", "unit-death-knight"},
68    {"unit-dwarves", "unit-goblin-sappers"},
69    {"unit-ballista", "unit-catapult"},
70    {"unit-ballista-super", "unit-catapult-super"},
71    {"unit-balloon", "unit-zeppelin"},
72    {"unit-gryphon-rider", "unit-dragon"},
73    {"unit-human-watch-tower", "unit-orc-watch-tower"},
74    {"unit-human-guard-tower", "unit-orc-guard-tower"},
75    {"unit-human-cannon-tower", "unit-orc-cannon-tower"},
76    {"unit-human-guard-tower-super", "unit-orc-guard-tower-super"},
77    {"unit-human-cannon-tower-super", "unit-orc-cannon-tower-super"},
78    {"unit-caanoo-wiseman", "unit-caanoo-wiseskeleton"},
79    {"unit-human-shipyard", "unit-orc-shipyard"},
80    {"unit-human-refinery", "unit-orc-refinery"},
81    {"unit-human-foundry", "unit-orc-foundry"},
82    {"unit-human-oil-platform", "unit-orc-oil-platform"},
83    {"unit-human-oil-tanker", "unit-orc-oil-tanker"},
84    {"unit-human-submarine", "unit-orc-submarine"},
85    {"unit-human-destroyer", "unit-orc-destroyer"},
86    {"unit-battleship", "unit-ogre-juggernaught"},
87    {"unit-human-transport", "unit-orc-transport"},
88    {"unit-attack-peasant", "unit-skeleton"}
89  }
90
91  HumanEquivalent = {}
92  OrcEquivalent = {}
93
94  for i=1,table.getn(t) do
95    HumanEquivalent[t[i][2]] = t[i][1]
96    OrcEquivalent[t[i][1]] = t[i][2]
97  end
98end
99
100-- Convert a unit type to the equivalent for a different race
101function ConvertUnitType(unittype, race)
102  local equiv
103
104  if (race == "human") then
105    equiv = HumanEquivalent[unittype]
106  else
107    equiv = OrcEquivalent[unittype]
108  end
109
110  if (equiv ~= nil) then
111    return equiv
112  else
113    return unittype
114  end
115end
116
117-- Convert unit type to the player's race
118function CreateUnit(unittype, player, pos)
119  if (GameCycle ~= 0) then
120    return OldCreateUnit(unittype, player, pos)
121  end
122
123  -- Don't add any units in 1 peasant only mode
124  if (GameSettings.NumUnits == 1 and player ~= 15) then
125    return
126  end
127
128  -- Leave neutral the way it is
129  if (player == 15) then
130    return OldCreateUnit(unittype, player, pos)
131  end
132
133  if (Players[player].Type == PlayerNobody) then
134    return nil
135  end
136
137  unittype = ConvertUnitType(unittype, GetPlayerData(player, "RaceName"))
138
139  return OldCreateUnit(unittype, player, pos)
140end
141
142if (OldSetAiType == nil) then
143  OldSetAiType = SetAiType
144end
145
146function SetAiType(player, aiscript)
147	if (GameSettings.Presets[player].AIScript == "Map Default" or
148		GameSettings.Presets[player].AIScript == _("Map Default") or GameSettings.Presets[player].AIScript == "") then
149		OldSetAiType(player, aiscript)
150	else
151		OldSetAiType(player, GameSettings.Presets[player].AIScript)
152	end
153end
154
155if (OldSetPlayerData == nil) then
156  OldSetPlayerData = SetPlayerData
157end
158
159--Define Player Data.
160function SetupPlayer(player, race, ai, gold, wood, oil, x, y)
161	if (race == "man") then race = "human" end
162	SetStartView(player, x, y)
163	SetPlayerData(player, "Resources", "wood", wood)
164	SetPlayerData(player, "Resources", "gold", gold)
165	SetPlayerData(player, "Resources", "oil", oil)
166	SetPlayerData(player, "RaceName", race)
167	SetAiType(player, ai)
168	if (GameDefinition["Map"][player]["Team"] ~= nil) then
169		for index = 0, 15 do
170			if ((GameDefinition["Map"][index]["Team"] ~= nil) and (GameDefinition["Map"][player]["Team"] == GameDefinition["Map"][index]["Team"])) then
171				SetSharedVision(player, true, index)
172				SetDiplomacy(player, "allied", index)
173			else
174				SetSharedVision(player, false, index)
175				SetDiplomacy(player, "enemy", index)
176			end
177		end
178	end
179end
180
181function SetMapTeams(player, team, position)
182	GameDefinition["Map"][player]["Team"] = team
183	GameDefinition["Map"][player]["Player"] = position
184end
185
186function SpawnUnits(player, unit, x, y, width, height)
187	SpawnUnitSquare(player, unit, x, y, width, height)
188end
189
190function SpawnUnitSquare(player, unit, x, y, width, height)
191	for loopx = 0, width-1 do
192		for loopy = 0, height-1 do
193			CreateUnit(unit, player, {(x+loopx), (y+loopy)})
194		end
195	end
196end
197
198function OrderUnits(player, unit, fromx, fromy, width, height, tox, toy, order)
199	OrderUnitSquare(player, unit, fromx, fromy, width, height, tox, toy, order)
200end
201
202function OrderUnitSquare(player, unit, fromx, fromy, width, height, tox, toy, order)
203	if (order == nil) then order = "move" end
204	if (height == nil) then height = 1 end
205	if (width == nil) then width = 1 end
206	for loopx = 0, width-1 do
207		for loopy = 0, height-1 do
208			if (GetNumUnitsAt(player, unit, {fromx+loopx,fromy+loopy}, {fromx+loopx,fromy+loopy}) > 0) then
209				OrderUnit(player, unit, {fromx+loopx,fromy+loopy,fromx+loopx,fromy+loopy}, {tox+width,toy+loopy,tox+width,toy+loopy}, order)
210			end
211		end
212	end
213end
214
215function GameDefinitionSetup(name, version, revision, map, topography)
216	if (name == "initialise") then
217		GameDefinition = {}
218		GameDefinition["Briefing"] = {}
219		GameDefinition["Map"] = {}
220		GameDefinition["Results"] = {}
221		for player = 0, 15 do
222			GameDefinition["Map"][player] = {}
223		end
224		GameDefinition["Player"] = {}
225		for team = 1, 4 do
226			GameDefinition["Player"][team] = {}
227			for player = 1, 8 do
228				-- GameDefinition["Player"][1][1]["Name"] = Shane Wolfe
229				-- GameDefinition["Player"][1][1]["Race"] = Human
230				-- GameDefinition["Player"][1][1]["Position"] = 8
231				GameDefinition["Player"][team][player] = {}
232				GameDefinition["Player"][team][player]["Name"] = nil
233				GameDefinition["Player"][team][player]["Race"] = nil
234				GameDefinition["Player"][team][player]["Position"] = nil
235			end
236		end
237	end
238	if ((name == "reset") or (name == "initialise")) then
239		GameDefinition["Name"] = "Skirmish"
240		GameDefinition["Version"] = "Modern"
241		GameDefinition["Revision"] = 1
242		GameDefinition["Map"]["Name"] = "None"
243		GameDefinition["Map"]["Topography"] = "None"
244		GameDefinition["Briefing"]["Active"] = true
245		GameDefinition["Briefing"]["Width"] = 800
246		GameDefinition["Briefing"]["Height"] = 600
247		GameDefinition["Briefing"]["Objectives"] = {"No", "Objectives"}
248		GameDefinition["Briefing"]["Title"] = "No Title"
249		GameDefinition["Briefing"]["Character"] = " "
250		GameDefinition["Briefing"]["Sync"] = " "
251		GameDefinition["Results"]["Player"] = "Mythic"
252		GameDefinition["Results"]["Enemy"] = "Wild"
253		local races = {'Mythic', 'Wild'}
254		for racesCount = 1, #races do
255			GameDefinition["Results"][races[racesCount]] = {}
256			GameDefinition["Results"][races[racesCount]]["InfantryCurrent"]  = 0
257			GameDefinition["Results"][races[racesCount]]["InfantryTotal"]    = 0
258			GameDefinition["Results"][races[racesCount]]["ArtilleryCurrent"] = 0
259			GameDefinition["Results"][races[racesCount]]["ArtilleryTotal"]   = 0
260			GameDefinition["Results"][races[racesCount]]["CavalryCurrent"]   = 0
261			GameDefinition["Results"][races[racesCount]]["CavalryTotal"]     = 0
262		end
263		if (Video.Width > GameDefinition["Briefing"]["Width"]) then
264			GameDefinition["Briefing"]["X"] = (Video.Width / 2) - (GameDefinition["Briefing"]["Width"] / 2)
265		else
266			GameDefinition["Briefing"]["X"] = 0
267		end
268		if (Video.Height > GameDefinition["Briefing"]["Height"]) then
269			GameDefinition["Briefing"]["Y"] = (Video.Height / 2) - (GameDefinition["Briefing"]["Height"] / 2)
270		else
271			GameDefinition["Briefing"]["Y"] = 0
272		end
273	elseif (version ~= nil) then
274		if (revision == nil) then revision = 1 end
275		GameDefinition["Name"] = name
276		if ((GameDefinition["Version"] == "Classic") and (version == "Modern")) then else
277			GameDefinition["Version"] = version
278		end
279		GameDefinition["Revision"] = revision
280		if (map ~= nil) then
281			GameDefinition["Map"]["Name"] = map
282			if (map ~= nil) then
283			--	GameDefinition["Map"]["Path"] = path
284			end
285			if (topography ~= nil) then
286				-- Land, Coastal, Islands
287				GameDefinition["Map"]["Topography"] = topography
288			end
289		end
290	end
291end
292
293function CampaignDataSetup(name, map, variable, value)
294	if (name == nil) then
295		CampaignData = {}
296	else
297		if (map == nil) then
298			CampaignData[name] = {}
299		elseif (type(map) == "number") then
300			CampaignData[name][GameDefinition["Map"]["Name"]]["Magic"] = map
301		else
302			if (variable == nil) then
303				CampaignData[name][map] = {}
304				CampaignData[name][map]["Magic"] = 0
305			else
306				CampaignData[name][map][variable] = value
307			end
308		end
309	end
310end
311
312CampaignDataSetup()
313CampaignDataSetup("Lucas Kage")
314CampaignDataSetup("Lucas Kage", "Dunath Plains")
315GameDefinitionSetup("initialise")
316
317-- Override with game settings
318function SetPlayerData(player, data, arg1, arg2)
319  if (GameCycle ~= 0) then
320    return OldSetPlayerData(player, data, arg1, arg2)
321  end
322
323  local res = {arg2, arg2, arg2}
324
325  if (data == "RaceName") then
326    if (GameSettings.Presets[player].Race == 1) then
327        arg1 = "human"
328      elseif (GameSettings.Presets[player].Race == 2) then
329        arg1 = "orc"
330      end
331  elseif (data == "Resources") then
332    if (GameSettings.Presets[player].Type == PlayerNobody) then
333      res = {0, 0, 0}
334    elseif (GameSettings.Resources == 1) then
335      res = {2000, 1000, 1000}
336    elseif (GameSettings.Resources == 2) then
337      res = {5000, 2000, 2000}
338    elseif (GameSettings.Resources == 3) then
339      res = {10000, 5000, 5000}
340	 elseif (GameSettings.Resources == 4) then
341      res = {30000, 15000, 10000}
342    end
343    if (arg1 == "gold") then
344      arg2 = res[1]
345    elseif (arg1 == "wood") then
346      arg2 = res[2]
347    elseif (arg1 == "oil") then
348      arg2 = res[3]
349    end
350  end
351
352  OldSetPlayerData(player, data, arg1, arg2)
353
354  -- If this is 1 peasant mode add the peasant now
355  if (data == "RaceName") then
356    if (GameSettings.NumUnits == 1) then
357      if (player ~= 15 and Players[player].Type ~= PlayerNobody) then
358        local unittype = ConvertUnitType("unit-peasant", GetPlayerData(player, "RaceName"))
359        OldCreateUnit(unittype, player, {Players[player].StartPos.x, Players[player].StartPos.y})
360      end
361    end
362  end
363end
364
365if (OldDefinePlayerTypes == nil) then
366  OldDefinePlayerTypes = DefinePlayerTypes
367end
368
369function DefineCustomMapRules()
370
371end
372
373function DefinePlayerTypes(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15)
374  if (IsSkirmishClassic == true or IsNetworkGame()==true or GameSettings.NetGameType == 2 or Editor.Running==4 or GameSettings.GameType==-1 or currentCampaign ~= nil) then
375	  local p = {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15}
376	  local foundperson = false
377	  local nump = GameSettings.Opponents
378	  if (nump == 0) then nump = 15 end
379
380	  -- FIXME: should randomly pick players to use
381	  for i=1,15 do
382		if (p[i] == "person" or p[i] == "computer") then
383		  if (p[i] == "person" and foundperson == false) then
384			foundperson = true
385		  else
386			if (nump == 0) then
387			  p[i] = "nobody"
388			else
389			  nump = nump - 1
390			end
391		  end
392		end
393	  end
394	  OldDefinePlayerTypes(p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15])
395  else
396	local plrsnmb = {nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil}
397		for i=0,15 do
398			if GameSettings.Presets[i].Type == PlayerPerson then
399				plrsnmb[i+1]="person"
400			elseif GameSettings.Presets[i].Type == PlayerComputer then
401				plrsnmb[i+1]="computer"
402			elseif GameSettings.Presets[i].Type == PlayerRescuePassive then
403				plrsnmb[i+1]="rescue-passive"
404			elseif GameSettings.Presets[i].Type == PlayerRescueActive then
405				plrsnmb[i+1]="rescue-active"
406			elseif GameSettings.Presets[i].Type == PlayerNeutral then
407				plrsnmb[i+1]="nobody"
408			elseif GameSettings.Presets[i].Type == -1 then
409				plrsnmb[i+1]=nil
410			end
411		end
412		OldDefinePlayerTypes(plrsnmb[1], plrsnmb[2], plrsnmb[3], plrsnmb[4], plrsnmb[5], plrsnmb[6], plrsnmb[7], plrsnmb[8],
413			plrsnmb[9], plrsnmb[10], plrsnmb[11], plrsnmb[12], plrsnmb[13], plrsnmb[14], plrsnmb[15])
414		mapinfo.playertypes[1] = plrsnmb[1]
415		mapinfo.playertypes[2] = plrsnmb[2]
416		mapinfo.playertypes[3] = plrsnmb[3]
417		mapinfo.playertypes[4] = plrsnmb[4]
418		mapinfo.playertypes[5] = plrsnmb[5]
419		mapinfo.playertypes[6] = plrsnmb[6]
420		mapinfo.playertypes[7] = plrsnmb[7]
421		mapinfo.playertypes[8] = plrsnmb[8]
422		mapinfo.playertypes[9] = plrsnmb[9]
423		mapinfo.playertypes[10] = plrsnmb[10]
424		mapinfo.playertypes[11] = plrsnmb[11]
425		mapinfo.playertypes[12] = plrsnmb[12]
426		mapinfo.playertypes[13] = plrsnmb[13]
427		mapinfo.playertypes[14] = plrsnmb[14]
428		mapinfo.playertypes[15] = plrsnmb[15]
429	end
430end
431
432if OldLoadTileModels == nil then
433	OldLoadTileModels = LoadTileModels
434end
435
436function LoadTileModels(tileset)
437  DefineCustomMapRules()
438  if (GameCycle ~= 0) then
439    return OldLoadTileModels(tileset)
440  end
441  if (GameSettings.Tileset == nil) then
442    return OldLoadTileModels(tileset)
443  end
444  OldLoadTileModels("scripts/tilesets/" .. GameSettings.Tileset)
445end
446
447-- Called by Stratagus when unloading a mod.
448
449function CleanModGame_Lua()
450
451end
452
453-- Called by Stratagus when a game is restarted
454function StartCustomGame_Lua()
455
456end
457
458-- Called by Stratagus when a game finished
459function CleanCustomGame_Lua()
460
461end
462
463function StartModGame_Lua()
464
465end
466
467function CleanGame_Lua()
468	print("game ends")
469	ReInitAiGameData()
470	CleanCustomGame_Lua()
471	StartModGame_Lua()
472end
473