view src/sim.tp @ 13:c92633098f1d

simulator now properly parses map and accepts input until A is sent to it over stdin
author Mike Pavone <pavone@retrodev.com>
date Sat, 14 Jul 2012 05:15:00 -0700
parents 6ef6dc8ab95e
children 26cfb964fe81
line wrap: on
line source

{
	true <- #{
	  if:else <- :self trueblock :elseblock {
		trueblock:
	  }
	}

	false <- #{
	  if:else <- :self trueblock :elseblock {
		elseblock:
	  }
	}

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

	makeCellTypes <- {
		allid <- #[]
		allobj <- #[]
		new <- :idstr {
			ret <- #{
				id <- (idstr byte: 0)
				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
						} else: {false}
					} 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) 
					isrobot <- { true }
					heldBreath <- 0
					move <- :cmd {
						cmd
					}
				}
			}
		}
    }
	ttrue <- true
	tfalse <- false
#{

	// utilities 
	true <- ttrue

	false <- tfalse

	// end utilities


	cellTypes <- makeCellTypes:

	state <- #{
		new <- :in_grid in_width in_height { 
			nextGrid <- #[]
			robot <- false
			endreached <- false
			foreach: in_grid :index el{
				nextGrid append: el
				if: (el isrobot) { 
					print: "found robot\n"
					robot <- el 
				} else: { true }
			}
			#{
				grid <- in_grid
				width <- in_width
				height <- in_height
				address <- :x y { x + y * width }
				setCell <- :x y val {
					grid set: (address: x y) val
				}
				getCell <- :x y {
					grid get: (address: x y)
				}
				water <- 0
				flooding <- 0
				waterproof <- 10
				collected <- 0
				moves <- 0
				ended <- {endreached}
				doUpdate <- {
					true
				}
				advance <- :roboCmd {
					endreached <- roboCmd = "A"
					robot move: roboCmd
					moves <- moves + 1
					doUpdate:
					self
				}
				
			}
		}
		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
				}
			}
			
			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
		{
			print: (string: myStep)
			myStep <- myStep + 1
			if: myStep > 5 {"A"} else: {"W"}
		}
	}
	
	readFile <- :path {
		fd <- os open: path (os O_RDONLY)
		print: "fd: " . fd . "\n"
		if: fd < 0 { "" } else: {
			cur <- ""
			part <- ""
			while: { 
				part <- os read: fd 128
				print: "read: " . part . "\n"
				part != ""
			} do: {
				cur <- cur . part
			}
			os close: fd
			cur
		}
	}
	
	getMove <- {
		os read: 0 1
	}

	main <- :args {
		if: (args length) < 2 {
			print: "usage: sim filename\n"
		} else: {
			print: (args get: 1) . "\n"
			text <- readFile: (args get: 1)
			simState <- state fromStr: text
			while: { if: (simState ended) {false} else: {true} } do: {
				print: "step...\n"
				simState advance: (getMove: )
			}
		}
	}

}
}