SmallTown4 - Germanunkol/trAInsported GitHub Wiki

Challenge-map SmallTown4

-- Smalltown4.lua - HaJo Gurt

--[[
Challenge-map for trAInsported (map for a single AI)


History:
Original    From: ExampleChallenge.lua / Smalltown1.lua
2014-02-15  initial version
2014-02-16  fix: create psx with different start- and destination coords
2014-02-17  directDistance() .. nearbyRail()


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


local ch = {}

ch.version = "3"		-- req. version of trAInsported

ch.maxTrains  =  2
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 = 10
local maxY =  7
local _map1 =
	"CCCCh--___" ..
	"CHHChh-___" ..
	"CHHC---___" ..
	"CCCC#-CCCC" ..
	"__--#-CWHC" ..
	"__--##CSHC" ..
	"__--__CCCC"

local passengersRemaining = 4
local passengersCreated   = false
local maxTime   = 360
]]--

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 = 10
local maxY =  7
local _map =
	"CH-CCC-CCC" ..
	"CHSC-C-CWC" ..
	"CHCCCC-CCC" ..
	"C-C--C---C" ..
	"CCccCC-C-C" ..
	"C___--CCHC" ..
	"CCCCCCCCCC"
-- --  ....+....1....+....2

local passengersRemaining = 4
local passengersCreated   = false
local maxTime   = 360

local startupMessage = "Welcome to Smalltown!\n"..
	"Your job is to move all the people in Smalltown\n"..
	"to the store in the top right corner within " ..
	maxTime .. " seconds!"

--local startTime = 0

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

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

-- fill in mapdata:
x=1
y=1
t=0
for p = 1, string.len(_map) do
	s = string.upper( string.sub(_map, p,p) )
	if s == "#" then s = "C" end
	if s == "." then s = "_" end
	if s == "-" then s = "_" end
	if s == "_" then s = " " end
	if s == "W" then s = "STORE" end

	if s ~= " " then
	      ch.map[x][y] = s
		t=t+1
	end
	if x >= maxX then
		x=1
		y=y+1
	else
		x=x+1
	end
end
print( "Map", ch.name, "done:", maxX,maxY, t )


function directDistance(x1,y1, x2,y2)
	d=math.sqrt( (x1-x2)^2 + (y1-y2)^2 )
	return d
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)
local destX,destY, r,d, x,y, x1,y1, x2,y2

-- Todo: 
-- create some VIPs (e.g. one for each AI)

	if time > 3 and not passengersCreated then
		destX = 9	-- Location of store/warehouse as destination
		destY = 2
		-- passenger.new( x, y, destX, destY )
		-- passenger.new( 1, math.random(4) , 10, math.random(4) + 3 )

		for i = 1, passengersRemaining do
			r = 9
			while r>0 do
				--x = math.random(maxX)
				--y = math.random(maxY)
				x = math.random(destX-1,destX+1)  -- area around store
				y = math.random(destY-1,destY+1)
				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 on 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 shoppers still missing..."
	end
	if passengersRemaining == 0 then
		return "won", "Well done !"
	end

end

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

return ch

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