def update_joints(self): w = normalized(self.body.collider.half_width) * self.direction h = normalized(self.body.collider.half_height) foot_offset = 0.3 * ((self.front_foot.relative_position[1]) + (self.back_foot.relative_position[1])) * basis(1) self.body.set_position(self.position - 0.5 * self.crouched * basis(1) + foot_offset) self.shoulder = self.body.position + 0.15 * ( 1 - self.crouched) * h - 0.2 * w self.back_hip = self.body.position - foot_offset + 0.1 * w - 0.45 * h self.front_hip = self.body.position - foot_offset - 0.1 * w - 0.45 * h if self.destroyed: angle_goal = self.body.angle else: angle_goal = np.arctan(self.hand_goal[1] / (self.hand_goal[0] + 1e-6)) angle_goal = max(self.body.angle - 0.25, min(self.body.angle + 0.25, angle_goal)) self.head.angle += 0.1 * (angle_goal - self.head.angle) self.head.set_position(self.body.position - (1 - self.crouched) * 0.35 * (self.head.angle - self.body.angle) * w * self.direction + 0.2 * self.crouched * w + (1 - 0.5 * self.crouched) * h)
def flip_horizontally(self): if type(self.collider) is Rectangle: w = normalized(self.collider.half_width) r = self.collider.position - self.position self.collider.position -= 2 * np.dot(r, w) * w self.direction *= -1 self.image_position[0] *= -1
def throw_object(self, velocity=0.0): if not self.object: return self.object.velocity[:] = normalized( self.hand_goal) * velocity * self.throw_speed self.object.angular_velocity = -0.5 * self.direction * velocity self.object.gravity_scale = 1.0 if self.object.collider: self.object.collider.group = Group.THROWN self.object = None self.grab_timer = self.grab_delay
def seek_players(self, players): if self.destroyed: return goal = None dist = np.inf for p in players: if p.destroyed: continue d = norm(p.position - self.position) if d < dist: goal = p.position dist = d if goal is None: return if dist > 2.0: self.goal_velocity[0] = 0.25 * normalized(goal - self.position)[0] else: self.goal_velocity[0] = 0.0 self.attack() self.hand_goal[0] = np.sign(goal - self.position)[0]
import json from helpers import relative_path, normalized, find_end_time from orderbook import Orderbook from metrics import orderbook_stddev, orderbook_spread, orderbook_unbounded, orderbook_regression_error, orderbook_regression_r2 # Configurable INCREMENT = 30*60*1000 # 30 minutes HISTORY_AMOUNT = 4*24*60*60*1000 # how many ms in the past to look at, in this case, 4 days METRIC = normalized(orderbook_regression_error) ITEM_FILTER = lambda item: 1500000 > item['overall_average'] > 2000 ORDERBOOK_FILTER = lambda orderbook: orderbook.volume_at(END_TIME) > 10 # Calculated END_TIME = find_end_time() START_TIME = END_TIME - HISTORY_AMOUNT RANGE = range(START_TIME, END_TIME, INCREMENT) def load_orderbooks(): summary_file = relative_path('static', 'summary.json') with open(summary_file, 'r') as f: summaries = json.load(f) downloadable = [item_summary for item_summary in summaries.values() if ITEM_FILTER(item_summary)] orderbooks = [Orderbook(summaries, item['id']) for item in downloadable] return [orderbook for orderbook in orderbooks if ORDERBOOK_FILTER(orderbook)] def main(): orderbooks = load_orderbooks()
def update(self, gravity, time_step, colliders): for p in self.particle_clouds: p.update(gravity, time_step) if not p.active: self.particle_clouds.remove(p) if not self.active: return if self.velocity[1] != 0: self.on_ground = False self.speed = norm(self.velocity) if self.speed != 0: self.velocity *= min(self.speed, MAX_SPEED) / self.speed delta_pos = self.velocity * time_step + 0.5 * self.acceleration * time_step**2 self.set_position(self.position + delta_pos) acc_old = self.acceleration.copy() self.acceleration = self.get_acceleration(gravity) if self.rest_angle is None: self.angular_velocity = -self.gravity_scale * self.velocity[0] delta_angle = self.angular_velocity * time_step + 0.5 * self.angular_acceleration * time_step**2 if delta_angle: self.rotate(delta_angle) ang_acc_old = float(self.angular_acceleration) self.angular_acceleration = 0.0 if self.collider is None or not self.collision_enabled: return if any(np.abs(delta_pos) > 0.01) or abs(delta_angle) > 1e-3: self.collider.update_occupied_squares(colliders) self.collider.update_collisions(colliders) for collision in self.collider.collisions: collider = collision.collider if not collider.parent.collision_enabled: continue if collider.group is Group.PLATFORMS: if self.parent and self.collider.group in {Group.GUNS, Group.SWORDS, Group.SHIELDS}: self.collider.collisions.remove(collision) continue if collider.half_height[1] > 0: if self.collider.position[1] - delta_pos[1] - self.collider.axis_half_width(basis(1)) \ < collider.position[1] + collider.half_height[1]: self.collider.collisions.remove(collision) continue elif self.collider.position[1] - delta_pos[1] + self.collider.axis_half_width(basis(1)) \ > collider.position[1] + collider.half_height[1]: self.collider.collisions.remove(collision) continue if self.collider.group is Group.THROWN: if collider.parent is self.parent: self.collider.collisions.remove(collision) continue try: collider.parent.parent.throw_object() except AttributeError: pass if collision.overlap[1] > 0: self.on_ground = True if not self.parent: if self.rest_angle is not None: self.rotate(-self.angle + self.direction * self.rest_angle) self.angular_velocity = 0.0 elif collision.overlap[0] != 0: self.angular_velocity *= -1 n = min(int(self.speed * 5), 10) if n > 1: self.particle_clouds.append(Dust(self.position, self.speed * normalized(collision.overlap), n)) self.set_position(self.position + collision.overlap) n = collision.overlap self.velocity -= 2 * self.velocity.dot(n) * n / norm2(n) self.velocity *= self.bounce if not self.parent and isinstance(collider.parent, PhysicsObject): collider.parent.velocity[:] = -self.velocity if self.collider.group is Group.THROWN and self.collider.collisions: self.parent = None self.collider.group = self.group self.velocity += 0.5 * (acc_old + self.acceleration) * time_step self.angular_velocity += 0.5 * (ang_acc_old + self.angular_acceleration) * time_step if abs(self.velocity[0]) < 0.05: self.velocity[0] = 0.0 if self.parent is None and self.collider.collisions and self.speed > 0.1: self.sounds.add(self.bump_sound)