def _do_user_push(self, user_id, direction): self.incr_user_attr(user_id, "charge", -10) old_pos = self.user_pos[user_id] new_pos = direction_from_pos(direction, old_pos) target = self.obj_at(*new_pos) if target in (CONSTANTS.get("CELL_ROCK"), CONSTANTS.get("CELL_ROBOT")): new_posb = direction_from_pos(direction, new_pos) if self.obj_at(*new_posb) == 0: self.do_obj_move(new_pos, direction) self.do_user_move(user_id, direction) return 1 return 0
def __init__(self, engine, pos_val, user_attr): self.engine = engine self.map_h = MAP_HEIGHT self.map_w = MAP_WIDTH if user_attr is not None: self.user_attr = json.loads(user_attr) else: self.user_attr = {} self.pos_obj = {} self.user_pos = {} if pos_val is not None: pos_obj = json.loads(pos_val, use_decimal=True) for v in pos_obj: if len(v) == 4: x, y, t, i = v else: x, y, t = v i = None obj = GridObj(t=t, i=i, x=x, y=y) pos = (x, y) self.pos_obj[pos] = obj if obj.t == CONSTANTS.get('CELL_ROBOT'): self.user_pos[obj.i] = pos else: self.init_resources()
def kill_user(self, user_id): self.set_user_attr(user_id, "status", CONSTANTS.get("DEAD")) old_pos = self.user_pos[user_id] del self.user_pos[user_id] del self.pos_obj[old_pos] self.user_history(user_id, "YOUR BOT", "HAS DIED", 0) self.set_user_attr(user_id, "charge", 0)
def _do_user_pull(self, user_id, direction): old_pos = list(self.user_pos.get(user_id)) new_pos = direction_from_pos(direction, old_pos) if self.obj_at(*new_pos) == CONSTANTS.get("CELL_RESOURCE"): del self.pos_obj[new_pos] self.add_resource(user_id) return 1 return 0
def kill_user(self, user_id): self.set_user_attr(user_id, "status", CONSTANTS.get("DEAD")) old_pos = self.user_pos.get(user_id) if old_pos is not None: del self.user_pos[user_id] del self.pos_user[tuple(old_pos)] self.user_history(user_id, "YOUR BOT", "HAS DIED", 0) self.set_user_attr(user_id, "charge", 0)
def _do_user_move(self, user_id, direction): self.incr_user_attr(user_id, "charge", -5) old_pos = self.user_pos[user_id] new_pos = direction_from_pos(direction, old_pos) if self.obj_at(*new_pos) == 0: self.user_pos[user_id] = new_pos self.pos_obj[new_pos] = GridObj(t=CONSTANTS.get("CELL_ROBOT"), i=user_id, x=new_pos[0], y=new_pos[1]) del self.pos_obj[old_pos] return 1 return 0
def _do_user_punch(self, user_id, direction): self.incr_user_attr(user_id, "charge", -10) old_pos = self.user_pos[user_id] new_pos = direction_from_pos(direction, old_pos) target = self.user_at(*new_pos) if target is not None: self.user_history(target, "YOUR BOT", "WAS ATTACKED", 0) self.kill_user(target) self.pos_obj[new_pos] = GridObj(t=CONSTANTS.get("CELL_RESOURCE"), i=None, x=new_pos[0], y=new_pos[1]) return 1 return 0
def obj_at(self, x, y): pos = (x, y) if pos in self.pos_obj: return self.pos_obj[pos].t return CONSTANTS.get('CELL_EMPTY')
def spawn_rock(self): pos = self.get_open_position() if pos is not None: self.pos_obj[pos] = GridObj(t=CONSTANTS.get("CELL_ROCK"), i=None, x=pos[0], y=pos[1])
def spawn_user(self, user_id): pos = self.get_open_position() if pos is None: raise GridLangException("Could not locate empty space for you! Sorry! It will try again next tick.") self.user_pos[user_id] = pos self.pos_obj[pos] = GridObj(t=CONSTANTS.get("CELL_ROBOT"), i=user_id, x=pos[0], y=pos[1])
def is_user_dead(self, user_id): v = self.get_user_attr(user_id, "status") return v == CONSTANTS.get("DEAD")
def user_at(self, x, y): pos = (x, y) if pos in self.pos_obj and self.pos_obj[pos].t == CONSTANTS.get("CELL_ROBOT"): return self.pos_obj[pos].i
def obj_at(self, x, y): m = self.map_data[y][x] if m == 0 and (x, y) in self.pos_user: return CONSTANTS.get('CELL_ROBOT') return m