Exemple #1
0
 def sweep_collision(self, collidee, v, debug=False):
     """
     self (collider) moving by v, collidee stationery
     based on http://bit.ly/3grWzs
     """
     u_0 = [2, 2, 2]
     u_1 = [1, 1, 1]
     for i in xrange(3):
         if fops.lte(self.maxs[i], collidee.mins[i]) and fops.gt(v[i], 0):
             d = collidee.mins[i] - self.maxs[i]
             u_0[i] = d / v[i]
         elif fops.lte(collidee.maxs[i], self.mins[i]) and fops.lt(v[i], 0):
             d = collidee.maxs[i] - self.mins[i]
             u_0[i] = d / v[i]
         elif not(fops.lte(self.maxs[i], collidee.mins[i]) or fops.gte(self.mins[i], collidee.maxs[i])):
             u_0[i] = 0
         if fops.gte(collidee.maxs[i], self.mins[i]) and fops.gt(v[i], 0):
             d = collidee.maxs[i] - self.mins[i]
             u_1[i] = d / v[i]
         elif fops.gte(self.maxs[i], collidee.mins[i]) and fops.lt(v[i], 0):
             d = collidee.mins[i] - self.maxs[i]
             u_1[i] = d / v[i]
     u0 = max(u_0)
     if u0 == 2 or fops.gte(u0, 1.0):
         col = False
     else:
         col = fops.lte(u0, min(u_1))
     return col, u0
Exemple #2
0
 def sweep_collision(self, collidee, v, debug=False):
     """
     self (collider) moving by v, collidee stationery
     based on http://bit.ly/3grWzs
     """
     u_0 = [2, 2, 2]
     u_1 = [1, 1, 1]
     for i in xrange(3):
         if fops.lte(self.maxs[i], collidee.mins[i]) and fops.gt(v[i], 0):
             d = collidee.mins[i] - self.maxs[i]
             u_0[i] = d / v[i]
         elif fops.lte(collidee.maxs[i], self.mins[i]) and fops.lt(v[i], 0):
             d = collidee.maxs[i] - self.mins[i]
             u_0[i] = d / v[i]
         elif not (fops.lte(self.maxs[i], collidee.mins[i])
                   or fops.gte(self.mins[i], collidee.maxs[i])):
             u_0[i] = 0
         if fops.gte(collidee.maxs[i], self.mins[i]) and fops.gt(v[i], 0):
             d = collidee.maxs[i] - self.mins[i]
             u_1[i] = d / v[i]
         elif fops.gte(self.maxs[i], collidee.mins[i]) and fops.lt(v[i], 0):
             d = collidee.mins[i] - self.maxs[i]
             u_1[i] = d / v[i]
     u0 = max(u_0)
     if u0 == 2 or fops.gte(u0, 1.0):
         col = False
     else:
         col = fops.lte(u0, min(u_1))
     return col, u0
Exemple #3
0
 def check_aabbs(self):
     out = []
     self.add_grid_bounding_boxes_to(out)
     check = [0, 0, 0, 0]
     if len(out) == 3:
         bb = out[2]
         if fops.gt(bb.min_x, self.x):
             check[2] = -1
         elif fops.lt(bb.max_x, self.x + 1):
             check[2] = 1
         if fops.gt(bb.min_z, self.z):
             check[3] = -1
         elif fops.lt(bb.max_z, self.z + 1):
             check[3] = 1
     bb = out[1]
     if fops.gt(bb.min_x, self.x):
         check[0] = -1
     elif fops.lt(bb.max_x, self.x + 1):
         check[0] = 1
     if fops.gt(bb.min_z, self.z):
         check[1] = -1
     elif fops.lt(bb.max_z, self.z + 1):
         check[1] = 1
     if check[2] != 0 or check[3] != 0:
         if check[0] != 0:
             check[2] = check[0]
         if check[1] != 0:
             check[3] = check[1]
         yield AABB.from_block_coords(self.x + check[2] * config.PLAYER_RADIUS, self.y + 0.5, self.z + check[3] * config.PLAYER_RADIUS)
     else:
         if check[0] != 0:
             yield AABB.from_block_coords(self.x + check[0] * config.PLAYER_RADIUS, self.y + 0.5, self.z)
         if check[1] != 0:
             yield AABB.from_block_coords(self.x, self.y + 0.5, self.z + check[1] * config.PLAYER_RADIUS)
