SmallTown5 - Germanunkol/trAInsported GitHub Wiki

Challenge-map SmallTown5

-- SmallTown5.lua - HaJo Gurt

--[[
Generate challenge-maps for trAInsported (maps for a single AI)

History:
Original from: ExampleChallenge.lua, SmallTown4.lua
2014-02-15	first version
2014-02-16	fixed map
2014-02-17 	fixed psx-gen: minimum travel-distance,
		directDistance(), manhattanDistance(), nearbyRail()
2014-02-18	first version: Challenge-5

For documentation see:
* http://trainsportedgame.no-ip.org/maps.php
* https://github.com/Germanunkol/trAInsported/blob/master/MakingMaps.md
]]--

local ch = {}

--ch.name = "Challenge-5"
ch.version = "4"		-- req. version of trAInsported

ch.maxTrains  =  1
ch.startMoney = 25	-- 1 train costs 25

-- ##### ##### ##### ##### ##### ##### ##### ##### #####

--[[ -- mapdata from SmallTown1:
local mapAuthor = "Germanunkol"
local mapDescr  = "SmallTown1"

ch.name = "Challenge1"
ch.maxTrains, ch.startMoney = 1, 25

local maxX, maxY = 10,  7
local _map1 =
	"CCCCh--___" ..
	"CHHChh-___" ..
	"CHHC---___" ..
	"CCCC#-CCCC" ..
	"__--#-CWHC" ..
	"__--##CSHC" ..
	"__--__CCCC"

local passengersRemaining, passengersCreated  = 4, false
local maxTime = 180

local startupMessage = "Welcome to SmallTown!\n"..
	"Transport the people around in Smalltown.\n"..
	"You have " .. maxTime .. " seconds!"
]]--

--[[ -- mapdata from SmallTown4:
local mapAuthor = "maxxst  2013-07"
	-- from https://github.com/Germanunkol/trAInsported/pull/56
local mapDescr  = "SmallTown4"

ch.name = "Challenge4"
ch.maxTrains, ch.startMoney = 1, 25

local maxX, maxY = 10,  7
local _map =
	"CH-CCC-CCC" ..
	"CHSC-C-CWC" ..
	"CHCCCC-CCC" ..
	"C-C--C---C" ..
	"CCccCC-C-C" ..
	"C___--CCHC" ..
	"CCCCCCCCCC"

local passengersRemaining, passengersCreated = 8, false
local maxTime = 360

local startupMessage = "Welcome to SmallTown!\n"..
	"Transport the people around in Smalltown.\n"..
	"You have " .. maxTime .. " seconds!"
]]--


-- ##### ##### ##### ##### ##### ##### ##### ##### #####

--[[ ]]--
local mapAuthor = "hajo4  2014-02"
local mapDescr  = "Inner town, outer ring. (The distance-measuring challenge :)"

ch.name = "Challenge-5"
ch.maxTrains, ch.startMoney = 2, 25

local maxX, maxY = 11, 11
local _map =
	"C-CCCCCCCCC" ..
	"C-c-_c_--_C" ..
	"Ccc.Hc1.ccC" ..
	"C-..Hc1..-C" ..
	"C-hhHS111-C" ..
	"CcccS-ScccC" ..
	"C-hhHSHHH-C" ..
	"C-..HcH---C" ..
	"Ccc.HcH-ccC" ..
	"C---_c_-c-C" ..
	"CCCCCCCCC-C"

--     ....+....1....+....2

local passengersRemaining, passengersCreated  = 8, false
local maxTime = 600

local startupMessage = "Welcome to CenterTown!\n"..
	"Transport the people around in Centertown.\n"..
	"You have " .. maxTime .. " seconds!"
--[[
	"CCCCCCCCCCC" ..
	"C-c-_c_--cC" ..
	"C-c.HcX..-C" ..
	"C-..Hcx..-C" ..
	"C-hhHSh..-C" ..
	"CcccS-ScccC" ..
	"C-hhHSHHH-C" ..
	"C-..HcH---C" ..
	"Ccc.HcH-ccC" ..
	"--c-_c_-c-C" ..
	"CCCCCCCCC-C"
]]--

--local startTime = 0

-- ##### ##### ##### ##### ##### ##### ##### ##### #####

-- create a new, empty map with known dimensions:
ch.map = challenges.createEmptyMap(maxX, maxY)

-- fill in mapdata:

