view src/sim.tp @ 7:1bd46f854dbb

progress on sim, celltypes as objects
author William Morgan <bill@mrgn.org>
date Sat, 14 Jul 2012 00:43:06 -0700
parents 86cdb799f950
children 66ea6fdd3fcb
line wrap: on
line source

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

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

	cellTypes <- #{
		allstr <- #[]
		allobj <- #[]
		make <- :idstr {
			ret <- #{
				id <- (idstr byte: 0)
			}
			allobj append: ret
			allstr append: idstr
			ret
		}
		find <- :idstr {
			if: idstr = "R" { robot } else: {
				index <- 0
				while: { 
					if: index < (allstr length) { 
						(allstr get: index) != idstr
					} else: {false}
				} do: {
					index <- index + 1
				}
				if: index <- (allstr length) {
					allobj get: index
				} else: {
					empty
				}
			}
		}
		wall        <- make: "#"
		empty       <- make: " "
		earth       <- make: "."
		rock        <- make: "*"
		lambda      <- make: "\\"
		closedlift  <- make: "L"
		openlift    <- make: "O"
		newline     <- make: "\n"
		robot       <- {
		#{
			id <- ("R"  byte: 0) 
			heldBreath <- 0
		}
    }

	state <- #{
		new <- :in_grid in_width in_height { 
			next_grid <- #[]
			foreach: in_grid :index el{
				dst_grid append: el
			}
			#{
				grid <- []
				width <- in_width
				height <- in_height
				robot <- cellTypes robot
				address <- :x y { x + y * width }
				setCell <- :x y cell {
					grid set: (address: x y) cell
				}
				getCell <- :x y {
					grid get: (address: x y)
				}
				water <- 0
				flooding <- 0
				waterproof <- 10
				collected <- 0
				moves <- 0
				ended <- false
				doMove <- :roboMove {
					
					robo <- doMove r
					nexty <- doMove y
					ended <- roboMove = "A"
				}
				doUpdate <- {
					updateCell <- :x, y {
						if
					}
				}
				advance <- :roboCmd {
					robot move: roboCmd
					moves <- moves + 1
					doUpdate:
					self
				}
				
			}
		}
		fromStr <- :str {
			strLen <- str byte_length:
			index <- 0
			maxRow <- 0
			curRow <- 0
			while: {index < strLen} do {
				curByte <- str byte: index
				if: curByte = (cellType newline id) {
					maxRow <- if: curRow > maxRow {curRow} else: {maxRow}
				} else: {
					curRow = curRow + 1
				}
			}
			grid <- #[ ("#" byte: 0) ("#" byte: 0) ("#" byte: 0)  (			                 "#" byte: 0) (" " byte: 0) ("#" byte: 0)  (			                 "#" byte: 0) ("#" byte: 0) ("#" byte: 0)] 
			fresh <- new: grid 3 3
		}
	}

	testMoves <- {
		myStep <- 0
		{
			print: (string: myStep)
			myStep <- myStep + 1
			if: myStep > 5 {"A"} else: {"W"}
		}
	}
	
	main <- {
		
	}

/*
	main <- {
		testInput <- "derp" 
		simState <- state fromStr: testInput
		roboMove <- "W"
		getMove <- testMoves:
		while: {playing: simState roboMove} do: {
			print: "step..."
			roboMove <- getMove:
			simState advance:
		}
	}
*/
}