• Lumen Armiger
Light Bringer
  • Lumen Armiger

Dungeon Builder

import sqlite3 as SQLver
import sys
import random
import os

random.seed()

def Check_dir(current,mod):
    if ROOM[current] + mod in ROOM:
        result = ROOM[current] + mod
    else:
        result = 0
    return result

def Random_value(low,high):
    result = random.randint(low,high)
    return result

GCenter = [23,24,25,33,34,35,43,44,45]        # Center grid    
GUL = [1,2,3,11,12,13,14,21,22,31]            # Upper Left
GLL = [32,41,42,51,52,53,61,62,63,64]        # Lower Left
GUR = [4,5,6,7,15,16,17,26,27,36]            # Upper Right
GLR = [37,46,47,54,55,56,57,65,66,67]        # Lower Right
Static_grid = [1,2,3,4,5,6,7,11,12,13,14,15,16,17,21,22,23,24,25,26,27,31,32,33,34,35,36,37,
41,42,43,44,45,46,47,51,52,53,54,55,56,57,61,62,63,64,65,66,67] # Used for mapping
ROOM = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]  # 25 rooms
RNorth = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]    # Rooms north of ROOM
RSouth = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]    # South
REast = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]        # East
RWest = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]        # West

#############################################
connection = SQLver.connect("TextAdventure.db")
with connection: 
    COM = connection.cursor()

############# FOR TESTING ###############
COM.execute("drop table if exists Dungeon")
############# FOR TESTING ###############

random.shuffle(GCenter)        # Grid divided into five areas to spread out the rooms
random.shuffle(GUL)
random.shuffle(GLL)
random.shuffle(GUR)
random.shuffle(GLR)

count = 0    # counter
while count <= 24:
    if count < 5:
        ROOM[count] = GCenter[count]
    elif count > 4 and count < 10:
        ROOM[count] = GUL[count-5]
    elif count > 9 and count < 15:
        ROOM[count] = GLL[count-10]
    elif count > 14 and count < 20:
        ROOM[count] = GUR[count-15]
    else:
        ROOM[count] = GLR[count-20]
    count+=1

#############################################
        
count = 0
while count <= 24:
    
    n = Check_dir(count,-10)
    if n != 0:
        RNorth[count] = n
    else:
        n = Check_dir(count,-20)    
        if n != 0:
            RNorth[count] = n
        else:
            n = Check_dir(count,-30)
            if n != 0:
                RNorth[count] = n

    s = Check_dir(count,10)
    if s != 0:
        RSouth[count] = s
    else:
        s = Check_dir(count,20)    
        if s != 0:
            RSouth[count] = s
        else:
            s = Check_dir(count,30)
            if s != 0:
                RSouth[count] = s

    e = Check_dir(count,1)
    if e != 0:
        REast[count] = e
    else:
        e = Check_dir(count,2)    
        if e != 0:
            REast[count] = e
        else:
            e = Check_dir(count,3)
            if e != 0:
                REast[count] = e

    w = Check_dir(count,-1)
    if w != 0:
        RWest[count] = w
    else:
        w = Check_dir(count,-2)    
        if w != 0:
            RWest[count] = w
        else:
            w = Check_dir(count,-3)
            if w != 0:
                RWest[count] = w
    count+=1

##########################################################    

COM.execute("create table Dungeon(RoomID INT,Desc TEXT,North INT,South INT,East INT,West INT)")
z = 0
while z <= 23:

    RN = Random_value(1,100)
    if RN <= 100:        # CHANGE THIS ONCE FEATURES ADDED #        
        COM.execute("select count (*) from Gen_desc")
        record = COM.fetchall()
        TotalRec = (record[0][0])
        selected = Random_value(1,TotalRec)
        
        
        ###Name = COM.execute("select Name from Gen_desc where Gen_desc_ID =?"),(selected)
        ###  ^This doesn't work and I'm not sure why^

        COM.execute("select Description from Gen_desc where Gen_desc_ID =(?)",(selected,))
        TDesc = COM.fetchone()
        RoomDesc = TDesc[0]
        COM.execute("insert into Dungeon values (?,?,?,?,?,?)",(ROOM[z],RoomDesc,RNorth[z],RSouth[z],REast[z],RWest[z]))
        connection.commit()
    z+=1

### Room number 25 becomes the "Start Room"    
COM.execute("insert into Dungeon values (?,?,?,?,?,?)",(ROOM[z],'Start Room',RNorth[z],RSouth[z],REast[z],RWest[z]))
connection.commit()    
################ TESTING ############################    
print "\n\n  ROOM      N       S       E       W"
z = 0
while z <= 24:
    print "    ",ROOM[z],"    ",RNorth[z],"    ",RSouth[z],"    ",REast[z],"    ",RWest[z]
    z+=1
print "\n\n\n"
z = 0
y = 2

while z <= 48:
    if Static_grid[z] in ROOM:
        if Static_grid[z] in (1,2,3,4,5,6,7):
            val = Static_grid[z] *10
        else:
            val = Static_grid[z]
        print val,
    else:
        print "  ",
    if Static_grid[z] in (7,17,27,37,47,57,67):
        print "  "
        
        y+=1
    z+=1
    

Friday 10.03.14
Posted by Benjamin Chilton
Newer / Older

Powered by Squarespace 6