Skip to content

Instantly share code, notes, and snippets.

@laserbat
Created July 8, 2011 08:50
Show Gist options
  • Save laserbat/1071386 to your computer and use it in GitHub Desktop.
Save laserbat/1071386 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
PHP MRPAS
Copyright (c) 2010 Dominik Marczuk
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* The name of Dominik Marczuk may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY DOMINIK MARCZUK ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DOMINIK MARCZUK BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import random
mapWidth = 80
mapHeight = 50
playerPosX = 40
playerPosY = 25
class cell:
def __init__(self):
self.fov = False
self.transparent = False
self.walkable = False
class mainmap:
def __init__ (self,w, h):
self.width = w
self.height = h
self.nbcells = w * h
self.cells = []
for i in xrange(self.nbcells + 1):
self.cells.append(cell())
def generate (self):
for i in xrange(self.nbcells + 1):
if random.randint(0,100) > 10:
self.cells[i].transparent = True
self.cells[i].walkable = True
else:
self.cells[i].transparent = False
self.cells[i].walkable = False
self.cells[i].fov = False
def displayTile(self,idx):
if self.cells[idx].walkable:
if self.cells[idx].fov == True:
c = '<img src="ground-lit.gif">'
else:
c = '<img src="ground-unlit.gif">'
else:
if self.cells[idx].fov == True:
c = '<img src="wall-lit.gif">'
else:
c = '<img src="wall-unlit.gif">'
if playerPosY * mapWidth + playerPosX == idx:
c = '<img src="pc.gif">'
print c
#the fov itself
class MRPAS:
def computeQuadrant (self,m,playerX,playerY,maxRadius,lightWalls,dx,dy):
startAngle = [None] * 1000
endAngle = [None] * 1000
#octant: vertical edge:
iteration = 1
done = False
totalObstacles = 0
obstaclesInLastLine = 0
minAngle = 0.0
x = 0
y = 0
#do while there are unblocked slopes left and the algo is within
# the map's boundaries
#scan progressive lines/columns from the PC outwards
y = playerY + dy
if y < 0 or y >= m.height:
done = True
while not done:
#process cells in the line
slopesPerCell = 1.0 / (iteration + 1);
halfSlopes = slopesPerCell * 0.5
processedCell = abs(minAngle / slopesPerCell)
minx = max(0, playerX - iteration)
maxx = min(m.width - 1, playerX + iteration)
done = True
x = playerX + (processedCell * dx)
while x >= minx and x <= maxx:
c = x + (y * m.width)
#calculate slopes per cell
visible = True
startSlope = processedCell * slopesPerCell
centreSlope = startSlope + halfSlopes
endSlope = startSlope + slopesPerCell
if obstaclesInLastLine > 0 and m.cells[int(c)].fov == False:
idx = 0
while visible and idx < obstaclesInLastLine:
if m.cells[int(c)].transparent == True:
if centreSlope > startAngle[idx] and centreSlope <\
endAngle[idx]:
visible = False
else:
if startSlope >= startAngle[idx] and endSlope <= endAngle[idx]:
visible = False;
if visible and (m.cells[c - (m.width * dy)].fov == False or\
not m.cells[c - (m.width * dy)].transparent)\
and (x - dx >= 0 and x - dx < m.width and\
(m.cells[c - (m.width * dy) - dx].fov == False\
or not m.cells[c - (m.width * dy) - dx].\
transparent)):
visible = False
idx += 1
if visible:
m.cells[int(c)].fov = True
done = False
#if the cell is opaque, block the adjacent slopes
if not m.cells[int(c)].transparent:
if minAngle >= startSlope:
minAngle = endSlope
else:
startAngle[totalObstacles] = startSlope
endAngle[totalObstacles + 1] = endSlope
if (not lightWalls):
m.cells[int(c)].fov = False
processedCell += 1
x += dx
if iteration == maxRadius:
done = True
iteration += 1
obstaclesInLastLine = totalObstacles;
y += dy
if y < 0 or y >= m.height:
done = True
if minAngle == 1.0:
done = True
#octant: horizontal edge
iteration = 1 #iteration of the algo for this octant
done = False
totalObstacles = 0
obstaclesInLastLine = 0
minAngle = 0.0
x = 0
y = 0
#do while there are unblocked slopes left and the algo is within the map's boundaries
#scan progressive lines/columns from the PC outwards
x = playerX + dx #the outer slope's coordinates (first processed line)
if x < 0 or x >= m.width:
done = True
while not done:
#process cells in the line
slopesPerCell = 1.0 / (iteration + 1)
halfSlopes = slopesPerCell * 0.5
processedCell = abs(minAngle / slopesPerCell)
miny = max(0, playerY - iteration)
maxy = min(m.height - 1, playerY + iteration);
done = True
y = playerY + (processedCell * dy)
while y >= miny and y <= maxy:
y += dy
c = x + (y * m.width)
#calculate slopes per cell
visible = True
startSlope = (processedCell * slopesPerCell)
centreSlope = startSlope + halfSlopes
endSlope = startSlope + slopesPerCell
if obstaclesInLastLine > 0 and m.cells[int(c)].fov == False:
idx = 0
while visible and idx < obstaclesInLastLine:
if m.cells[int(c)].transparent == True:
if centreSlope > startAngle[idx] and centreSlope <\
endAngle[idx]:
visible = False
else:
if startSlope >= startAngle[idx] and endSlope <= endAngle[idx]:
visible = False
if visible and (m.cells[c - dx].fov == False or\
not m.cells[c - dx].transparent) and\
(y - dy >= 0 and y - dy < m.height and (m.cells[c -\
(m.width * dy) - dx].fov == False or not m.cells[c -\
(m.width * dy) - dx].transparent)):
visible = False
idx += 1
if visible:
m.cells[int(c)].fov = True
done = False;
#if the cell is opaque, block the adjacent slopes
if not m.cells[int(c)].transparent:
if minAngle >= startSlope:
minAngle = endSlope
else:
startAngle[totalObstacles] = startSlope;
endAngle[totalObstacles + 1] = endSlope;
if not lightWalls:
m.cells[int(c)].fov = False
processedCell += 1
if iteration == maxRadius:
done = True
iteration += 1
obstaclesInLastLine = totalObstacles
x += dx
if x < 0 or x >= m.width:
done = True
if minAngle == 1.0:
done = True
def computeFov (self,m,playerX,playerY,maxRadius,lightWalls):
#first, zero the FOV map
for c in xrange(m.nbcells + 1):
m.cells[c].fov = False
#set PC's position as visible
m.cells[playerX + (playerY * m.width)].fov = True
#compute the 4 quadrants of the map
self.computeQuadrant(m, playerX, playerY, maxRadius, lightWalls, 1, 1);
self.computeQuadrant(m, playerX, playerY, maxRadius, lightWalls, 1, -1);
self.computeQuadrant(m, playerX, playerY, maxRadius, lightWalls, -1, 1);
self.computeQuadrant(m, playerX, playerY, maxRadius, lightWalls, -1, -1);
#LET'S DO IT!
m = mainmap(mapWidth,mapHeight)
m.generate();
fov = MRPAS()
print "Content-Type: text/html\n\n",
print """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>PHP MRPAS</title>
<style type="text/css">
body { font-family: monospace; background-color: black; color: white; line-height: 0; text-align: center; }
img { border: 0; padding: 0; margin: 0; }
</style>
</head>
<body>
<h1>PHP MRPAS</h1>
<h3>Hit "refresh" to generate another map.</h3>
<p>
"""
fov.computeFov(m,playerPosX,playerPosY,0, True)
for j in xrange(mapHeight):
for i in xrange(mapWidth):
m.displayTile(j * mapWidth + i)
print '<br>'
print """
</p>
</body>
</html>
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment