catsock
catsock

Reputation: 1851

Wiring logic in my game

I'm writing a tile based game in python. Basically you can place different blocks on the tiles, and some generate or use powers. So, there's wires. After trying to figure out how I was gonna program the wires, I decided on making a wireNetwork class. I got everything working fine and dandy except for when a network is split in two. Let's say there is two separate wire networks, and I bridge the gap between them with wires so they're connected. My code notices that and groups the two wire networks into a single merged one. But what if I was to delete the bridge, making two individual groups of networks again? How would I handle this? Here's the wiring code from my game down below.

class wireTemplate():
    def __init__(self, image, maxPower):
        self.image = pygame.transform.scale(image, (tileW,tileH))
        wireTemplates.append(self)

        self.maxPower = maxPower

    def reproduce(self,x,y):
        global wireMap
        temp = copy.copy(self)
        temp.rect = pygame.Rect(x*tileW,y*tileH,tileW,tileH)
        temp.x = x
        temp.y = y
        wireMap[y][x] = temp
        wires.append(temp)
        didSomething = False
        for i in getSurrounding(wireMap,x,y):
            if i != None and i.type == "Wire":
                if temp not in i.network.wires:
                    i.network.wires.append(temp)
                    temp.network = i.network
                    didSomething = True
                #getSurrounding() returns the surrounding tiles of the
                #coordinates specified.
                for i2 in getSurrounding(wireMap,x,y):
                    if i2 != None and i2.type == "Wire":
                        if i.network != i2.network:
                            mergeNetworks(i.network,i2.network)
        if not didSomething:
            temp.network = wireNetwork()
            temp.network.wires.append(temp)
        return temp

    def delete(self):
        wires.remove(self)
        self.network.wires.remove(self)
        if self.network.wires == []: self.network.delete()
        for iteration, i in enumerate(wireMap):
            if self in i:
                wireMap[iteration][i.index(self)] = None

class wireNetwork():
    def __init__(self):
        wireNetworks.append(self)
        self.wires = []
        self.outputs = []
        self.inputs = []
        self.color = pygame.Color(random.choice([0,255]),random.choice([0,255]),random.choice([0,255]),50)

    def delete(self):
        wireNetworks.remove(self)

Upvotes: 0

Views: 77

Answers (1)

asthasr
asthasr

Reputation: 9397

What you're modeling is a graph; you need each node to keep track of its neighbors. You can keep a data structure in memory for fast identification of whether two nodes are connected (the union-find algorithm is perfect for this), and this is quite fast as long as you are either using the structure or continuing to accumulate connections. A disconnection will require that you rebuild the union-find data structure from scratch, which will be relatively expensive, but you have the source data necessary to do so (the neighbor data for each of the nodes).

Upvotes: 2

Related Questions