41

Writing a real-time multiplayer game - Matthew Luard

Embed Size (px)

Citation preview

Page 1: Writing a real-time multiplayer game - Matthew Luard
Page 2: Writing a real-time multiplayer game - Matthew Luard
Page 3: Writing a real-time multiplayer game - Matthew Luard
Page 4: Writing a real-time multiplayer game - Matthew Luard
Page 5: Writing a real-time multiplayer game - Matthew Luard
Page 6: Writing a real-time multiplayer game - Matthew Luard
Page 7: Writing a real-time multiplayer game - Matthew Luard
Page 8: Writing a real-time multiplayer game - Matthew Luard
Page 9: Writing a real-time multiplayer game - Matthew Luard
Page 10: Writing a real-time multiplayer game - Matthew Luard
Page 11: Writing a real-time multiplayer game - Matthew Luard
Page 12: Writing a real-time multiplayer game - Matthew Luard
Page 13: Writing a real-time multiplayer game - Matthew Luard
Page 14: Writing a real-time multiplayer game - Matthew Luard
Page 15: Writing a real-time multiplayer game - Matthew Luard
Page 16: Writing a real-time multiplayer game - Matthew Luard
Page 17: Writing a real-time multiplayer game - Matthew Luard
Page 18: Writing a real-time multiplayer game - Matthew Luard
Page 19: Writing a real-time multiplayer game - Matthew Luard
Page 20: Writing a real-time multiplayer game - Matthew Luard
Page 21: Writing a real-time multiplayer game - Matthew Luard
Page 22: Writing a real-time multiplayer game - Matthew Luard
Page 23: Writing a real-time multiplayer game - Matthew Luard
Page 24: Writing a real-time multiplayer game - Matthew Luard
Page 25: Writing a real-time multiplayer game - Matthew Luard
Page 26: Writing a real-time multiplayer game - Matthew Luard
Page 27: Writing a real-time multiplayer game - Matthew Luard
Page 28: Writing a real-time multiplayer game - Matthew Luard
Page 29: Writing a real-time multiplayer game - Matthew Luard
Page 30: Writing a real-time multiplayer game - Matthew Luard
Page 31: Writing a real-time multiplayer game - Matthew Luard
Page 32: Writing a real-time multiplayer game - Matthew Luard
Page 33: Writing a real-time multiplayer game - Matthew Luard
Page 34: Writing a real-time multiplayer game - Matthew Luard
Page 35: Writing a real-time multiplayer game - Matthew Luard
Page 36: Writing a real-time multiplayer game - Matthew Luard
Page 37: Writing a real-time multiplayer game - Matthew Luard
Page 38: Writing a real-time multiplayer game - Matthew Luard
Page 39: Writing a real-time multiplayer game - Matthew Luard

cdef object get_lowest(set objects): cdef int current cdef int lowest = -1 cdef Tile lowest_object cdef Tile item for item in objects: current = item.H if lowest == -1 or current < lowest: lowest = current lowest_object = item return lowest_object def aStar(dict graph, Tile current, Tile end): cdef set openList = set() cdef set closedList = set() current.parent = None openList.add(current) cdef list retrace cdef Tile tile while len(openList) > 0: current = get_lowest(openList) if current is end: retrace = [] while current: retrace.insert(0, current) #change return representation here current = current.parent return retrace openList.discard(current) closedList.add(current) for tile in <list>(graph[current]): if tile not in closedList: tile.H = (abs(end.x-tile.x)+abs(end.y-tile.y))*10 openList.add(tile) tile.parent = current return []

def aStar(self, graph, current, end): open = [] closed = [] path = [] def retracePath(c): path.insert(0,c) if c.parent == None: return retracePath(c.parent) open.append(current) while len(open) is not 0: current = min(open, key=lambda inst:inst.H) if current == end:

Page 40: Writing a real-time multiplayer game - Matthew Luard

return retracePath(current) open.remove(current) closed.append(current) for tile in graph[current]: if tile not in closed: tile.H = (abs(end.x-tile.x)+abs(end.y-tile.y))*10 if tile not in open: open.append(tile) tile.parent = current return path

// Base implementation used from code by Mwjuhl at https://code.google.com/p/a-star-java/source/browse/AStar/src/aStar/AStar.java, altered somewhat to be as similar to Python implementations as possible. public Path calcShortestPath(int startX, int startY, int goalX, int goalY) { map.getStartNode().setDistanceFromStart(0); closedList.clear(); openList.clear(); openList.add(map.getStartNode()); while(openList.size() != 0) { Node current = openList.getFirst(); // check if our current Node location is the goal Node. If it is, we are done. if(current.getX() == map.getGoalLocationX() && current.getY() == map.getGoalLocationY()) { return remakePath(current); } //move current Node to the closed (already searched) list openList.remove(current); closedList.add(current); //go through all the current Nodes neighbors and calculate if one should be our next step for(Node neighbor : current.getNeighborList()) { boolean neighborIsBetter; // searched list? if (closedList.contains(neighbor)) continue; if (!neighbor.isObstacle) { float neighborDistanceFromStart = (current.getDistanceFromStart() + map.getDistanceBetween(current, neighbor)); // add to open list if it doesn't already have it if(!openList.contains(neighbor)) { openList.add(neighbor); neighbor.setPreviousNode(current); neighbor.setDistanceFromStart(neighborDistanceFromStart); neighbor.setHeuristicDistanceFromGoal(heuristic.getEstimatedDistanceToGoal(

Page 41: Writing a real-time multiplayer game - Matthew Luard

neighbor.getX(), neighbor.getY(), map.getGoalLocationX(), map.getGoalLocationY())); } } } return null; }