Exemple #4
0
 def action(self):
     b_obj = self.blackboard.bot_object
     self.status = self._check_status(b_obj)
     if self.status != Status.running:
         return
     on_ladder = self.blackboard.bot_is_on_ladder(b_obj)
     in_water = self.blackboard.bot_is_in_water(b_obj)
     if on_ladder or in_water:
         elev = self.target_state.platform_y - b_obj.y
         if fops.gt(elev, 0):
             self.jump(b_obj)
             self.move(b_obj)
         elif fops.lt(elev, 0):
             self.move(b_obj)
         else:
             if on_ladder:
                 self.sneak(b_obj)
             self.move(b_obj)
     elif self.blackboard.bot_is_standing(b_obj):
         elev = self.target_state.platform_y - b_obj.y
         if fops.lte(elev, 0):
             self.move(b_obj)
         elif fops.gt(elev, 0):
             if self.start_state.base_in(b_obj.aabb):
                 self.jump(b_obj)
             self.move(b_obj)
     else:
         self.move(b_obj)
Exemple #5
0
 def _tick(self):
     if self.cancelled:
         self.status = Status.failure
         return
     b_obj = self.bot.bot_object
     self.status = self.check_status(b_obj)
     if self.status != Status.running:
         return
     on_ladder = self.bot.is_on_ladder(b_obj)
     in_water = self.bot.is_in_water(b_obj)
     if on_ladder or in_water:
         elev = self.target_state.platform_y - b_obj.y
         if fops.gt(elev, 0):
             self.jump(b_obj)
             self.move(b_obj)
         elif fops.lt(elev, 0):
             self.move(b_obj)
         else:
             if on_ladder:
                 self.sneak(b_obj)
             self.move(b_obj)
     elif self.bot.is_standing(b_obj):
         elev = self.target_state.platform_y - b_obj.y
         if fops.lte(elev, 0):
             self.move(b_obj)
         elif fops.gt(elev, 0):
             if self.start_state.base_in(b_obj.aabb):
                 self.jump(b_obj)
             self.move(b_obj)
     else:
         self.move(b_obj)
Exemple #6
0
    def sweep_collision(self, collidee, v, debug=False):
        """
        self moving by v, collidee stationery
        based on http://bit.ly/3grWzs
        """
        u_0 = [2, 2, 2]
        u_1 = [1, 1, 1]
        dists = [None, None, None]
        for i in xrange(3):
            if fops.lte(self.maxs[i], collidee.mins[i]) and fops.gt(v[i], 0):
                d = collidee.mins[i] - self.maxs[i]
                dists[i] = d
                u_0[i] = d / v[i]
            elif fops.lte(collidee.maxs[i], self.mins[i]) and fops.lt(v[i], 0):
                d = collidee.maxs[i] - self.mins[i]
                dists[i] = d
                u_0[i] = d / v[i]
            elif fops.eq(v[i], 0) and \
                    not(fops.lte(self.maxs[i], collidee.mins[i]) or
                        fops.gte(self.mins[i], collidee.maxs[i])):
                u_0[i] = 0
            elif not(fops.lte(self.maxs[i], collidee.mins[i]) or
                     fops.gte(self.mins[i], collidee.maxs[i])):
                u_0[i] = 0
            if fops.gte(collidee.maxs[i], self.mins[i]) and fops.gt(v[i], 0):
                d = collidee.maxs[i] - self.mins[i]
                u_1[i] = d / v[i]
            elif fops.gte(self.maxs[i], collidee.mins[i]) and fops.lt(v[i], 0):
                d = collidee.mins[i] - self.maxs[i]
                u_1[i] = d / v[i]

        if max(u_0) == 2:
            u0 = None
            col = False
        else:
            u0 = max(u_0)
            u1 = min(u_1)
            if fops.gte(u0, 1.0):
                col = False
            else:
                col = fops.lte(u0, u1)
        return col, u0
Exemple #7
0
 def sweep_collision(self, bb, vect, debug=False, max_height=False):
     boxes = self.grid_bounding_box
     if len(boxes) == 0:
         raise Exception(
             "0 bounding boxes from block %s, cannot handle that" % self)
     elif len(boxes) == 1:
         col, rel_d = bb.sweep_collision(boxes[0], vect, debug=debug)
         return col, rel_d, boxes[0]
     else:
         col_rel_d = 1.1
         col_bb = None
         for box in boxes:
             col, rel_d = bb.sweep_collision(box, vect, debug=debug)
             if max_height and col and fops.eq(col_rel_d, rel_d):
                 if fops.lt(col_bb.max_y, bb.max_y):
                     col_bb = bb
             if col and fops.lt(rel_d, col_rel_d):
                 col_rel_d = rel_d
                 col_bb = bb
         return col_bb is not None, col_rel_d, col_bb
