view src/sim.tp @ 32:0a55ee387d69

Change grid order
author Mike Pavone <pavone@retrodev.com>
date Sat, 14 Jul 2012 23:03:04 -0700
parents f7a1daaec925
children efa82c5e95c2
line wrap: on
line source

{
	eachbyte <- :string action {
		strLen <- string byte_length:
		index <- 0
		while: {index < strLen} do: {
			element <- (string byte: index)
			action: index element
			index <- index + 1
		}
	}

	debugLog <- :str {
		os write: 2 str
	}

	makeCellTypes <- {
		typedict <- dict linear
		new <- :idStr {
			ret <- #{
				id <- (idStr byte: 0)
				str <- idStr
				isrobot <- { false }
			}
			typedict set: (ret id) ret
			ret
		}
		#{
			find <- :id {
				if: id = ("R" byte: 0) { robot: } else: {
					typedict get: id withDefault: empty
				}
			}
			wall        <- new: "#"
			empty       <- new: " "
			earth       <- new: "."
			rock        <- new: "*"
			lambda      <- new: "\\"
			closedlift  <- new: "L"
			openlift    <- new: "O"
			newline     <- new: "\n"
			robot       <- {
				#{
					id <- ("R"  byte: 0)
					str <- "R"
					isrobot <- { true }
					heldBreath <- 0
					x <- 0
					y <- 0
					move <- :cmd mine {
						writeMove <- :xPrime yPrime {
							mine setCell: xPrime yPrime self
							mine setCell: x y empty
							x <- xPrime
							y <- yPrime
						}
						writeMove: x (y + 1)
					}
				}
			}
		}
    }
#{

	cellTypes <- makeCellTypes:

	state <- #{
		new <- :in_grid in_width in_height { 
			nextGrid <- #[]
			robot <- false
			endreached <- false
			ret <- #{
				grid <- in_grid
				width <- in_width
				height <- in_height
				calcIndex <- :x y { x + y * width }
				calcX <- :index {index % width}
				calcY <- :index {index / width}
				setCell <- :x y val {
					grid set: (calcIndex: x y) val
				}
				getCell <- :x y {
					grid get: (calcIndex: x y)
				}
				updatePos <- :obj Index {
					obj x!: (calcX: Index)
					obj y!: (calcY: Index)
				}
				water <- 0
				flooding <- 0
				waterproof <- 10
				collected <- 0
				moves <- 0
				ended <- {endreached}
				doUpdate <- {
					true
				}
				advance <- :roboCmd {
					endreached <- roboCmd = "A"
					robot move: roboCmd self
					moves <- moves + 1
					doUpdate:
					self
				}
				printGrid <- {
					cur <- (grid length) - width
					col <- 0
					while: {cur >= 0} do: {
						os write: 2 ((grid get: cur) str)
						cur <- cur + 1
						col <- col + 1
						if: col = width {
							col <- 0
							cur <- cur - (width + width)
							os write: 2 "\n"
						}
					}
				}
			}
			foreach: in_grid :index el{
				nextGrid append: el
				if: (el isrobot) { 
					robot <- el
					ret updatePos: robot index
				}
			}
			ret
		}
		
		fromStr <- :str {
			strLen <- str byte_length:
			maxCol <- 0
			nl <- (cellTypes newline) id
			blank <- cellTypes empty
			lines <- #[]
			curline <- #[]
			eachbyte: str :index element {
				if: element = nl {
					col <- curline length
					maxCol <- if: col > maxCol {col} else: {maxCol}					
					lines append: curline
					curline <- #[]
				} else: {
					curline append: (cellTypes find: element)
				}
			}
			grid <- #[]
			cur <- (lines length) - 1
			while: { cur >= 0 } do: {
				curline <- (lines get: cur)
				foreach: curline :idx el {
					grid append: el
				}
				extra <- maxCol - (curline length)
				while: { extra > 0 } do: {	
					grid append: blank
					extra <- extra - 1
				}
				cur <- cur - 1
			}
			new: grid maxCol (lines length)
		}
	}

	testMoves <- {
		myStep <- 0
		{
			myStep <- myStep + 1
			if: myStep > 5 {"A"} else: {"W"}
		}
	}
	
	readFd <- :fd {
		if: fd < 0 { "" } else: {
			cur <- ""
			part <- ""
			while: { 
				part <- os read: fd 128
				part != ""
			} do: {
				cur <- cur . part
			}
			cur
		}
	}
	
	readFile <- :path {
		fd <- os open: path (os O_RDONLY)
		out <- readFd: fd
		os close: fd
		out
	}
	
	getMove <- {
		ret <- os read: 0 1
		while: {ret = "\n"} do: {
			ret <- os read: 0 1
		}
		ret
	}

	main <- :args {
		if: (args length) < 2 {
			print: "usage: sim filename\n"
		} else: {
			verbose <- true
			text <- readFile: (args get: 1)
			print: text
			os close: 1
			simState <- state fromStr: text
			derp <- simState ended:
			while: { not: (simState ended: ) } do: {
				simState advance: (getMove: )
				if: verbose {
					simState printGrid
				}
			}
		}
	}

}
}