From bf99f4428d07954f6586112f211ac4fb63946331 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Fri, 25 Nov 2016 13:41:54 +0800 Subject: [PATCH] Refactored weapon taking in Person::takeWeapon --- Source/GameTick.cpp | 22 +++------------------- Source/Person.cpp | 36 ++++++++++++++---------------------- Source/Person.h | 2 ++ Source/Weapons.cpp | 8 +------- 4 files changed, 20 insertions(+), 48 deletions(-) diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index e156114..4da057e 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -6370,12 +6370,7 @@ void Game::Tick() if (weapons[j].getType() != staff) emit_sound_at(knifedrawsound, Person::players[i]->coords, 128.); - Person::players[i]->weaponactive = 0; - weapons[j].owner = Person::players[i]->id; - if (Person::players[i]->num_weapons > 0) - Person::players[i]->weaponids[Person::players[i]->num_weapons] = Person::players[i]->weaponids[0]; - Person::players[i]->num_weapons++; - Person::players[i]->weaponids[0] = j; + Person::players[i]->takeWeapon(j); } } } else if ((Person::players[i]->isIdle() || @@ -6404,12 +6399,7 @@ void Game::Tick() if (weapons[k].getType() != staff) emit_sound_at(knifedrawsound, Person::players[i]->coords, 128.); - Person::players[i]->weaponactive = 0; - weapons[k].owner = Person::players[i]->id; - if (Person::players[i]->num_weapons > 0) - Person::players[i]->weaponids[Person::players[i]->num_weapons] = Person::players[i]->weaponids[0]; - Person::players[i]->num_weapons++; - Person::players[i]->weaponids[0] = k; + Person::players[i]->takeWeapon(k); } } } @@ -6461,7 +6451,6 @@ void Game::Tick() if (fleshstuck) emit_sound_at(fleshstabremovesound, Person::players[i]->coords, 128.); - Person::players[i]->weaponactive = 0; if (weapons[k].owner != -1) { if (Person::players[i]->victim->num_weapons == 1) Person::players[i]->victim->num_weapons = 0; @@ -6509,12 +6498,7 @@ void Game::Tick() Person::players[i]->victim->jointVel(rightshoulder) += relative * 6; Person::players[i]->victim->jointVel(leftshoulder) += relative * 6; } - weapons[k].owner = i; - if (Person::players[i]->num_weapons > 0) { - Person::players[i]->weaponids[Person::players[i]->num_weapons] = Person::players[i]->weaponids[0]; - } - Person::players[i]->num_weapons++; - Person::players[i]->weaponids[0] = k; + Person::players[i]->takeWeapon(k); } } } diff --git a/Source/Person.cpp b/Source/Person.cpp index b1ebde7..3e0f528 100644 --- a/Source/Person.cpp +++ b/Source/Person.cpp @@ -1857,13 +1857,7 @@ void Person::DoAnimations() emit_sound_at(knifedrawsound, coords, 128.); } - weaponactive = 0; - weapons[i].owner = id; - if (num_weapons > 0) { - weaponids[num_weapons] = weaponids[0]; - } - num_weapons++; - weaponids[0] = i; + takeWeapon(i); } } } @@ -1894,9 +1888,7 @@ void Person::DoAnimations() emit_sound_at(knifedrawsound, coords, 128.); } } - weaponactive = 0; if (weapons[i].owner != -1) { - victim = Person::players[weapons[i].owner]; if (victim->num_weapons == 1) victim->num_weapons = 0; @@ -1941,12 +1933,7 @@ void Person::DoAnimations() victim->jointVel(rightshoulder) += relative * 6; victim->jointVel(leftshoulder) += relative * 6; } - weapons[i].owner = id; - if (num_weapons > 0) { - weaponids[num_weapons] = weaponids[0]; - } - num_weapons++; - weaponids[0] = i; + takeWeapon(i); } } } @@ -3060,13 +3047,7 @@ void Person::DoAnimations() if ((animTarget == swordslashreversalanim || animTarget == knifeslashreversalanim || animTarget == staffhitreversalanim || animTarget == staffspinhitreversalanim) && animation[animTarget].label[frameCurrent] == 5) { if (victim->weaponactive != -1 && victim->num_weapons > 0) { if (weapons[victim->weaponids[victim->weaponactive]].owner == int(victim->id)) { - weapons[victim->weaponids[victim->weaponactive]].owner = id; - weaponactive = 0; - if (num_weapons > 0) { - weaponids[num_weapons] = weaponids[victim->weaponactive]; - } - num_weapons++; - weaponids[0] = victim->weaponids[victim->weaponactive]; + takeWeapon(victim->weaponids[victim->weaponactive]); victim->num_weapons--; if (victim->num_weapons > 0) { victim->weaponids[victim->weaponactive] = victim->weaponids[victim->num_weapons]; @@ -6591,3 +6572,14 @@ int Person::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate, return firstintersecting; } +void Person::takeWeapon(int weaponId) +{ + weaponactive = 0; + weapons[weaponId].owner = id; + if (num_weapons > 0) { + weaponids[num_weapons] = weaponids[0]; + } + num_weapons++; + weaponids[0] = weaponId; +} + diff --git a/Source/Person.h b/Source/Person.h index 9f5e729..235f14d 100644 --- a/Source/Person.h +++ b/Source/Person.h @@ -393,6 +393,8 @@ public: void setAnimation(int); void DoAnimations(); void RagDoll(bool checkcollision); + + void takeWeapon (int weaponId); }; const int maxplayers = 10; diff --git a/Source/Weapons.cpp b/Source/Weapons.cpp index 5207997..d876320 100644 --- a/Source/Weapons.cpp +++ b/Source/Weapons.cpp @@ -225,16 +225,10 @@ void Weapon::DoStuff(int i) (Person::players[j]->creature == rabbittype && Random() % 2 == 0 && Person::players[j]->aitype == attacktypecutoff && Person::players[j]->weaponactive == -1)) { emit_sound_at(knifedrawsound, Person::players[j]->coords, 128.); - Person::players[j]->weaponactive = 0; Person::players[j]->animTarget = removeknifeanim; Person::players[j]->frameTarget = 1; Person::players[j]->target = 1; - owner = Person::players[j]->id; - if (Person::players[j]->num_weapons > 0) { - Person::players[j]->weaponids[Person::players[j]->num_weapons] = Person::players[j]->weaponids[0]; - } - Person::players[j]->num_weapons++; - Person::players[j]->weaponids[0] = i; + Person::players[j]->takeWeapon(i); Person::players[j]->aitype = attacktypecutoff; } else { -- 2.39.5