Exemple #8
0
 def check_aabbs(self):
     out = []
     self.add_grid_bounding_boxes_to(out)
     check = [0, 0, 0, 0]
     if len(out) == 3:
         bb = out[2]
         if fops.gt(bb.min_x, self.x):
             check[2] = -1
         elif fops.lt(bb.max_x, self.x + 1):
             check[2] = 1
         if fops.gt(bb.min_z, self.z):
             check[3] = -1
         elif fops.lt(bb.max_z, self.z + 1):
             check[3] = 1
     bb = out[1]
     if fops.gt(bb.min_x, self.x):
         check[0] = -1
     elif fops.lt(bb.max_x, self.x + 1):
         check[0] = 1
     if fops.gt(bb.min_z, self.z):
         check[1] = -1
     elif fops.lt(bb.max_z, self.z + 1):
         check[1] = 1
     if check[2] != 0 or check[3] != 0:
         if check[0] != 0:
             check[2] = check[0]
         if check[1] != 0:
             check[3] = check[1]
         yield AABB.from_block_coords(
             self.x + check[2] * config.PLAYER_RADIUS, self.y + 0.5,
             self.z + check[3] * config.PLAYER_RADIUS)
     else:
         if check[0] != 0:
             yield AABB.from_block_coords(
                 self.x + check[0] * config.PLAYER_RADIUS, self.y + 0.5,
                 self.z)
         if check[1] != 0:
             yield AABB.from_block_coords(
                 self.x, self.y + 0.5,
                 self.z + check[1] * config.PLAYER_RADIUS)
Exemple #9
0
 def min_collision_between(self, bb1, bb2, horizontal=False, max_height=False):
     ubb = bb1.extend_to(dy=-1).union(bb2.extend_to(dy=-1))
     blcks = self.blocks_in_aabb(ubb)
     dvect = bb1.vector_to(bb2)
     if horizontal:
         dvect = (dvect[0], 0, dvect[2])
     col_rel_d = 1.1
     col_bb = None
     for blk in blcks:
         col, rel_d, bb = blk.sweep_collision(
             bb1, dvect, max_height=max_height)
         if col and fops.eq(col_rel_d, rel_d):
             if max_height:
                 if fops.lt(col_bb.max_y, bb.max_y):
                     col_bb = bb
         if col and fops.lt(rel_d, col_rel_d):
             col_rel_d = rel_d
             col_bb = bb
     if col_bb is not None:
         return col_rel_d * tools.vector_size(dvect), col_bb
     else:
         return None, None
 def _tick(self):
     b_obj = self.bot.bot_object
     self.status = self.check_status(b_obj)
     if self.status != Status.running:
         return
     col_distance, col_bb = self.world.grid.min_collision_between(b_obj.aabb,
                                                                  self.target_space.bb_stand,
                                                                  horizontal=True,
                                                                  max_height=True)
     if self.bot.is_on_ladder(b_obj) or self.bot.is_in_water(b_obj):
         elev = self.target_space.bb_stand.min_y - b_obj.aabb.min_y
         if fops.gt(elev, 0):
             self.jump(b_obj)
             self.move(b_obj)
         elif fops.lt(elev, 0):
             if col_distance is None:
                 self.move(b_obj)
         else:
             self.move(b_obj)
     elif self.bot.is_standing(b_obj):
         if col_distance is None:
             self.move(b_obj)
         else:
             elev = self.target_space.bb_stand.min_y - b_obj.aabb.min_y
             if fops.lte(elev, 0):
                 self.move(b_obj)
             elif fops.gt(elev, 0) and fops.lte(elev, config.MAX_STEP_HEIGHT):
                 self.move(b_obj)
             elif fops.gt(elev, config.MAX_STEP_HEIGHT) and fops.lt(elev, config.MAX_JUMP_HEIGHT):
                 ticks_to_col = col_distance / self.bot.current_motion(b_obj)
                 ticks_to_jump = math.sqrt(2 * elev / config.G) * 20
                 if ticks_to_col < ticks_to_jump:
                     self.jump(b_obj)
                 self.move(b_obj)
             else:
                 raise Exception("move elevation error %s with collision %s" % (elev, col_distance))
     else:
         self.move(b_obj)
