-- ======================================================================= -- Test script for debugging. -- ======================================================================= include "scripting/infrastructure.lua" push_textdomain("tribes") function switch_player(plr_number1, plr_number2) local game = wl.Game() local player = game.players[plr_number1] player:switchplayer(plr_number2) end function observer_mode(plr_number) local game = wl.Game() local player = game.players[plr_number] player.see_all = true end function play_mode(plr_number) local game = wl.Game() local player = game.players[plr_number] player.see_all = false end function peace_mode(plr_number1, plr_number2) local game = wl.Game() local player1 = game.players[plr_number1] local player2 = game.players[plr_number2] player1:set_attack_forbidden(plr_number2, true) player2:set_attack_forbidden(plr_number1, true) player1.team = plr_number1 player2.team = plr_number1 end function war_mode(plr_number1, plr_number2) local game = wl.Game() local player1 = game.players[plr_number1] local player2 = game.players[plr_number2] player1:set_attack_forbidden(plr_number2, false) player2:set_attack_forbidden(plr_number1, false) player1.team = plr_number1 player2.team = plr_number2 end function allow_building(plr_number, bld_name) local game = wl.Game() local player = game.players[plr_number] if bld_name == "all" then player:allow_buildings(bld_name) else player:allow_buildings{bld_name} end end function forbid_building(plr_number, bld_name) local game = wl.Game() local player = game.players[plr_number] if bld_name == "all" then player:forbid_buildings(bld_name) else player:forbid_buildings{bld_name} end end function allow_worker(plr_number, wrk_name) local game = wl.Game() local player = game.players[plr_number] if wrk_name == "all" then player:allow_workers(wrk_name) else player:allow_workers{wrk_name} end end function forbid_worker(plr_number, wrk_name) local game = wl.Game() local player = game.players[plr_number] if wrk_name == "all" then player:forbid_workers(wrk_name) else player:forbid_workers{wrk_name} end end function start_expedition(plr_number) local game = wl.Game() local player = game.players[plr_number] local tribe = player.tribe local ports = player:get_buildings(tribe.name .. "_port") for i, port in ipairs(ports) do port:start_expedition() end end function remove_object(startx, starty) local game = wl.Game() local map = game.map map:get_field(startx, starty).immovable:remove() end function destroy_object(startx, starty) local game = wl.Game() local map = game.map map:get_field(startx, starty).immovable:destroy() end function place_object(startx, starty, objectname) local game = wl.Game() local map = game.map map:place_immovable(objectname, map:get_field(startx, starty)) end function place_objects(startx, starty, radius, objectname, objectcount) local game = wl.Game() local map = game.map local field = map:get_field(startx, starty) local fields = field:region(radius) local idx while #fields > 0 and objectcount > 0 do local idx = math.random(#fields) local f = fields[idx] if not f.immovable then place_object(f.x, f.y, objectname) objectcount = objectcount - 1 end table.remove(fields, idx) end end function place_flag(plr_number, startx, starty) local game = wl.Game() local map = game.map local player = game.players[plr_number] player:place_flag(map:get_field(startx, starty)) end function remove_flag(startx, starty) local game = wl.Game() local map = game.map if (map:get_field(startx, starty).immovable.descr.type_name == "flag") then remove_object(startx, starty) end end function place_road(startx, starty, cmd) local game = wl.Game() local map = game.map local startflag = map:get_field(startx, starty).immovable local player = startflag.owner local roadtype = "normal" if cmd:sub(-1) ~= "|" then cmd = cmd .. "|" end moves = {} for m in cmd:gmatch("%a+[,|]") do moves[#moves+1] = m:sub(1,-2) if(m:sub(-1) == '|') then --moves[#moves+1] = true -- Force the road r = player:place_road(roadtype, startflag, table.unpack(moves)) startflag = r.end_flag moves = {} end end end function remove_road(startx, starty) local game = wl.Game() local map = game.map if (map:get_field(startx, starty).immovable.descr.type_name == "road") then remove_object(startx, starty) end end function find_needed_flag_on_road(center_field, player, radius) for f_idx, field in ipairs(center_field:region(radius)) do if player == field.owner and field.buildable and field.has_roads == true then return field end end return nil end function connect_road(startx, starty, targetx, targety) local game = wl.Game() local map = game.map local startfield = map:get_field(startx, starty) local startflag = startfield.immovable local player = startflag.owner local roadtype = "normal" local diffx = targetx - startx local diffy = targety - starty while not ((startfield.x == targetx) and (startfield.y == targety)) do startx = startfield.x starty = startfield.y diffx = targetx - startx diffy = targety - starty if (diffx > 0) and (diffy > 0) then road = player:place_road(roadtype, startflag, "br", "br") startflag = road.end_flag startfield = startflag.fields[1] end if (diffx > 0) and (diffy == 0) then road = player:place_road(roadtype, startflag, "r", "r") startflag = road.end_flag startfield = startflag.fields[1] end if (diffx > 0) and (diffy < 0) then road = player:place_road(roadtype, startflag, "tr", "tr") startflag = road.end_flag startfield = startflag.fields[1] end if (diffx < 0) and (diffy > 0) then road = player:place_road(roadtype, startflag, "bl", "bl") startflag = road.end_flag startfield = startflag.fields[1] end if (diffx < 0) and (diffy == 0) then road = player:place_road(roadtype, startflag, "l", "l") startflag = road.end_flag startfield = startflag.fields[1] end if (diffx < 0) and (diffy < 0) then road = player:place_road(roadtype, startflag, "tl", "tl") startflag = road.end_flag startfield = startflag.fields[1] end if (diffx == 0) and (diffy > 0) then road = player:place_road(roadtype, startflag, "br", "bl") startflag = road.end_flag startfield = startflag.fields[1] end if (diffx == 0) and (diffy < 0) then road = player:place_road(roadtype, startflag, "tr", "tl") startflag = road.end_flag startfield = startflag.fields[1] end end end function place_building(plr_number, startx, starty, radius, buildingname) local game = wl.Game() local map = game.map local field = map:get_field(startx, starty) local fields = field:region(radius) local player = game.players[plr_number] place_building_in_region(player, buildingname, fields) end function replace_building(startx, starty, buildingname) local game = wl.Game() local map = game.map local player = map:get_field(startx, starty).owner if (map:get_field(startx, starty).immovable.descr.type_name == "flag") then remove_object(startx, starty-1) -- Remove existing building remove_object(startx-1, starty-1) -- Remove existing building else remove_object(startx, starty) -- Remove existing building end place_building_in_region(player, buildingname, map:get_field(startx, starty):region(1)) -- Place new one end function replace_all_buildings(plr_number, buildingname, buildingname2) local game = wl.Game() local map = game.map local player = game.players[plr_number] for startx = 0, map.width - 1 do for starty = 0, map.height - 1 do if map:get_field(startx, starty).immovable and map:get_field(startx, starty).immovable.owner == player then if map:get_field(startx, starty).immovable.descr.type_name == buildingname or map:get_field(startx, starty).immovable.descr.name == buildingname then replace_building(startx, starty, buildingname2) end end end end end function remove_building(startx, starty) local game = wl.Game() local map = game.map if (map:get_field(startx, starty).immovable.descr.type_name == "productionsite") then remove_object(startx, starty) end if (map:get_field(startx, starty).immovable.descr.type_name == "trainingsite") then remove_object(startx, starty) end if (map:get_field(startx, starty).immovable.descr.type_name == "militarysite") then remove_object(startx, starty) end if (map:get_field(startx, starty).immovable.descr.type_name == "warehouse") then remove_object(startx, starty) end if (map:get_field(startx, starty).immovable.descr.type_name == "market") then remove_object(startx, starty) end end function remove_all_buildings(plr_number, buildingname) local game = wl.Game() local player = game.players[plr_number] for i, plr in ipairs(game.players) do for j, tbuilding in ipairs(plr.tribe.buildings) do for k, building in ipairs(player:get_buildings(tbuilding.name)) do if tbuilding.type_name == buildingname or tbuilding.name == buildingname or plr.tribe.name == buildingname then building:remove() end end end end end function place_headquarters(plr_number, startx, starty) local game = wl.Game() local player = game.players[plr_number] local tribe = player.tribe local hqname = tribe.name .. "_headquarters" place_building(plr_number, startx, starty, 1, hqname) end function place_port(plr_number, startx, starty) local game = wl.Game() local player = game.players[plr_number] local tribe = player.tribe local ptname = tribe.name .. "_port" place_building(plr_number, startx, starty, 0, ptname) end function warehouse_worker_policy(startx, starty, workername, policiename) local game = wl.Game() local map = game.map local player = map:get_field(startx, starty).owner local tribe = player.tribe local field = map:get_field(startx, starty).immovable for j, tbuilding in ipairs(tribe.buildings) do for k, building in ipairs(player:get_buildings(tbuilding.name)) do if building.descr.type_name == "warehouse" and building == field then building:set_warehouse_policies(workername, policiename) end end end end function warehouse_ware_policy(startx, starty, warename, policiename) local game = wl.Game() local map = game.map local player = map:get_field(startx, starty).owner local tribe = player.tribe local field = map:get_field(startx, starty).immovable for j, tbuilding in ipairs(tribe.buildings) do for k, building in ipairs(player:get_buildings(tbuilding.name)) do if building.descr.type_name == "warehouse" and building == field then building:set_warehouse_policies(warename, policiename) end end end end function warehouse_ware_count(startx, starty, warename, warecount) local game = wl.Game() local map = game.map local player = map:get_field(startx, starty).owner local tribe = player.tribe local field = map:get_field(startx, starty).immovable for j, tbuilding in ipairs(tribe.buildings) do for k, building in ipairs(player:get_buildings(tbuilding.name)) do if building.descr.type_name == "warehouse" and building == field then building:set_wares(warename, warecount) end end end end function set_warehouse_remove_all(startx, starty) local game = wl.Game() local map = game.map local player = map:get_field(startx, starty).owner local tribe = player.tribe for i, ware in ipairs(tribe.wares) do warehouse_ware_policy(startx, starty, ware.name, "remove") end end function set_warehouse_dontstock_all(startx, starty) local game = wl.Game() local map = game.map local player = map:get_field(startx, starty).owner local tribe = player.tribe for i, ware in ipairs(tribe.wares) do warehouse_ware_policy(startx, starty, ware.name, "dontstock") end end function set_warehouse_prefer_all(startx, starty) local game = wl.Game() local map = game.map local player = map:get_field(startx, starty).owner local tribe = player.tribe for i, ware in ipairs(tribe.wares) do warehouse_ware_policy(startx, starty, ware.name, "prefer") end end function reset_warehouse_policy(startx, starty) local game = wl.Game() local map = game.map local player = map:get_field(startx, starty).owner local tribe = player.tribe for i, ware in ipairs(tribe.wares) do warehouse_ware_policy(startx, starty, ware.name, "normal") end end function set_ware(plr_number, warename, warecount) local player = wl.Game().players[plr_number] local tribe = player.tribe for i, building in ipairs(tribe.buildings) do -- restock warehouses if building.type_name == "warehouse" then for i, building in ipairs(player:get_buildings(building.name)) do building:set_wares(warename, warecount) end end end end function cheat_wares(plr_number) local MAX_UNDEFINED_WARES = 16 local player = wl.Game().players[plr_number] local tribe = player.tribe for i, building in ipairs(tribe.buildings) do -- restock warehouses if building.type_name == "warehouse" then for i, building in ipairs(player:get_buildings(building.name)) do for i, ware in ipairs(tribe.wares) do if building:get_wares(ware.name) < MAX_UNDEFINED_WARES then building:set_wares(ware.name, MAX_UNDEFINED_WARES) end end end end end end -- following functions are only for debugging europeans tribe -- function allow_militarysites(plr_number) local game = wl.Game() local player = game.players[plr_number] local tribe = player.tribe if tribe.name == "europeans" then player:allow_buildings{"europeans_guardhouse", "europeans_tower_small", "europeans_blockhouse", "europeans_sentry"} player:allow_buildings{"europeans_barrier", "europeans_outpost", "europeans_advanced_barrier"} player:allow_buildings{"europeans_tower", "europeans_tower_high", "europeans_advanced_tower"} player:allow_buildings{"europeans_castle", "europeans_fortress", "europeans_advanced_castle"} end end function forbid_militarysites(plr_number) local game = wl.Game() local player = game.players[plr_number] local tribe = player.tribe if tribe.name == "europeans" then player:forbid_buildings{"europeans_guardhouse", "europeans_tower_small", "europeans_blockhouse", "europeans_sentry"} player:forbid_buildings{"europeans_barrier", "europeans_outpost", "europeans_advanced_barrier"} player:forbid_buildings{"europeans_tower", "europeans_tower_high", "europeans_advanced_tower"} player:forbid_buildings{"europeans_castle", "europeans_fortress", "europeans_advanced_castle"} end end function allow_trainingssites(plr_number) local game = wl.Game() local player = game.players[plr_number] local tribe = player.tribe if tribe.name == "europeans" then player:allow_buildings{"europeans_guardhall", "europeans_dungeon"} player:allow_buildings{"europeans_trainingscamp_basic", "europeans_trainingscamp_normal", "europeans_trainingscamp_advanced"} player:allow_buildings{"europeans_labyrinth", "europeans_arena", "europeans_colosseum"} end end function forbid_trainingssites(plr_number) local game = wl.Game() local player = game.players[plr_number] local tribe = player.tribe if tribe.name == "europeans" then player:forbid_buildings{"europeans_guardhall", "europeans_dungeon"} player:forbid_buildings{"europeans_trainingscamp_basic", "europeans_trainingscamp_normal", "europeans_trainingscamp_advanced"} player:forbid_buildings{"europeans_labyrinth", "europeans_arena", "europeans_colosseum"} end end function allow_normal_buildings(plr_number) local game = wl.Game() local player = game.players[plr_number] local tribe = player.tribe if tribe.name == "europeans" then player:allow_buildings{"europeans_well_normal", "europeans_lumberjacks_house_normal", "europeans_foresters_house_normal", "europeans_quarry_normal", "europeans_fishers_house_normal", "europeans_hunters_house_normal", "europeans_scouts_house_normal"} player:allow_buildings{"europeans_sawmill_normal", "europeans_lumberjacks_house_normal", "europeans_charcoal_kiln_normal", "europeans_stonemasons_house", "europeans_mill_normal", "europeans_tavern_level_3", "europeans_brewery_normal", "europeans_smelting_works_normal", "europeans_smithy_level_4"} player:allow_buildings{"europeans_recruitement_center_normal", "europeans_farm_level_2", "europeans_sheepfarm", "europeans_weaving_mill_normal", "europeans_coalmine_level_3", "europeans_ironmine_level_3", "europeans_goldmine_level_3"} end end function forbid_normal_buildings(plr_number) local game = wl.Game() local player = game.players[plr_number] local tribe = player.tribe if tribe.name == "europeans" then player:forbid_buildings{"europeans_well_normal", "europeans_lumberjacks_house_normal", "europeans_foresters_house_normal", "europeans_quarry_normal", "europeans_fishers_house_normal", "europeans_hunters_house_normal", "europeans_scouts_house_normal"} player:forbid_buildings{"europeans_sawmill_normal", "europeans_lumberjacks_house_normal", "europeans_charcoal_kiln_normal", "europeans_stonemasons_house", "europeans_mill_normal", "europeans_tavern_level_3", "europeans_brewery_normal", "europeans_smelting_works_normal", "europeans_smithy_level_4"} player:forbid_buildings{"europeans_recruitement_center_normal", "europeans_farm_level_2", "europeans_sheepfarm", "europeans_weaving_mill_normal", "europeans_coalmine_level_3", "europeans_ironmine_level_3", "europeans_goldmine_level_3"} end end function allow_advanced_buildings(plr_number) local game = wl.Game() local player = game.players[plr_number] local tribe = player.tribe if tribe.name == "europeans" then player:allow_buildings{"europeans_well_advanced", "europeans_lumberjacks_house_advanced", "europeans_foresters_house_advanced", "europeans_quarry_advanced", "europeans_fishers_house_advanced", "europeans_hunters_house_advanced", "europeans_scouts_house_advanced"} player:allow_buildings{"europeans_sawmill_advanced", "europeans_lumberjacks_house_advanced", "europeans_charcoal_kiln_advanced", "europeans_stonemasons_house", "europeans_mill_advanced", "europeans_inn_basic", "europeans_brewery_advanced", "europeans_smelting_works_advanced", "europeans_smithy_level_7"} player:allow_buildings{"europeans_recruitement_center_advanced", "europeans_farm_level_3", "europeans_sheepfarm", "europeans_weaving_mill_advanced", "europeans_coalmine_level_5", "europeans_ironmine_level_5", "europeans_goldmine_level_5"} end end function forbid_advanced_buildings(plr_number) local game = wl.Game() local player = game.players[plr_number] local tribe = player.tribe if tribe.name == "europeans" then player:forbid_buildings{"europeans_well_advanced", "europeans_lumberjacks_house_advanced", "europeans_foresters_house_advanced", "europeans_quarry_advanced", "europeans_fishers_house_advanced", "europeans_hunters_house_advanced", "europeans_scouts_house_advanced"} player:forbid_buildings{"europeans_sawmill_advanced", "europeans_lumberjacks_house_advanced", "europeans_charcoal_kiln_advanced", "europeans_stonemasons_house", "europeans_mill_advanced", "europeans_inn_basic", "europeans_brewery_advanced", "europeans_smelting_works_advanced", "europeans_smithy_level_7"} player:forbid_buildings{"europeans_recruitement_center_advanced", "europeans_farm_level_3", "europeans_sheepfarm", "europeans_weaving_mill_advanced", "europeans_coalmine_level_5", "europeans_ironmine_level_5", "europeans_goldmine_level_5"} end end -- following functions could produce memory leak - dont use it, if not necessary! -- function cheat_soldiers(plr_number) local MAX_UNDEFINED_SOLDIERS = 8 local SOLDIER_STATS_NOVICE = { 0, 0, 0, 0 } local player = wl.Game().players[plr_number] local tribe = player.tribe -- retrieve best soldier stats local soldier_stats = SOLDIER_STATS_NOVICE for i, worker in ipairs(tribe.workers) do if worker.type_name == "soldier" then soldier_stats = { worker.max_health_level, worker.max_attack_level, worker.max_defense_level, worker.max_evade_level } end end for i, building in ipairs(tribe.buildings) do -- restaff warehouses if building.type_name == "warehouse" then for i, building in ipairs(player:get_buildings(building.name)) do for i, worker in ipairs(tribe.workers) do if worker.type_name == "soldier" then building:set_soldiers(soldier_stats, MAX_UNDEFINED_SOLDIERS) else building:set_workers(worker.name, MAX_UNDEFINED_SOLDIERS) end end end end end end pop_textdomain()