]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Person.cpp
Refactored weapon taking in Person::takeWeapon
[lugaru.git] / Source / Person.cpp
index 0580434f52f958d4617937fbce59b2c8cff13953..3e0f52807d6cbbc60397baa42a0fec7d1da92368 100644 (file)
@@ -893,15 +893,10 @@ void Person::Reverse()
     if (animTarget == staffhitanim && distsq(&victim->coords, &coords) < 2 && ((victim->id == 0 && victim->crouchkeydown) || Random() % 4 == 0)) {
         if (victim->weaponactive != -1) {
             victim->throwtogglekeydown = 1;
-            weapons[victim->weaponids[0]].owner = -1;
-            weapons[victim->weaponids[0]].velocity = victim->velocity * .2;
-            if (weapons[victim->weaponids[0]].velocity.x == 0)
-                weapons[victim->weaponids[0]].velocity.x = .1;
-            weapons[victim->weaponids[0]].tipvelocity = weapons[victim->weaponids[0]].velocity;
-            weapons[victim->weaponids[0]].missed = 1;
-            weapons[victim->weaponids[0]].freetime = 0;
-            weapons[victim->weaponids[0]].firstfree = 1;
-            weapons[victim->weaponids[0]].physics = 1;
+            XYZ tempVelocity = victim->velocity * .2;
+            if (tempVelocity.x == 0)
+                tempVelocity.x = .1;
+            weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false);
             victim->num_weapons--;
             if (victim->num_weapons) {
                 victim->weaponids[0] = victim->weaponids[victim->num_weapons];
@@ -923,15 +918,10 @@ void Person::Reverse()
     if (animTarget == staffspinhitanim && distsq(&victim->coords, &coords) < 2 && ((victim->id == 0 && victim->crouchkeydown) || Random() % 2 == 0)) {
         if (victim->weaponactive != -1) {
             victim->throwtogglekeydown = 1;
-            weapons[victim->weaponids[0]].owner = -1;
-            weapons[victim->weaponids[0]].velocity = victim->velocity * .2;
-            if (weapons[victim->weaponids[0]].velocity.x == 0)
-                weapons[victim->weaponids[0]].velocity.x = .1;
-            weapons[victim->weaponids[0]].tipvelocity = weapons[victim->weaponids[0]].velocity;
-            weapons[victim->weaponids[0]].missed = 1;
-            weapons[victim->weaponids[0]].freetime = 0;
-            weapons[victim->weaponids[0]].firstfree = 1;
-            weapons[victim->weaponids[0]].physics = 1;
+            XYZ tempVelocity = victim->velocity * .2;
+            if (tempVelocity.x == 0)
+                tempVelocity.x = .1;
+            weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false);
             victim->num_weapons--;
             if (victim->num_weapons) {
                 victim->weaponids[0] = victim->weaponids[victim->num_weapons];
@@ -952,15 +942,10 @@ void Person::Reverse()
     if (animTarget == swordslashanim && distsq(&victim->coords, &coords) < 2 && ((victim->id == 0 && victim->crouchkeydown) || Random() % 4 == 0)) {
         if (victim->weaponactive != -1) {
             victim->throwtogglekeydown = 1;
-            weapons[victim->weaponids[0]].owner = -1;
-            weapons[victim->weaponids[0]].velocity = victim->velocity * .2;
-            if (weapons[victim->weaponids[0]].velocity.x == 0)
-                weapons[victim->weaponids[0]].velocity.x = .1;
-            weapons[victim->weaponids[0]].tipvelocity = weapons[victim->weaponids[0]].velocity;
-            weapons[victim->weaponids[0]].missed = 1;
-            weapons[victim->weaponids[0]].freetime = 0;
-            weapons[victim->weaponids[0]].firstfree = 1;
-            weapons[victim->weaponids[0]].physics = 1;
+            XYZ tempVelocity = victim->velocity * .2;
+            if (tempVelocity.x == 0)
+                tempVelocity.x = .1;
+            weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false);
             victim->num_weapons--;
             if (victim->num_weapons) {
                 victim->weaponids[0] = victim->weaponids[victim->num_weapons];
@@ -981,15 +966,10 @@ void Person::Reverse()
     if (animTarget == knifeslashstartanim && distsq(&victim->coords, &coords) < 2 && (victim->id == 0 || Random() % 4 == 0)) {
         if (victim->weaponactive != -1) {
             victim->throwtogglekeydown = 1;
-            weapons[victim->weaponids[0]].owner = -1;
-            weapons[victim->weaponids[0]].velocity = victim->velocity * .2;
-            if (weapons[victim->weaponids[0]].velocity.x == 0)
-                weapons[victim->weaponids[0]].velocity.x = .1;
-            weapons[victim->weaponids[0]].tipvelocity = weapons[victim->weaponids[0]].velocity;
-            weapons[victim->weaponids[0]].missed = 1;
-            weapons[victim->weaponids[0]].freetime = 0;
-            weapons[victim->weaponids[0]].firstfree = 1;
-            weapons[victim->weaponids[0]].physics = 1;
+            XYZ tempVelocity = victim->velocity * .2;
+            if (tempVelocity.x == 0)
+                tempVelocity.x = .1;
+            weapons[victim->weaponids[0]].drop(tempVelocity, tempVelocity, false);
             victim->num_weapons--;
             if (victim->num_weapons) {
                 victim->weaponids[0] = victim->weaponids[victim->num_weapons];
@@ -1064,16 +1044,9 @@ void Person::Reverse()
             victim->animTarget = staggerbackhighanim;
             victim->targetyaw = targetyaw + 180;
             victim->target = 0;
-            weapons[victim->weaponids[0]].owner = -1;
             aim = DoRotation(facing, 0, 90, 0) * 21;
             aim.y += 7;
-            weapons[victim->weaponids[0]].velocity = aim * -.2;
-            weapons[victim->weaponids[0]].tipvelocity = aim;
-            weapons[victim->weaponids[0]].missed = 1;
-            weapons[victim->weaponids[0]].hitsomething = 0;
-            weapons[victim->weaponids[0]].freetime = 0;
-            weapons[victim->weaponids[0]].firstfree = 1;
-            weapons[victim->weaponids[0]].physics = 1;
+            weapons[victim->weaponids[0]].drop(aim * -.2, aim);
             victim->num_weapons--;
             if (victim->num_weapons) {
                 victim->weaponids[0] = victim->weaponids[num_weapons];
@@ -1107,16 +1080,9 @@ void Person::Reverse()
             animTarget = staggerbackhighanim;
             targetyaw = targetyaw + 180;
             target = 0;
-            weapons[weaponids[0]].owner = -1;
             aim = DoRotation(facing, 0, 90, 0) * 21;
             aim.y += 7;
-            weapons[weaponids[0]].velocity = aim * -.2;
-            weapons[weaponids[0]].tipvelocity = aim;
-            weapons[weaponids[0]].hitsomething = 0;
-            weapons[weaponids[0]].missed = 1;
-            weapons[weaponids[0]].freetime = 0;
-            weapons[weaponids[0]].firstfree = 1;
-            weapons[weaponids[0]].physics = 1;
+            weapons[victim->weaponids[0]].drop(aim * -.2, aim);
             num_weapons--;
             if (num_weapons) {
                 weaponids[0] = weaponids[num_weapons];
@@ -1528,15 +1494,8 @@ void Person::RagDoll(bool checkcollision)
         // drop weapon
         if (Random() % 2 == 0) {
             if (weaponactive != -1 && animTarget != rabbitkickanim && num_weapons > 0) {
-                weapons[weaponids[0]].owner = -1;
-                weapons[weaponids[0]].hitsomething = 0;
-                weapons[weaponids[0]].velocity = jointVel(righthand) * scale * -.3;
+                weapons[weaponids[0]].drop(jointVel(righthand) * scale * -.3, jointVel(righthand) * scale);
                 weapons[weaponids[0]].velocity.x += .01;
-                weapons[weaponids[0]].tipvelocity = jointVel(righthand) * scale;
-                weapons[weaponids[0]].missed = 1;
-                weapons[weaponids[0]].freetime = 0;
-                weapons[weaponids[0]].firstfree = 1;
-                weapons[weaponids[0]].physics = 1;
                 num_weapons--;
                 if (num_weapons) {
                     weaponids[0] = weaponids[num_weapons];
@@ -1898,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);
                             }
                         }
                 }
@@ -1935,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;
@@ -1982,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);
                             }
                         }
                 }
@@ -2666,16 +2612,9 @@ void Person::DoAnimations()
                     if (weaponactive != -1) {
                         escapednum = 0;
                         XYZ aim;
-                        weapons[weaponids[0]].owner = -1;
                         aim = victim->coords + DoRotation(victim->jointPos(abdomen), 0, victim->yaw, 0) * victim->scale + victim->velocity * findDistance(&victim->coords, &coords) / 50 - (coords + DoRotation(jointPos(righthand), 0, yaw, 0) * scale);
                         Normalise(&aim);
-                        weapons[weaponids[0]].velocity = aim * 50;
-                        weapons[weaponids[0]].tipvelocity = aim * 50;
-                        weapons[weaponids[0]].missed = 0;
-                        weapons[weaponids[0]].hitsomething = 0;
-                        weapons[weaponids[0]].freetime = 0;
-                        weapons[weaponids[0]].firstfree = 1;
-                        weapons[weaponids[0]].physics = 0;
+                        weapons[weaponids[0]].thrown(aim * 50);
                         num_weapons--;
                         if (num_weapons) {
                             weaponids[0] = weaponids[num_weapons];
@@ -2810,16 +2749,9 @@ void Person::DoAnimations()
                             victim->animTarget = staggerbackhighanim;
                             victim->targetyaw = targetyaw + 180;
                             victim->target = 0;
-                            weapons[victim->weaponids[0]].owner = -1;
                             aim = DoRotation(facing, 0, 90, 0) * 21;
                             aim.y += 7;
-                            weapons[victim->weaponids[0]].velocity = aim * -.2;
-                            weapons[victim->weaponids[0]].tipvelocity = aim;
-                            weapons[victim->weaponids[0]].missed = 1;
-                            weapons[weaponids[0]].hitsomething = 0;
-                            weapons[victim->weaponids[0]].freetime = 0;
-                            weapons[victim->weaponids[0]].firstfree = 1;
-                            weapons[victim->weaponids[0]].physics = 1;
+                            weapons[victim->weaponids[0]].drop(aim * -.2, aim);
                             victim->num_weapons--;
                             if (victim->num_weapons) {
                                 victim->weaponids[0] = victim->weaponids[num_weapons];
@@ -3115,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];
@@ -4268,15 +4194,8 @@ void Person::DoStuff()
             deathbleeding = 0;
         if (bloodloss > damagetolerance && animation[animTarget].attack == neutral) {
             if (weaponactive != -1) {
-                weapons[weaponids[0]].owner = -1;
-                weapons[weaponids[0]].velocity = velocity * scale * -.3;
+                weapons[weaponids[0]].drop(velocity * scale * -.3, velocity * scale);
                 weapons[weaponids[0]].velocity.x += .01;
-                weapons[weaponids[0]].tipvelocity = velocity * scale;
-                weapons[weaponids[0]].missed = 1;
-                weapons[weaponids[0]].hitsomething = 0;
-                weapons[weaponids[0]].freetime = 0;
-                weapons[weaponids[0]].firstfree = 1;
-                weapons[weaponids[0]].physics = 1;
                 num_weapons--;
                 if (num_weapons) {
                     weaponids[0] = weaponids[num_weapons];
@@ -4694,15 +4613,8 @@ void Person::DoStuff()
         RagDoll(0);
 
         if (weaponactive != -1) {
-            weapons[weaponids[0]].owner = -1;
-            weapons[weaponids[0]].velocity = velocity * scale * -.3;
+            weapons[weaponids[0]].drop(velocity * scale * -.3, velocity * scale);
             weapons[weaponids[0]].velocity.x += .01;
-            weapons[weaponids[0]].tipvelocity = velocity * scale;
-            weapons[weaponids[0]].missed = 1;
-            weapons[weaponids[0]].hitsomething = 0;
-            weapons[weaponids[0]].freetime = 0;
-            weapons[weaponids[0]].firstfree = 1;
-            weapons[weaponids[0]].physics = 1;
             num_weapons--;
             if (num_weapons) {
                 weaponids[0] = weaponids[num_weapons];
@@ -4757,15 +4669,8 @@ void Person::DoStuff()
         DoBlood(1, 255);
 
         if (weaponactive != -1) {
-            weapons[weaponids[0]].owner = -1;
-            weapons[weaponids[0]].velocity = velocity * scale * -.3;
+            weapons[weaponids[0]].drop(velocity * scale * -.3, velocity * scale);
             weapons[weaponids[0]].velocity.x += .01;
-            weapons[weaponids[0]].tipvelocity = velocity * scale;
-            weapons[weaponids[0]].missed = 1;
-            weapons[weaponids[0]].hitsomething = 0;
-            weapons[weaponids[0]].freetime = 0;
-            weapons[weaponids[0]].firstfree = 1;
-            weapons[weaponids[0]].physics = 1;
             num_weapons--;
             if (num_weapons) {
                 weaponids[0] = weaponids[num_weapons];
@@ -6667,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;
+}
+