Exemple #11
0
 def calculate_axis_offset(self, collidee, d, axis):
     for i in xrange(3):
         if i == axis:
             continue
         if fops.lte(self.maxs[i], collidee.mins[i]) or \
                 fops.gte(self.mins[i], collidee.maxs[i]):
             return d
     if d < 0 and fops.lte(collidee.maxs[axis], self.mins[axis]):
         dout = collidee.maxs[axis] - self.mins[axis]
         if fops.gt(dout, d):
             d = dout
     elif d > 0 and fops.gte(collidee.mins[axis], self.maxs[axis]):
         dout = collidee.mins[axis] - self.maxs[axis]
         if fops.lt(dout, d):
             d = dout
     return d
Exemple #12
0
 def calculate_axis_offset(self, collidee, d, axis):
     for i in xrange(3):
         if i == axis:
             continue
         if fops.lte(self.maxs[i], collidee.mins[i]) or \
                 fops.gte(self.mins[i], collidee.maxs[i]):
             return d
     if d < 0 and fops.lte(collidee.maxs[axis], self.mins[axis]):
         dout = collidee.maxs[axis] - self.mins[axis]
         if fops.gt(dout, d):
             d = dout
     elif d > 0 and fops.gte(collidee.mins[axis], self.maxs[axis]):
         dout = collidee.mins[axis] - self.maxs[axis]
         if fops.lt(dout, d):
             d = dout
     return d
 def can_stand_between(self, gs, debug=False):
     if self.block.is_ladder_vine or gs.block.is_ladder_vine:
         return True
     if self.block.is_water or gs.block.is_water:
         return True
     if fops.gt(abs(self.platform.min_y - gs.platform.min_y), config.MAX_STEP_HEIGHT):
         return True
     stand_platform = self.platform.expand(config.PLAYER_BODY_DIAMETER - 0.09, 0, config.PLAYER_BODY_DIAMETER - 0.09)
     self.intersection = gs.stand_block.intersection_on_axes(
         stand_platform, x=True, z=True, debug=debug)
     if self.intersection is None:
         return False
     if self.stand_block.x != gs.stand_block.x and self.stand_block.z != gs.stand_block.z:
         if fops.lt(gs.platform.get_side('min', x=True, z=True), 0.5):
             return False
         else:
             return True
     else:
         return True
 def _can_stand_on(self):
     """
     can stand on top of the center of the block
     """
     if isinstance(self.block, blocks.Cactus):
         return False
     under = self.grid.get_block(self.coords[0], self.coords[1] - 1, self.coords[2])
     if not self.block.collidable and not under.is_fence and not self.block.is_water and not self.block.is_ladder_vine:
         return False
     if self.block.is_ladder_vine or self.block.is_water:
         bb = AABB.from_block_coords(self.block.coords)
         if under.is_fence:
             self.bb_stand = bb.shift(min_y=under.max_y)
         else:
             if not under.collidable or (under.collidable and fops.lt(under.max_y, bb.min_y)):
                 bb1 = bb.offset(dy=0.5)
                 if self.can_be_in(bb1):
                     bb = bb1
             self.bb_stand = bb
         self.stand_block = self.block
         self.platform = self.bb_stand.set_to(max_y=self.bb_stand.min_y)
     else:
         if under.is_fence:
             fence_top = under.maxedge_platform(y=1)
             if self.block.collidable:
                 self.platform = self.block.maxedge_platform(y=1)
                 self.stand_block = self.block
                 if fence_top.min_y > self.platform.min_y:
                     self.platform = fence_top
                     self.stand_block = under
             else:
                 self.platform = fence_top
                 self.stand_block = under
         else:
             self.platform = self.block.maxedge_platform(y=1)
             self.stand_block = self.block
         bb = AABB.from_block_coords(self.block.coords)
         self.bb_stand = bb.offset(dy=self.platform.min_y - bb.min_y)
         if not self.bb_stand.collides_on_axes(self.platform, x=True, z=True):
             return False
     return self.can_be_in(self.bb_stand)
 def can_go_between(self, gs, update_to_bb_stand=False, debug=False):
     edge_cost = 0
     bb_stand = self.bb_stand
     other_bb_stand = gs.bb_stand
     if bb_stand.horizontal_distance(other_bb_stand) > config.HORIZONTAL_MOVE_DISTANCE_LIMIT:
         if debug:
             print 'horizontal distance too far', bb_stand.horizontal_distance(other_bb_stand)
         return False
     if fops.gt(bb_stand.min_y, other_bb_stand.min_y):
         if (bb_stand.min_y - other_bb_stand.min_y) > 3:
             return False
         elev = bb_stand.min_y - other_bb_stand.min_y
         elev_bb = other_bb_stand.extend_to(dy=elev)
         bb_from = bb_stand
         bb_to = other_bb_stand.offset(dy=elev)
     elif fops.lt(bb_stand.min_y, other_bb_stand.min_y):
         if fops.lte(bb_stand.grid_y + 2, other_bb_stand.min_y):
             if debug:
                 print 'over 2 high difference'
             return False
         elev = other_bb_stand.min_y - bb_stand.min_y
         in_water = self.grid.aabb_in_water(bb_stand)
         if in_water and \
                 other_bb_stand.grid_y > bb_stand.grid_y and \
                 fops.gt(other_bb_stand.min_y, other_bb_stand.grid_y) and \
                 not self.grid.aabb_in_water(bb_stand.shift(min_y=other_bb_stand.min_y)) and \
                 fops.gte(other_bb_stand.min_y - (bb_stand.grid_y + 1), config.MAX_WATER_JUMP_HEIGHT - 0.15):
             if debug:
                 print 'water cannot go'
             return False
         if self.grid.aabb_on_ladder(bb_stand) and \
                 other_bb_stand.grid_y > bb_stand.grid_y and \
                 fops.gt(other_bb_stand.min_y, other_bb_stand.grid_y) and \
                 not self.grid.aabb_on_ladder(bb_stand.shift(min_y=other_bb_stand.min_y)) and \
                 fops.gte(other_bb_stand.min_y - (bb_stand.grid_y + 1), config.MAX_VINE_JUMP_HEIGHT - 0.2):
             if debug:
                 print 'ladder cannot go'
             return False
         if fops.gt(elev, config.MAX_JUMP_HEIGHT) and not in_water:
             if debug:
                 print 'too high'
             return False
         if fops.lte(elev, config.MAX_STEP_HEIGHT):
             elev = config.MAX_STEP_HEIGHT
             aabbs = self.grid.aabbs_in(bb_stand.extend_to(0, elev, 0))
             for bb in aabbs:
                 elev = bb_stand.calculate_axis_offset(bb, elev, 1)
             if fops.lt(bb_stand.min_y + elev, other_bb_stand.min_y):
                 if debug:
                     print 'cannot make step'
                 return False
         elev_bb = bb_stand.extend_to(dy=elev)
         bb_from = bb_stand.offset(dy=elev)
         bb_to = other_bb_stand
     else:
         elev = 0
         elev_bb = None
         bb_from = bb_stand
         bb_to = other_bb_stand
     if elev_bb is not None:
         if self.grid.aabb_collides(elev_bb):
             if debug:
                 print 'elevation collision'
             return False
         if self.blocks_to_avoid(self.grid.blocks_in_aabb(elev_bb)):
             if debug:
                 print 'elevation hitting avoid block'
             return False
     if self.grid.collision_between(bb_from, bb_to, debug=debug):
         if debug:
             print 'horizontal collision'
         return False
     if self.blocks_to_avoid(self.grid.passing_blocks_between(bb_from, bb_to)):
         if debug:
             print 'hitting avoid block'
         return False
     edge_cost += config.COST_DIRECT * bb_from.horizontal_distance(bb_to)
     if not (fops.lte(elev, config.MAX_STEP_HEIGHT) and fops.gte(elev, -config.MAX_STEP_HEIGHT)):
         edge_cost += config.COST_FALL * \
             bb_from.horizontal_distance(bb_to)
         if elev < 0:
             edge_cost += config.COST_FALL * elev
         else:
             edge_cost += config.COST_JUMP
     self.edge_cost = edge_cost
     if update_to_bb_stand:
         gs.bb_stand = other_bb_stand
     return True
Exemple #16
0
 def set_direction_towards(self, point, debug=False):
     if fops.lt(self.distance_to(point), 0):
         self.a *= -1
         self.b *= -1