#include "precompile.h" #include #include #include #include "shot.h" #include "creature.h" #include "room.h" #include "car.h" #include "trigger.h" #include "buff.h" #include "mt/Param.h" #include "mt/Hero.h" #include "mt/Equip.h" #include "mt/Buff.h" #include "mt/Skill.h" #include "mt/SkillNumber.h" static const auto hero_transform = glm::rotate( glm::mat4(1.0), //glm::radians(65.0f), glm::radians(0.0f), glm::vec3(0.0, 0.0, 1.0) ); struct BulletInfo { CreatureWeakPtr c; long long weapon_uniid = 0; const mt::Equip* weapon_meta = nullptr; const mt::Equip* bullet_meta = nullptr; const mt::Skill* skill_meta = nullptr; Position bullet_born_pos; glm::vec3 bullet_dir = GlmHelper::ZERO; float fly_distance = 0; int weapon_lv = 0; int delay_time = 0; float recoil_force = 0; int invincible_buff_uniid = 0; int trace_target_uniid = 0; float track_change_time = 0; int is_through = 0; int hand = 0; float shot_animi_time = 0; const mt::HeroShotAnimation* shot_animi = nullptr; int bullet_idx = 0; int bullet_num = 0; a8::XTimerWp keep_shot_animi_timer_ptr; }; static void CalcGunMuzzlePosition(Creature* c, const mt::Equip* weapon_meta, const mt::HeroShotAnimation* shot_animi, glm::vec4& gun_muzzle_position, int bulletIdx, int bulletNum) { if (shot_animi) { if (weapon_meta->double_gun()&& bulletIdx > (int)(bulletNum / 2)) { gun_muzzle_position += glm::vec4( shot_animi->l_x, shot_animi->l_y, shot_animi->l_z, 0 ); } else { if (c->IsCar() && !weapon_meta->double_gun()) { switch (c->shot_hole) { case 1: { gun_muzzle_position += glm::vec4( shot_animi->p3_x, shot_animi->p3_y, shot_animi->p3_z, 0 ); } break; case 2: { gun_muzzle_position += glm::vec4( shot_animi->p4_x, shot_animi->p4_y, shot_animi->p4_z, 0 ); } break; case 3: { gun_muzzle_position += glm::vec4( shot_animi->p5_x, shot_animi->p5_y, shot_animi->p5_z, 0 ); } break; default: { } break; } } else { gun_muzzle_position += glm::vec4( shot_animi->r_x, shot_animi->r_y, shot_animi->r_z, 0 ); } } }//end if if (weapon_meta->_gun_muzzle_position) { gun_muzzle_position += glm::vec4( -std::get<0>(*weapon_meta->_gun_muzzle_position.get()), std::get<1>(*weapon_meta->_gun_muzzle_position.get()), std::get<2>(*weapon_meta->_gun_muzzle_position.get()), 0 ); } } static void InternalCreateBullet(BulletInfo& bullet_info) { if (!bullet_info.c.Get()) { return; } Creature* c = bullet_info.c.Get(); if (c->dead) { return; } if (c->downed) { return; } if (bullet_info.delay_time <= 0) { if (c->GetCurrWeapon()->meta->bullet_consume_type() == kBulletConsumeMulti) { if (c->GetCurrWeapon()->ammo <= 0) { return; } --c->GetCurrWeapon()->ammo; if (c->GetCurrWeapon()->ammo <= 0) { c->AutoLoadingBullet(); } if ((c->IsPlayer() || c->IsCar())) { c->room->frame_event.AddBulletNumChg(c->GetWeakPtrRef()); c->room->frame_event.AddWeaponAmmoChg(c->GetWeakPtrRef()); } } if (bullet_info.recoil_force > 0) { if (c->GetCurrWeapon()->ammo <= 0) { c->DoRecoilForce(bullet_info.recoil_force); bullet_info.bullet_born_pos.FromGlmVec3(bullet_info.bullet_born_pos.ToGlmVec3() - (bullet_info.bullet_dir * bullet_info.recoil_force)); } } if (c->IsCar() && c->AsCar()->IsSingle()) { glm::vec3 bullet_born_offset; bool is_player = c->IsPlayer(); bool is_car = c->IsCar(); float bullet_born_angle = GlmHelper::CalcAngle(c->GetAttackDir(), GlmHelper::RIGHT); if (c->GetAttackDir().y > 0.00001f) { bullet_born_angle = -bullet_born_angle; } float old_bullet_born_angle = bullet_born_angle; GlmHelper::RotateY(bullet_born_offset, bullet_born_angle); auto transform = glm::rotate(hero_transform, bullet_born_angle * A8_PI, glm::vec3(0.0, 1.0, 0.0)); glm::vec4 gun_muzzle_position(0.0, 0.0, 0.0, 0.0); CalcGunMuzzlePosition(c, bullet_info.weapon_meta, bullet_info.shot_animi, gun_muzzle_position, bullet_info.bullet_idx, bullet_info.bullet_num); glm::vec4 v = transform * gun_muzzle_position; bullet_born_offset = glm::vec3(v.z *10*1, 0.0f, v.x*10*-1); bullet_info.bullet_born_pos.FromGlmVec3(c->GetPos().ToGlmVec3() + bullet_born_offset); bullet_info.bullet_dir = c->GetShotDir(); } int bullet_uniid = 0; if (mt::Param::s().prebattle_can_use_skill || !(c->HasBuffEffect(kBET_Jump) || c->HasBuffEffect(kBET_Fly))) { bullet_uniid = c->room->CreateBullet (c, c->shot_passenger, bullet_info.weapon_meta, bullet_info.bullet_meta, bullet_info.skill_meta, bullet_info.bullet_born_pos, bullet_info.bullet_dir, bullet_info.fly_distance, bullet_info.weapon_uniid, bullet_info.trace_target_uniid, bullet_info.hand, bullet_info.keep_shot_animi_timer_ptr, bullet_info.shot_animi_time); #ifdef DEBUG1 if (bullet_info.c.Get()->IsPlayer()) { bullet_info.c.Get()->SendDebugMsg(a8::Format("CreateBullet id:%d", {bullet_info.weapon_meta->id()})); } #endif } bullet_uniid = bullet_uniid ? bullet_uniid : c->room->AllocUniid(); #ifdef DEBUG if (c->IsPlayer()) { a8::XPrintf("bullet_born_pos:%f,%f,%f bullet_dir:%f,%f,%f pos:%f,%f,%f\n", { bullet_info.bullet_born_pos.x, bullet_info.bullet_born_pos.y, bullet_info.bullet_born_pos.z, bullet_info.bullet_dir.x, bullet_info.bullet_dir.y, bullet_info.bullet_dir.z, c->GetPos().GetX(), c->GetPos().GetY(), c->GetPos().GetZ(), }); } #endif c->room->frame_event.AddBullet (bullet_uniid, c->GetWeakPtrRef(), bullet_info.weapon_meta, bullet_info.weapon_lv, bullet_info.bullet_born_pos, bullet_info.bullet_dir, bullet_info.fly_distance, bullet_info.trace_target_uniid, bullet_info.hand); if (bullet_uniid && bullet_info.trace_target_uniid) { c->AddTraceBullet( bullet_uniid, bullet_info.trace_target_uniid, bullet_info.weapon_meta->id() ); } } else { std::shared_ptr info_copy = std::make_shared(); *info_copy = bullet_info; auto timer = bullet_info.c.Get()->room->xtimer.SetTimeoutWpEx ( bullet_info.delay_time / FRAME_RATE_MS, [info_copy] (int event, const a8::Args* args) { if (a8::TIMER_EXEC_EVENT == event) { info_copy->delay_time = 0; InternalCreateBullet(*info_copy); } }, &bullet_info.c.Get()->xtimer_attacher); if (!c->room->BattleStarted()) { c->room->AddToPostBattleAutoFreeList(timer); } } } static void ProcMissile(Creature* c, const mt::Equip* weapon_meta, const mt::Equip* bullet_meta, const mt::Skill* skill_meta, float fly_distance, long long weapon_uniid, int trace_target_uniid) { if (!skill_meta) { c->room->frame_event.AddShot(c->GetWeakPtrRef()); } if (c->aiming) { if (weapon_meta->aiming_cast_time() > 0) { int buff_uniid = c->TryAddBuff(c, kVertigoBuffId); Buff* buff = c->GetBuffByUniId(buff_uniid); if (buff && !buff->remover_timer.expired()) { c->room->xtimer.ModifyTime(buff->remover_timer, weapon_meta->aiming_cast_time() / FRAME_RATE_MS); } } } else { if (weapon_meta->cast_time() > 0) { int buff_uniid = c->TryAddBuff(c, kVertigoBuffId); Buff* buff = c->GetBuffByUniId(buff_uniid); if (buff && !buff->remover_timer.expired()) { c->room->xtimer.ModifyTime(buff->remover_timer, weapon_meta->cast_time() / FRAME_RATE_MS); } } } if (weapon_meta->_bullet_born_offset.empty()) { return; } const mt::Buff * buff_meta = mt::Buff::GetById(bullet_meta->buffid()); if (buff_meta) { typedef struct { glm::vec3 old_context_dir; Position old_context_pos; } _T; auto context = std::make_shared<_T>(); DelayAddBuffHandle* handle = new DelayAddBuffHandle; handle->pre_add_cb = [fly_distance, context] (Creature* c) { context->old_context_dir = c->context_dir; context->old_context_pos = c->context_pos; c->context_dir = c->GetAttackDir(); c->context_pos.FromGlmVec3(c->GetPos().ToGlmVec3() + c->GetAttackDir() * fly_distance); }; handle->post_add_cb = [context] (Creature* c, int buff_uniid) { c->context_dir = context->old_context_dir; c->context_pos = context->old_context_pos; }; auto tuple = weapon_meta->_bullet_born_offset.at(0); c->TryDelayAddBuff(c, bullet_meta->buffid(), std::get<3>(tuple), handle); } } void InternalShot(Creature* c, const mt::Equip* weapon_meta, const mt::Equip* bullet_meta, const mt::Skill* skill_meta, float fly_distance, long long weapon_uniid, int trace_target_uniid) { if (weapon_meta->_inventory_slot() == IS_TRAP || weapon_meta->_inventory_slot() == IS_MINE) { ProcMissile(c, weapon_meta, bullet_meta, skill_meta, fly_distance, weapon_uniid, trace_target_uniid); return; } for (auto& tuple : weapon_meta->_bullet_born_offset) { glm::vec3 bullet_born_offset = glm::vec3(std::get<0>(tuple), 0.0f, std::get<1>(tuple)); float bullet_born_angle = GlmHelper::CalcAngle(c->GetAttackDir(), GlmHelper::UP); if (c->GetAttackDir().x > 0.00001f) { bullet_born_angle = -bullet_born_angle; } GlmHelper::RotateY(bullet_born_offset, bullet_born_angle); Position bullet_born_pos; bullet_born_pos.FromGlmVec3(c->GetPos().ToGlmVec3() + bullet_born_offset); if (c->room->OverBorder(bullet_born_pos, 0.0f)) { return; } } #if 1 c->room->frame_event.AddShot(c->GetWeakPtrRef()); #else if (skill_meta || weapon_meta->equip_type() == EQUIP_TYPE_THROW) { c->room->frame_event.AddShot(c->GetWeakPtrRef()); } #endif int invincible_buff_uniid = 0; if (weapon_meta->lock_time > 0) { int buff_uniid = c->TryAddBuff(c, kVertigoBuffId); Buff* buff = c->GetBuffByUniId(buff_uniid); if (buff && !buff->remover_timer.expired()) { c->room->xtimer.ModifyTime(buff->remover_timer, weapon_meta->lock_time / FRAME_RATE_MS); } } if (c->aiming) { if (weapon_meta->aiming_cast_time() > 0) { int buff_uniid = c->TryAddBuff(c, kVertigoBuffId); Buff* buff = c->GetBuffByUniId(buff_uniid); if (buff && !buff->remover_timer.expired()) { c->room->xtimer.ModifyTime(buff->remover_timer, weapon_meta->aiming_cast_time() / FRAME_RATE_MS); } } } else { if (weapon_meta->cast_time() > 0) { int buff_uniid = c->TryAddBuff(c, kVertigoBuffId); Buff* buff = c->GetBuffByUniId(buff_uniid); if (buff && !buff->remover_timer.expired()) { c->room->xtimer.ModifyTime(buff->remover_timer, weapon_meta->cast_time() / FRAME_RATE_MS); } } } const mt::HeroShotAnimation* shot_animi = c->GetHeroMeta() ? c->GetHeroMeta()->GetShotAnimi(weapon_meta->shootfire()) : nullptr; a8::XTimerWp keep_shot_animi_timer_ptr; { int shot_animi_time = (shot_animi ? shot_animi->t : 0); if (weapon_meta && weapon_meta->equip_subtype() == GUN_SUB_EQUIP_TYPE_FLY_HOOk) { keep_shot_animi_timer_ptr = c->TryDelayAddBuff(c, kKeepShotAnimiBuffId, shot_animi_time); } } int bulletIdx = 0; int bulletNum = weapon_meta->_bullet_born_offset.size(); for (auto& tuple : weapon_meta->_bullet_born_offset) { ++bulletIdx; glm::vec3 bullet_born_offset = glm::vec3(std::get<0>(tuple), 0.0f, std::get<1>(tuple)); glm::vec3 bullet_born_pos = c->GetPos().ToGlmVec3() + c->shoot_offset + bullet_born_offset; glm::vec3 bullet_dir = c->GetShotDir(); float bullet_angle = std::get<2>(tuple); if (weapon_meta->bullet_angle() >= 0.10f) { int angle = (int)weapon_meta->bullet_angle() * 1000; if (angle > 0) { bullet_angle += (rand() % angle) / 1000.0f * (rand() % 2 == 0 ? 1 : -1); } } GlmHelper::RotateY(bullet_dir, bullet_angle / 180.0f); int shot_animi_time = (shot_animi ? shot_animi->t : 0); if (shot_animi_time > 0) { shot_animi_time = std::max(100, shot_animi_time); } { bool is_player = c->IsPlayer(); bool is_car = c->IsCar(); float bullet_born_angle = GlmHelper::CalcAngle(c->GetAttackDir(), GlmHelper::RIGHT); if (c->GetAttackDir().y > 0.00001f) { bullet_born_angle = -bullet_born_angle; } float old_bullet_born_angle = bullet_born_angle; GlmHelper::RotateY(bullet_born_offset, bullet_born_angle); auto transform = glm::rotate(hero_transform, bullet_born_angle * A8_PI, glm::vec3(0.0, 1.0, 0.0)); glm::vec4 gun_muzzle_position(0.0, 0.0, 0.0, 0.0); CalcGunMuzzlePosition(c, weapon_meta, shot_animi, gun_muzzle_position, bulletIdx, bulletNum); glm::vec4 v = transform * gun_muzzle_position; bullet_born_offset = glm::vec3(v.z *10*1, 0.0f, v.x*10*-1); bullet_born_pos = c->GetPos().ToGlmVec3() + bullet_born_offset; if (c->IsPlayer() || c->IsCar()) { #ifdef DEBUG a8::XPrintf("idx:%d offset:%f,%f angle:%f old_angle:%f angle_xy:%f,%f %f %f gun_muzzle_position:%f,%f,%f pos:%f,%f,%f gun_id:%d t:%d\n", { bulletIdx, bullet_born_offset.x, bullet_born_offset.y, bullet_born_angle, old_bullet_born_angle, c->GetAttackDir().x, c->GetAttackDir().y, (bullet_born_angle * 180), (bullet_born_angle - glm::radians(90.0f) / A8_PI) * 180, gun_muzzle_position.x, gun_muzzle_position.y, gun_muzzle_position.z, bullet_born_pos.x, bullet_born_pos.y, bullet_born_pos.z, weapon_meta->id(), shot_animi_time }); #endif } } { BulletInfo bullet_info; bullet_info.c = c->GetWeakPtrRef(); bullet_info.weapon_uniid = weapon_uniid; bullet_info.weapon_meta = weapon_meta; bullet_info.skill_meta = skill_meta; bullet_info.bullet_meta = bullet_meta; bullet_info.bullet_born_pos.FromGlmVec3(bullet_born_pos); bullet_info.bullet_dir = bullet_dir; bullet_info.fly_distance = fly_distance; bullet_info.delay_time = std::get<3>(tuple); bullet_info.recoil_force = std::get<4>(tuple); bullet_info.invincible_buff_uniid = invincible_buff_uniid; bullet_info.trace_target_uniid = trace_target_uniid; bullet_info.keep_shot_animi_timer_ptr = keep_shot_animi_timer_ptr; bullet_info.shot_animi_time = shot_animi_time; bullet_info.shot_animi = shot_animi; bullet_info.bullet_idx = bulletIdx; bullet_info.bullet_num = bulletNum; if (skill_meta && (skill_meta->GetMagicId() == MAGIC_AXXF || skill_meta->GetMagicId() == MAGIC_HJHX) ) { bullet_info.trace_target_uniid = c->GetSkillTargetId(); } if (weapon_meta->double_gun() && bulletIdx > (int)(bulletNum / 2)) { bullet_info.hand = 1; } #ifdef DEBUG if (bullet_info.trace_target_uniid) { a8::XPrintf("bullet trace_target_uniid:%d\n", {bullet_info.trace_target_uniid}); } #endif if (bullet_info.skill_meta && bullet_info.skill_meta->_number_meta) { c->GetTrigger()->SkillBulletPreCreate(bullet_info.delay_time, bullet_info.skill_meta); } InternalCreateBullet(bullet_info); } } c->GetTrigger()->Shot(weapon_meta); if (weapon_meta->recoil_force() > 0.000001) { c->DoRecoilForce(weapon_meta->recoil_force()); } if (c->HasBuffEffect(kBET_Hide)) { c->RemoveBuffByEffectId(kBET_Hide); } if (trace_target_uniid) { c->LockAttackDir(1000); } if (c->aiming) { c->aiming = false; c->aiming_frameno = 0; c->power_idx = -1; c->ClearAimingBuffs(); } }