// Draw a laser shaped 3d looking thing using vertex coloring (useful for things like colored laser glows) // If max_len is > 1.0, then this caps the length to be no longer than max_len pixels float g3_draw_laser_rgb(vec3d *headp, float head_width, vec3d *tailp, float tail_width, int r, int g, int b, uint tmap_flags, float max_len ) { if (!Lasers){ return 0.0f; } if((!Cmdline_nohtl) && tmap_flags & TMAP_HTL_3D_UNLIT){ return g3_draw_laser_htl(headp,head_width,tailp,tail_width,r,g,b,tmap_flags | TMAP_HTL_3D_UNLIT); } float headx, heady, headr, tailx, taily, tailr; vertex pt1, pt2; float depth; Assert( G3_count == 1 ); g3_rotate_vertex(&pt1,headp); g3_project_vertex(&pt1); if (pt1.flags & PF_OVERFLOW) return 0.0f; g3_rotate_vertex(&pt2,tailp); g3_project_vertex(&pt2); if (pt2.flags & PF_OVERFLOW) return 0.0f; if ( (pt1.codes & pt2.codes) != 0 ) { // Both off the same side return 0.0f; } headx = pt1.screen.xyw.x; heady = pt1.screen.xyw.y; headr = (head_width*Matrix_scale.xyz.x*Canv_w2*pt1.screen.xyw.w); tailx = pt2.screen.xyw.x; taily = pt2.screen.xyw.y; tailr = (tail_width*Matrix_scale.xyz.x*Canv_w2*pt2.screen.xyw.w); float len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) ); // Cap the length if needed. if ( (max_len > 1.0f) && (len_2d > max_len) ) { float ratio = max_len / len_2d; tailx = headx + ( tailx - headx ) * ratio; taily = heady + ( taily - heady ) * ratio; tailr = headr + ( tailr - headr ) * ratio; len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) ); } depth = (pt1.world.xyz.z+pt2.world.xyz.z)*0.5f; float max_r = headr; float a; if ( tailr > max_r ) max_r = tailr; if ( max_r < 1.0f ) max_r = 1.0f; float mx, my, w, h1,h2; if ( len_2d < max_r ) { h1 = headr + (max_r-len_2d); if ( h1 > max_r ) h1 = max_r; h2 = tailr + (max_r-len_2d); if ( h2 > max_r ) h2 = max_r; len_2d = max_r; if ( fl_abs(tailx - headx) > 0.01f ) { a = (float)atan2( taily-heady, tailx-headx ); } else { a = 0.0f; } w = len_2d; } else { a = atan2_safe( taily-heady, tailx-headx ); w = len_2d; h1 = headr; h2 = tailr; } mx = (tailx+headx)/2.0f; my = (taily+heady)/2.0f; // Draw box with width 'w' and height 'h' at angle 'a' from horizontal // centered around mx, my if ( h1 < 1.0f ) h1 = 1.0f; if ( h2 < 1.0f ) h2 = 1.0f; float sa, ca; sa = (float)sin(a); ca = (float)cos(a); vertex v[4]; vertex *vertlist[4] = { &v[3], &v[2], &v[1], &v[0] }; memset(v,0,sizeof(vertex)*4); if ( depth < 0.0f ) depth = 0.0f; v[0].screen.xyw.x = (-w/2.0f)*ca + (-h1/2.0f)*sa + mx; v[0].screen.xyw.y = (-w/2.0f)*sa - (-h1/2.0f)*ca + my; v[0].world.xyz.z = pt1.world.xyz.z; v[0].screen.xyw.w = pt1.screen.xyw.w; v[0].texture_position.u = 0.0f; v[0].texture_position.v = 0.0f; v[0].r = (ubyte)r; v[0].g = (ubyte)g; v[0].b = (ubyte)b; v[0].a = 255; v[1].screen.xyw.x = (w/2.0f)*ca + (-h2/2.0f)*sa + mx; v[1].screen.xyw.y = (w/2.0f)*sa - (-h2/2.0f)*ca + my; v[1].world.xyz.z = pt2.world.xyz.z; v[1].screen.xyw.w = pt2.screen.xyw.w; v[1].texture_position.u = 1.0f; v[1].texture_position.v = 0.0f; v[1].r = (ubyte)r; v[1].g = (ubyte)g; v[1].b = (ubyte)b; v[1].a = 255; v[2].screen.xyw.x = (w/2.0f)*ca + (h2/2.0f)*sa + mx; v[2].screen.xyw.y = (w/2.0f)*sa - (h2/2.0f)*ca + my; v[2].world.xyz.z = pt2.world.xyz.z; v[2].screen.xyw.w = pt2.screen.xyw.w; v[2].texture_position.u = 1.0f; v[2].texture_position.v = 1.0f; v[2].r = (ubyte)r; v[2].g = (ubyte)g; v[2].b = (ubyte)b; v[2].a = 255; v[3].screen.xyw.x = (-w/2.0f)*ca + (h1/2.0f)*sa + mx; v[3].screen.xyw.y = (-w/2.0f)*sa - (h1/2.0f)*ca + my; v[3].world.xyz.z = pt1.world.xyz.z; v[3].screen.xyw.w = pt1.screen.xyw.w; v[3].texture_position.u = 0.0f; v[3].texture_position.v = 1.0f; v[3].r = (ubyte)r; v[3].g = (ubyte)g; v[3].b = (ubyte)b; v[3].a = 255; gr_tmapper(4, vertlist, tmap_flags | TMAP_FLAG_RGB | TMAP_FLAG_GOURAUD | TMAP_FLAG_CORRECT); return depth; }
// Returns TRUE if the weapon will never hit the other object. // If it can it predicts how long until these two objects need // to be checked and fills the time in in current_pair. int weapon_will_never_hit( object *obj_weapon, object *other, obj_pair * current_pair ) { Assert( obj_weapon->type == OBJ_WEAPON ); weapon *wp = &Weapons[obj_weapon->instance]; weapon_info *wip = &Weapon_info[wp->weapon_info_index]; // mprintf(( "Frame: %d, Weapon=%d, Other=%d, pair=$%08x\n", G3_frame_count, OBJ_INDEX(weapon), OBJ_INDEX(other), current_pair )); // Do some checks for weapons that don't turn if ( !(wip->wi_flags[Weapon::Info_Flags::Turns]) ) { // This first check is to see if a weapon is behind an object, and they // are heading in opposite directions. If so, we don't need to ever check // them again. This is only valid for weapons that don't turn. float vdot; if (wip->subtype == WP_LASER) { vec3d velocity_rel_weapon; vm_vec_sub(&velocity_rel_weapon, &obj_weapon->phys_info.vel, &other->phys_info.vel); vdot = -vm_vec_dot(&velocity_rel_weapon, &obj_weapon->orient.vec.fvec); } else { vdot = vm_vec_dot( &other->phys_info.vel, &obj_weapon->phys_info.vel); } if ( vdot <= 0.0f ) { // They're heading in opposite directions... // check their positions vec3d weapon2other; vm_vec_sub( &weapon2other, &other->pos, &obj_weapon->pos ); float pdot = vm_vec_dot( &obj_weapon->orient.vec.fvec, &weapon2other ); if ( pdot <= -other->radius ) { // The other object is behind the weapon by more than // its radius, so it will never hit... return 1; } } // FUTURE ENHANCEMENT IDEAS // Given a laser does it hit a slow or not moving object // in its life or the next n seconds? We'd actually need to check the // model for this. } // This check doesn't care about orient, only looks at the maximum speed // of the two objects, so it knows that in the next n seconds, they can't // go further than some distance, so don't bother checking collisions for // that time. This is very rough, but is so general that it works for // everything and immidiately gets rid of a lot of cases. if ( current_pair ) { // Find the time it will take before these get within each others distances. // tmp->next_check_time = timestamp(500); //vector max_vel; //maximum foward velocity in x,y,z float max_vel_weapon, max_vel_other; //SUSHI: Fix bug where additive weapon velocity screws up collisions //If the PF_CONST_VEL flag is set, we can safely assume it doesn't change speed. if (obj_weapon->phys_info.flags & PF_CONST_VEL) max_vel_weapon = obj_weapon->phys_info.speed; else if (wp->lssm_stage==5) max_vel_weapon = wip->lssm_stage5_vel; else max_vel_weapon = wp->weapon_max_vel; max_vel_other = other->phys_info.max_vel.xyz.z; if (max_vel_other < 10.0f) { if ( vm_vec_mag_squared( &other->phys_info.vel ) > 100 ) { // bump up velocity from collision max_vel_other = vm_vec_mag( &other->phys_info.vel ) + 10.0f; } else { max_vel_other = 10.0f; // object may move from collision } } // check weapon that does not turn against sphere expanding at ship maxvel // compare (weeapon) ray with expanding sphere (ship) to find earliest possible collision time // look for two time solutions to Xw = Xs, where Xw = Xw0 + Vwt*t Xs = Xs + Vs*(t+dt), where Vs*dt = radius of ship // Since direction of Vs is unknown, solve for (Vs*t) and find norm of both sides if ( !(wip->wi_flags[Weapon::Info_Flags::Turns]) ) { vec3d delta_x, laser_vel; float a,b,c, delta_x_dot_vl, delta_t; float root1, root2, root, earliest_time; if (max_vel_weapon == max_vel_other) { // this will give us NAN using the below formula, so check every frame current_pair->next_check_time = timestamp(0); return 0; } vm_vec_sub( &delta_x, &obj_weapon->pos, &other->pos ); laser_vel = obj_weapon->phys_info.vel; // vm_vec_copy_scale( &laser_vel, &weapon->orient.vec.fvec, max_vel_weapon ); delta_t = (other->radius + 10.0f) / max_vel_other; // time to get from center to radius of other obj delta_x_dot_vl = vm_vec_dot( &delta_x, &laser_vel ); a = max_vel_weapon*max_vel_weapon - max_vel_other*max_vel_other; b = 2.0f * (delta_x_dot_vl - max_vel_other*max_vel_other*delta_t); c = vm_vec_mag_squared( &delta_x ) - max_vel_other*max_vel_other*delta_t*delta_t; float discriminant = b*b - 4.0f*a*c; if ( discriminant < 0) { // never hit return 1; } else { root = fl_sqrt( discriminant ); root1 = (-b + root) / (2.0f * a) * 1000.0f; // get time in ms root2 = (-b - root) / (2.0f * a) * 1000.0f; // get time in ms } // standard algorithm if (max_vel_weapon > max_vel_other) { // find earliest positive time if ( root1 > root2 ) { float temp = root1; root1 = root2; root2 = temp; } if (root1 > 0) { earliest_time = root1; } else if (root2 > 0) { // root1 < 0 and root2 > 0, so we're inside sphere and next check should be next frame current_pair->next_check_time = timestamp(0); // check next time return 0; } else { // both times negative, so never collides return 1; } } // need to modify it for weapons that are slower than ships else { if (root2 > 0) { earliest_time = root2; } else { current_pair->next_check_time = timestamp(0); return 0; } } // check if possible collision occurs after weapon expires if ( earliest_time > 1000*wp->lifeleft ) return 1; // Allow one worst case frametime to elapse (~5 fps) earliest_time -= 200.0f; if (earliest_time > 100) { current_pair->next_check_time = timestamp( fl2i(earliest_time) ); return 0; } else { current_pair->next_check_time = timestamp(0); // check next time return 0; } } else { float dist, max_vel, time; max_vel = max_vel_weapon + max_vel_other; // suggest that fudge factor for other radius be changed to other_radius + const (~10) dist = vm_vec_dist( &other->pos, &obj_weapon->pos ) - (other->radius + 10.0f); if ( dist > 0.0f ) { time = (dist*1000.0f) / max_vel; int time_ms = fl2i(time); // check if possible collision occurs after weapon expires if ( time_ms > 1000*wp->lifeleft ) return 1; time_ms -= 200; // Allow at least one worst case frametime to elapse (~5 fps) if ( time_ms > 100 ) { // If it takes longer than 1/10th of a second, then delay it current_pair->next_check_time = timestamp(time_ms); //mprintf(( "Delaying %d ms\n", time_ms )); return 0; } } current_pair->next_check_time = timestamp(0); // check next time } } return 0; }
void radar_plot_object_std( object *objp ) { vec3d pos, tempv; float dist, rscale, zdist, max_radar_dist; int xpos, ypos; vec3d *world_pos = &objp->pos; float awacs_level; // don't process anything here. Somehow, a jumpnode object caused this function // to get entered on server side. if( Game_mode & GM_STANDALONE_SERVER ){ return; } // multiplayer clients ingame joining should skip this function if ( MULTIPLAYER_CLIENT && (Net_player->flags & NETINFO_FLAG_INGAME_JOIN) ){ return; } // get team-wide awacs level for the object if not ship int ship_is_visible = 0; if (objp->type == OBJ_SHIP) { if (Player_ship != NULL) { if (ship_is_visible_by_team(objp, Player_ship)) { ship_is_visible = 1; } } } // only check awacs level if ship is not visible by team awacs_level = 1.5f; if (Player_ship != NULL && !ship_is_visible) { awacs_level = awacs_get_level(objp, Player_ship); } // if the awacs level is unviewable - bail if(awacs_level < 0.0f && !See_all){ return; } // Apply object type filters switch (objp->type) { case OBJ_SHIP: // Place to cull ships, such as NavBuoys break; case OBJ_JUMP_NODE: { // don't plot hidden jump nodes if ( objp->jnp->is_hidden() ) return; // filter jump nodes here if required break; } case OBJ_WEAPON: { // if not a bomb, return if ( !(Weapon_info[Weapons[objp->instance].weapon_info_index].wi_flags & WIF_BOMB) ) return; // if we don't attack the bomb, return if ( !iff_x_attacks_y(Player_ship->team, obj_team(objp)) ) return; // if a local ssm is in subspace, return if (Weapons[objp->instance].lssm_stage == 3) return; break; } // if any other kind of object, don't show it on radar default: return; } // JAS -- new way of getting the rotated point that doesn't require this to be // in a g3_start_frame/end_frame block. vm_vec_sub(&tempv, world_pos, &Player_obj->pos); vm_vec_rotate(&pos, &tempv, &Player_obj->orient); // Apply range filter dist = vm_vec_dist(world_pos, &Player_obj->pos); max_radar_dist = Radar_ranges[HUD_config.rp_dist]; if (dist > max_radar_dist) { return; } if (dist < pos.xyz.z) { rscale = 0.0f; } else { rscale = (float) acos(pos.xyz.z / dist) / 3.14159f; //2.0f; } zdist = fl_sqrt((pos.xyz.x * pos.xyz.x) + (pos.xyz.y * pos.xyz.y)); float new_x_dist, clipped_x_dist; float new_y_dist, clipped_y_dist; if (zdist < 0.01f) { new_x_dist = 0.0f; new_y_dist = 0.0f; } else { new_x_dist = (pos.xyz.x / zdist) * rscale * radx; new_y_dist = (pos.xyz.y / zdist) * rscale * rady; // force new_x_dist and new_y_dist to be inside the radar float hypotenuse; float max_radius; hypotenuse = (float) _hypot(new_x_dist, new_y_dist); max_radius = i2fl(Current_radar_global->Radar_radius[gr_screen.res][0] - 5); if (hypotenuse >= max_radius) { clipped_x_dist = max_radius * (new_x_dist / hypotenuse); clipped_y_dist = max_radius * (new_y_dist / hypotenuse); new_x_dist = clipped_x_dist; new_y_dist = clipped_y_dist; } } xpos = fl2i(Current_radar_global->Radar_center[gr_screen.res][0] + new_x_dist); ypos = fl2i(Current_radar_global->Radar_center[gr_screen.res][1] - new_y_dist); // determine the range within which the radar blip is bright if (timestamp_elapsed(Radar_calc_bright_dist_timer)) { Radar_calc_bright_dist_timer = timestamp(1000); Radar_bright_range = player_farthest_weapon_range(); if (Radar_bright_range <= 0) Radar_bright_range = 1500.0f; } blip *b; int blip_bright = 0; int blip_type = 0; if (N_blips >= MAX_BLIPS) { // out of blips, don't plot //Gahhh, this is bloody annoying -WMC //Int3(); return; } b = &Blips[N_blips]; b->flags = 0; // bright if within range blip_bright = (dist <= Radar_bright_range); // flag the blip as a current target if it is if (OBJ_INDEX(objp) == Player_ai->target_objnum) { b->flags |= BLIP_CURRENT_TARGET; blip_bright = 1; } radar_stuff_blip_info_std(objp, blip_bright, &b->blip_color, &blip_type); if (blip_bright) list_append(&Blip_bright_list[blip_type], b); else list_append(&Blip_dim_list[blip_type], b); b->x = xpos; b->y = ypos; // see if blip should be drawn distorted if (objp->type == OBJ_SHIP) { // ships specifically hidden from sensors if (Ships[objp->instance].flags & SF_HIDDEN_FROM_SENSORS) b->flags |= BLIP_DRAW_DISTORTED; // determine if its AWACS distorted if (awacs_level < 1.0f) b->flags |= BLIP_DRAW_DISTORTED; } // don't distort the sensor blips if the player has primitive sensors and the nebula effect // is not active if (Player_ship->flags2 & SF2_PRIMITIVE_SENSORS) { if (!(The_mission.flags & MISSION_FLAG_FULLNEB)) b->flags &= ~BLIP_DRAW_DISTORTED; } N_blips++; }
// ------------------------------------------------------------------------------------------------- // update_ets() is called once per frame for every OBJ_SHIP in the game. The amount of energy // to send to the weapons and shields is calculated, and the top ship speed is calculated. The // amount of time elapsed from the previous call is passed in as the parameter fl_frametime. // // parameters: obj ==> object that is updating their energy system // fl_frametime ==> game frametime (in seconds) // void update_ets(object* objp, float fl_frametime) { float max_new_shield_energy, max_new_weapon_energy, _ss; if ( fl_frametime <= 0 ){ return; } ship* ship_p = &Ships[objp->instance]; ship_info* sinfo_p = &Ship_info[ship_p->ship_info_index]; float max_g=sinfo_p->max_weapon_reserve, max_s=ship_p->ship_max_shield_strength; if ( ship_p->flags & SF_DYING ){ return; } if ( sinfo_p->power_output == 0 ){ return; } // new_energy = fl_frametime * sinfo_p->power_output; // update weapon energy max_new_weapon_energy = fl_frametime * sinfo_p->max_weapon_regen_per_second * max_g; if ( objp->flags & OF_PLAYER_SHIP ) { ship_p->weapon_energy += Energy_levels[ship_p->weapon_recharge_index] * max_new_weapon_energy * The_mission.ai_profile->weapon_energy_scale[Game_skill_level]; } else { ship_p->weapon_energy += Energy_levels[ship_p->weapon_recharge_index] * max_new_weapon_energy; } if ( ship_p->weapon_energy > sinfo_p->max_weapon_reserve ){ ship_p->weapon_energy = sinfo_p->max_weapon_reserve; } float shield_delta; max_new_shield_energy = fl_frametime * sinfo_p->max_shield_regen_per_second * max_s; if ( objp->flags & OF_PLAYER_SHIP ) { shield_delta = Energy_levels[ship_p->shield_recharge_index] * max_new_shield_energy * The_mission.ai_profile->shield_energy_scale[Game_skill_level]; } else { shield_delta = Energy_levels[ship_p->shield_recharge_index] * max_new_shield_energy; } shield_add_strength(objp, shield_delta); if ( (_ss = shield_get_strength(objp)) > ship_p->ship_max_shield_strength ){ for (int i=0; i<objp->n_quadrants; i++){ objp->shield_quadrant[i] *= ship_p->ship_max_shield_strength / _ss; } } // calculate the top speed of the ship based on the energy flow to engines float y = Energy_levels[ship_p->engine_recharge_index]; ship_p->current_max_speed = ets_get_max_speed(objp, y); // AL 11-15-97: Rules for engine strength affecting max speed: // 1. if strength >= 0.5 no affect // 2. if strength < 0.5 then max_speed = sqrt(strength) // // This will translate to 71% max speed at 50% engines, and 31% max speed at 10% engines // float strength = ship_get_subsystem_strength(ship_p, SUBSYSTEM_ENGINE); // don't let engine strength affect max speed when playing on lowest skill level if ( (objp != Player_obj) || (Game_skill_level > 0) ) { if ( strength < SHIP_MIN_ENGINES_FOR_FULL_SPEED ) { ship_p->current_max_speed *= fl_sqrt(strength); } } if ( timestamp_elapsed(ship_p->next_manage_ets) ) { if ( !(objp->flags & OF_PLAYER_SHIP) ) { ai_manage_ets(objp); ship_p->next_manage_ets = timestamp(AI_MODIFY_ETS_INTERVAL); } else { if ( Weapon_energy_cheat ){ ship_p->weapon_energy = sinfo_p->max_weapon_reserve; } } } }
void HudGaugeRadarOrb::drawContactImage(vec3d *pnt, int rad, int idx, int clr_idx, float mult) { int h, w; float aspect_mp; // need to get bitmap info bm_get_info(idx, &w, &h); Assert(w > 0); // get multiplier if (h == w) { aspect_mp = 1.0f; } else { aspect_mp = (((float) h) / ((float) w)); } //gr_set_bitmap(idx,GR_ALPHABLEND_NONE,GR_BITBLT_MODE_NORMAL,1.0f); float sizef = fl_sqrt(vm_vec_dist(&Orb_eye_position, pnt) * 8.0f); // might need checks unless the targeted blip is always wanted to be larger float radius = (float) Radar_blip_radius_normal; if (sizef < radius) sizef = radius; //Make so no evil things happen Assert(mult > 0.0f); //modify size according to value from tables sizef *= mult; // animate the targeted icon - option 1 of highlighting the targets if ( rad == Radar_blip_radius_target ) { if (radar_target_id_flags & RTIF_PULSATE) { // use mask to make the darn thing work faster sizef *= 1.3f + (sinf(10 * f2fl(Missiontime)) * 0.3f); } if (radar_target_id_flags & RTIF_BLINK) { if (Missiontime & 8192) return; } if (radar_target_id_flags & RTIF_ENLARGE) { sizef *= 1.3f; } } if ( idx >= 0 ) { //g3_draw_polygon(pnt, &vmd_identity_matrix, sizef/35.0f, aspect_mp*sizef/35.0f, tmap_flags); material mat_params; material_set_unlit_color(&mat_params, idx, &gr_screen.current_color, true, false); g3_render_rect_oriented(&mat_params, pnt, &vmd_identity_matrix, sizef / 35.0f, aspect_mp * sizef / 35.0f); } if ( clr_idx >= 0 ) { //g3_draw_polygon(pnt, &vmd_identity_matrix, sizef/35.0f, aspect_mp*sizef/35.0f, TMAP_FLAG_TEXTURED | TMAP_HTL_3D_UNLIT); material mat_params; material_set_unlit(&mat_params, clr_idx, 1.0f, true, false); g3_render_rect_oriented(&mat_params, pnt, &vmd_identity_matrix, sizef / 35.0f, aspect_mp * sizef / 35.0f); } }