view src/sim.tp @ 28:7a274d6026c8

Use built-in true and false.
author Mike Pavone <pavone@retrodev.com>
date Sat, 14 Jul 2012 19:33:35 -0700
parents a224dc43877f
children d9f1a063a466
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 <- {
		allid <- #[]
		allobj <- #[]
		new <- :idStr {
			ret <- #{
				id <- (idStr byte: 0)
				str <- idStr
				isrobot <- { false }
			}
			allobj append: ret
			allid append: (ret id)
			ret
		}
		#{
			find <- :id {
				if: id = ("R" byte: 0) { robot: } else: {
					index <- 0
					while: { 
						if: index < (allid length) { 
							(allid get: index) != id
						}
					} do: {
						index <- index + 1
					}
					if: index < (allid length) {
						allobj get: index
					} else: {
						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 <- {
					grid foreach: :index value {
						os write: 2 (value str)
						if: index % width = width - 1 {
							os write: 2 "\n"
						}
					}
				}				
			}
			foreach: in_grid :index el{
				nextGrid append: el
				if: (el isrobot) { 
					robot <- el
					ret updatePos: robot index
				} else: { true }
			}
			ret
		}
		fromStr <- :str {
			strLen <- str byte_length:
			maxCol <- 0
			col <- 0
			rows <- 0
			nl <- (cellTypes newline) id
			blank <- cellTypes empty
			eachbyte: str :index element {
				if: element = nl {
					maxCol <- if: col > maxCol {col} else: {maxCol}					
					col <- 0
					rows <- rows + 1
				} else: {
					col <- col + 1
				}
			}
			col <- 0
			grid <- #[]
			eachbyte: str :index element {
				if: element = nl {
					// add spaces
					while: { col < maxCol } do: {
						grid append: blank
						col <- col + 1
					}
					col <- 0
				} else: {
					grid append: (cellTypes find: element)
					col <- col + 1
				}
			}
			new: grid maxCol rows
		}
	}

	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 <- {
		os read: 0 1
	}

	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: { if: (simState ended: ) {false} else: {true} } do: {
				simState advance: (getMove: )
				if: verbose {
					simState printGrid
				}
			}
		}
	}

}
}