local x,y,b = 1,1,0
for p = 1, string.len(_map) do
	s = string.sub(_map, p,p)
	if s == "#" then s = "C" end
	if s == "c" then s = "C" end
	if s == "h" then s = "H" end
	if s == "." then s = " " end
	if s == "-" then s = " " end
	if s == "_" then s = " " end

	if s == "W" then s = "STORE" end
	-- 3x3 buildings:
	if s == "1" then s = "H" end
	if s == "X" then s = "HOUSE_1_LARGE_11" end
	if s == "x" then s = "HOUSE_1_LARGE_12" end

	if s == "Y" then s = "HOUSE_2_LARGE_11" end
	if s == "y" then s = "HOUSE_2_LARGE_12" end

	if s == "Z" then s = "HOUSE_3_LARGE_11" end
	if s == "z" then s = "HOUSE_3_LARGE_12" end
	if s == "3" then s = "HOUSE_3_LARGE_21" end

	if s == "Q" then s = "HOUSE_4_LARGE_11" end
	if s == "q" then s = "HOUSE_4_LARGE_12" end
	if s == "4" then s = "HOUSE_4_LARGE_21" end
	
	if s ~= " " then
	      ch.map[x][y] = s
		b=b+1
	end
	if x >= maxX then
		x=1
		y=y+1
	else
		x=x+1
	end
end
print( "Map ".. ch.name, "done:", maxX.."x"..maxY, b )


function directDistance(x1,y1, x2,y2)
	return math.sqrt( (x1-x2)^2 + (y1-y2)^2 )
end
function manhattanDistance(x1, y1, x2, y2)
    return math.abs(x1-x2) + math.abs(y1-y2)
end

function nearbyRail( x,y )
-- check where nearest rail is:
-- 0: tile at x,y is rail
-- 1: adjacent tile has rail (N,E,S,W)
-- 2: diagonally adjacent tile has rail (NE,NW,SE,SW)
-- 3: no rail in range

	if     ch.map[ x ][ y ] == "C" then return 0

	elseif ch.map[x+1][y  ] == "C" then return 1
	elseif ch.map[x-1][y  ] == "C" then return 1
	elseif ch.map[x  ][y+1] == "C" then return 1
	elseif ch.map[x  ][y-1] == "C" then return 1

	elseif ch.map[x+1][y+1] == "C" then return 2
	elseif ch.map[x+1][y-1] == "C" then return 2
	elseif ch.map[x-1][y+1] == "C" then return 2
	elseif ch.map[x-1][y-1] == "C" then return 2

	else return 3 end
end


function ch.start()
	challenges.setMessage(startupMessage)
end

function ch.update(time)
-- every second, update the messagebox for remaining time etc.
-- and keep track of / generate passengers

local destX,destY, r,d, x,y, x1,y2, x1,y2

	if time > 3 and not passengersCreated then

		-- destX = 9	-- Location of warehouse as destination
		-- destY = 2
		-- passenger.new( x, y, destX, destY )
		-- passenger.new( 1, math.random(4) , 10, math.random(4) + 3 )

	--Todo: 
	--provide several alternatives for creating passengers
	--make some VIPs

		--local destX,destY = 9,2	-- Location of destination (warehouse)
		for i = 1, passengersRemaining do
			r = 9
			while r>0 do
				--x = math.random(destX-1,destX+1)  -- area around store
				--y = math.random(destY-1,destY+1)
				x = math.random(maxX)
				y = math.random(maxY)
				r = nearbyRail( x,y )
				print( "# nearbyRail: "..x,y, r )
			end
			x2,y2 = x,y

			d,r = 0,9
			while (d<1.5) or (r>0) do	-- ensure both spots are near rails,
								-- and some minimum travel-distance apart
				x = math.random(maxX)
				y = math.random(maxY)
				d = directDistance( x,y, x2,y2 )
				r = nearbyRail( x,y )
				print( "## nearbyRail: "..x,y, r,d )
			end
			x1,y1 = x,y
			passenger.new( x1,y1, x2,y2 )
			print("### New passenger #".. i..":", x1.."/"..y1, x2.."/"..y2 )
		end
		passengersCreated = true

	end

    --challenges.setStatus("Map by "..mapAuthor.."\n" ..
	challenges.setStatus("Map "..ch.name.." by "..mapAuthor.."\n" ..
		math.floor(maxTime-time) .. " seconds remaining.\n" ..
		passengersRemaining .." passengers remaining.")

	if time > maxTime then
		return "lost", "Some passengers are still missing :-("
	end
	if passengersRemaining == 0 then
		return "won", "Well done !"
	end

end

function ch.passengerDroppedOff(train, p)
	if train.tileX == p.destX and train.tileY == p.destY then		
		passengersRemaining = passengersRemaining - 1
	end
	challenges.removeMessage()	-- remove msgbox after first passenger is dropped off
end

return ch

--.
⚠️ **GitHub.com Fallback** ⚠️