From: Côme Chilliet Date: Sun, 8 Jan 2017 17:43:28 +0000 (+0100) Subject: Replaced patchobjects arrays by vectors, cleaned code a bit X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=1a6ef830cdd01a9b7a8bbcfde8a51b7c430671a3;p=lugaru.git Replaced patchobjects arrays by vectors, cleaned code a bit --- diff --git a/Source/Animation/Skeleton.cpp b/Source/Animation/Skeleton.cpp index 1c88255..19150fb 100644 --- a/Source/Animation/Skeleton.cpp +++ b/Source/Animation/Skeleton.cpp @@ -111,7 +111,6 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale) XYZ bounceness; const int numrepeats = 3; float groundlevel = .15; - int m; unsigned i; XYZ temp; XYZ terrainnormal; @@ -321,112 +320,110 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale) if (longdead > 100) broken = 1; } - if (terrain.patchobjectnum[whichpatchx][whichpatchz]) - for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) { - unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][m]; - if (k < Object::objects.size()) { - if (Object::objects[k]->possible) { - friction = Object::objects[k]->friction; - XYZ start = joints[i].realoldposition; - XYZ end = joints[i].position * (*scale) + *coords; - whichhit = Object::objects[k]->model.LineCheckPossible(&start, &end, &temp, &Object::objects[k]->position, &Object::objects[k]->yaw); - if (whichhit != -1) { - if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) { - joints[i].locked = 1; - joints[i].delay = 1; - if (!Tutorial::active || id == 0) { - emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.); - } - breaking = true; + for (unsigned int m = 0; m < terrain.patchobjects[whichpatchx][whichpatchz].size(); m++) { + unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][m]; + if (k < Object::objects.size()) { + if (Object::objects[k]->possible) { + friction = Object::objects[k]->friction; + XYZ start = joints[i].realoldposition; + XYZ end = joints[i].position * (*scale) + *coords; + whichhit = Object::objects[k]->model.LineCheckPossible(&start, &end, &temp, &Object::objects[k]->position, &Object::objects[k]->yaw); + if (whichhit != -1) { + if (joints[i].label == groin && !joints[i].locked && joints[i].delay <= 0) { + joints[i].locked = 1; + joints[i].delay = 1; + if (!Tutorial::active || id == 0) { + emit_sound_at(landsound1, joints[i].position * (*scale) + *coords, 128.); } + breaking = true; + } - if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) { - joints[i].locked = 1; - joints[i].delay = 1; - if (!Tutorial::active || id == 0) { - emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.); - } + if (joints[i].label == head && !joints[i].locked && joints[i].delay <= 0) { + joints[i].locked = 1; + joints[i].delay = 1; + if (!Tutorial::active || id == 0) { + emit_sound_at(landsound2, joints[i].position * (*scale) + *coords, 128.); } + } - terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1; - if (terrainnormal.y > .8) - freefall = 0; - bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal))); - if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) { - bounceness = 0; - joints[i].velocity = joints[i].oldvelocity; - } - if (!Tutorial::active || id == 0) - if (findLengthfast(&bounceness) > 4000 && breaking) { - Object::objects[k]->model.MakeDecal(breakdecal, DoRotation(temp - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), .4, .5, Random() % 360); - Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2); - breaking = false; - camerashake += .6; - - emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords); - - addEnvSound(*coords, 64); - } - if (Object::objects[k]->type == treetrunktype) { - Object::objects[k]->rotx += joints[i].velocity.x * multiplier * .4; - Object::objects[k]->roty += joints[i].velocity.z * multiplier * .4; - Object::objects[k + 1]->rotx += joints[i].velocity.x * multiplier * .4; - Object::objects[k + 1]->roty += joints[i].velocity.z * multiplier * .4; - } - if (!joints[i].locked) - damage += findLengthfast(&bounceness) / 2500; - ReflectVector(&joints[i].velocity, &terrainnormal); - frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal)); - joints[i].velocity -= bounceness; - if (1 - friction * frictionness > 0) - joints[i].velocity *= 1 - friction * frictionness; - else - joints[i].velocity = 0; - if (findLengthfast(&bounceness) > 2500) { - Normalise(&bounceness); - bounceness = bounceness * 50; + terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1; + if (terrainnormal.y > .8) + freefall = 0; + bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal))); + if (findLengthfast(&joints[i].velocity) > findLengthfast(&joints[i].oldvelocity)) { + bounceness = 0; + joints[i].velocity = joints[i].oldvelocity; + } + if (!Tutorial::active || id == 0) + if (findLengthfast(&bounceness) > 4000 && breaking) { + Object::objects[k]->model.MakeDecal(breakdecal, DoRotation(temp - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), .4, .5, Random() % 360); + Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, 4, .2); + breaking = false; + camerashake += .6; + + emit_sound_at(breaksound2, joints[i].position * (*scale) + *coords); + + addEnvSound(*coords, 64); } - joints[i].velocity += bounceness * elasticity; - - - if (!joints[i].locked) - if (findLengthfast(&joints[i].velocity) < 1) { - joints[i].locked = 1; - } - if (findLengthfast(&bounceness) > 500) - Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2); - joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005; - if (longdead > 100) - broken = 1; + if (Object::objects[k]->type == treetrunktype) { + Object::objects[k]->rotx += joints[i].velocity.x * multiplier * .4; + Object::objects[k]->roty += joints[i].velocity.z * multiplier * .4; + Object::objects[k + 1]->rotx += joints[i].velocity.x * multiplier * .4; + Object::objects[k + 1]->roty += joints[i].velocity.z * multiplier * .4; } + if (!joints[i].locked) + damage += findLengthfast(&bounceness) / 2500; + ReflectVector(&joints[i].velocity, &terrainnormal); + frictionness = abs(normaldotproduct(joints[i].velocity, terrainnormal)); + joints[i].velocity -= bounceness; + if (1 - friction * frictionness > 0) + joints[i].velocity *= 1 - friction * frictionness; + else + joints[i].velocity = 0; + if (findLengthfast(&bounceness) > 2500) { + Normalise(&bounceness); + bounceness = bounceness * 50; + } + joints[i].velocity += bounceness * elasticity; + + + if (!joints[i].locked) + if (findLengthfast(&joints[i].velocity) < 1) { + joints[i].locked = 1; + } + if (findLengthfast(&bounceness) > 500) + Sprite::MakeSprite(cloudsprite, joints[i].position * (*scale) + *coords, joints[i].velocity * .06, 1, 1, 1, .5, .2); + joints[i].position = (temp - *coords) / (*scale) + terrainnormal * .005; + if (longdead > 100) + broken = 1; } } } + } joints[i].realoldposition = joints[i].position * (*scale) + *coords; } } multiplier = tempmult; - if (terrain.patchobjectnum[whichpatchx][whichpatchz]) - for (m = 0; m < terrain.patchobjectnum[whichpatchx][whichpatchz]; m++) { - unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][m]; - if (Object::objects[k]->possible) { - for (i = 0; i < 26; i++) { - //Make this less stupid - XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords; - XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords; - whichhit = Object::objects[k]->model.LineCheckSlidePossible(&start, &end, &Object::objects[k]->position, &Object::objects[k]->yaw); - if (whichhit != -1) { - joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale); - for (unsigned j = 0; j < muscles.size(); j++) { - if ((muscles[j].parent1->label == whichjointstartarray[i] && muscles[j].parent2->label == whichjointendarray[i]) || (muscles[j].parent2->label == whichjointstartarray[i] && muscles[j].parent1->label == whichjointendarray[i])) - muscles[j].DoConstraint(spinny); - } + for (unsigned int m = 0; m < terrain.patchobjects[whichpatchx][whichpatchz].size(); m++) { + unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][m]; + if (Object::objects[k]->possible) { + for (i = 0; i < 26; i++) { + //Make this less stupid + XYZ start = joints[jointlabels[whichjointstartarray[i]]].position * (*scale) + *coords; + XYZ end = joints[jointlabels[whichjointendarray[i]]].position * (*scale) + *coords; + whichhit = Object::objects[k]->model.LineCheckSlidePossible(&start, &end, &Object::objects[k]->position, &Object::objects[k]->yaw); + if (whichhit != -1) { + joints[jointlabels[whichjointendarray[i]]].position = (end - *coords) / (*scale); + for (unsigned j = 0; j < muscles.size(); j++) { + if ((muscles[j].parent1->label == whichjointstartarray[i] && muscles[j].parent2->label == whichjointendarray[i]) || (muscles[j].parent2->label == whichjointstartarray[i] && muscles[j].parent1->label == whichjointendarray[i])) + muscles[j].DoConstraint(spinny); } } } } + } for (i = 0; i < joints.size(); i++) { switch (joints[i].label) { diff --git a/Source/Environment/Terrain.cpp b/Source/Environment/Terrain.cpp index 6218c30..4e4971f 100644 --- a/Source/Environment/Terrain.cpp +++ b/Source/Environment/Terrain.cpp @@ -1141,7 +1141,7 @@ void Terrain::AddObject(XYZ where, float radius, int id) if (id >= 0 && id < 10000) { for (int i = 0; i < subdivision; i++) { for (int j = 0; j < subdivision; j++) { - if (patchobjectnum[i][j] < 300 - 1) { + if (patchobjects[i][j].size() < 300 - 1) { points[0].x = (size / subdivision) * i; points[0].z = (size / subdivision) * j; points[0].y = heightmap[(int)points[0].x][(int)points[0].z]; @@ -1151,8 +1151,7 @@ void Terrain::AddObject(XYZ where, float radius, int id) points[0] *= scale; points[1] *= scale; if (where.x + radius > points[0].x && where.x - radius < points[1].x && where.z + radius > points[0].z && where.z - radius < points[1].z) { - patchobjects[i][j][patchobjectnum[i][j]] = id; - patchobjectnum[i][j]++; + patchobjects[i][j].push_back(id); } } } @@ -1240,8 +1239,6 @@ void Terrain::MakeDecalLock(decal_type type, XYZ where, int whichx, int whichy, void Terrain::DoShadows() { - static int i, j, k, l, todivide; - static float brightness, total; static XYZ testpoint, testpoint2, terrainpoint, lightloc, col; lightloc = light.location; if (!skyboxtexture) { @@ -1256,8 +1253,8 @@ void Terrain::DoShadows() float shadowed; Normalise(&lightloc); //Calculate shadows - for (i = 0; i < size; i++) { - for (j = 0; j < size; j++) { + for (short int i = 0; i < size; i++) { + for (short int j = 0; j < size; j++) { terrainpoint.x = (float)(i) * scale; terrainpoint.z = (float)(j) * scale; terrainpoint.y = heightmap[i][j] * scale; @@ -1265,9 +1262,9 @@ void Terrain::DoShadows() shadowed = 0; patchx = (float)(i) * subdivision / size; patchz = (float)(j) * subdivision / size; - if (patchobjectnum[patchx][patchz]) { - for (k = 0; k < patchobjectnum[patchx][patchz]; k++) { - l = patchobjects[patchx][patchz][k]; + if (patchobjects[patchx][patchz].size()) { + for (unsigned int k = 0; k < patchobjects[patchx][patchz].size(); k++) { + unsigned int l = patchobjects[patchx][patchz][k]; if (Object::objects[l]->type != treetrunktype) { testpoint = terrainpoint; testpoint2 = terrainpoint + lightloc * 50 * (1 - shadowed); @@ -1278,7 +1275,7 @@ void Terrain::DoShadows() } Game::LoadingScreen(); } - brightness = dotproduct(&lightloc, &normals[i][j]); + float brightness = dotproduct(&lightloc, &normals[i][j]); if (shadowed) brightness *= 1 - shadowed; @@ -1303,11 +1300,11 @@ void Terrain::DoShadows() Game::LoadingScreen(); //Smooth shadows - for (i = 0; i < size; i++) { - for (j = 0; j < size; j++) { - for (k = 0; k < 3; k++) { - total = 0; - todivide = 0; + for (short int i = 0; i < size; i++) { + for (short int j = 0; j < size; j++) { + for (short int k = 0; k < 3; k++) { + float total = 0; + unsigned int todivide = 0; if (i != 0) { total += colors[j][i - 1][k]; todivide++; @@ -1348,8 +1345,8 @@ void Terrain::DoShadows() } } - for (i = 0; i < subdivision; i++) { - for (j = 0; j < subdivision; j++) { + for (unsigned int i = 0; i < subdivision; i++) { + for (unsigned int j = 0; j < subdivision; j++) { UpdateVertexArray(i, j); } } @@ -1359,9 +1356,6 @@ Terrain::Terrain() { size = 0; - memset(patchobjectnum, 0, sizeof(patchobjectnum)); - memset(patchobjects, 0, sizeof(patchobjects)); - scale = 1.0f; type = 0; memset(heightmap, 0, sizeof(heightmap)); diff --git a/Source/Environment/Terrain.hpp b/Source/Environment/Terrain.hpp index 884829c..207c8fd 100644 --- a/Source/Environment/Terrain.hpp +++ b/Source/Environment/Terrain.hpp @@ -59,8 +59,7 @@ public: Texture terraintexture; short size; - int patchobjectnum[subdivision][subdivision]; - unsigned int patchobjects[subdivision][subdivision][300]; + std::vector patchobjects[subdivision][subdivision]; float scale; int type; diff --git a/Source/GameDraw.cpp b/Source/GameDraw.cpp index 8bbeb34..c569a76 100644 --- a/Source/GameDraw.cpp +++ b/Source/GameDraw.cpp @@ -114,7 +114,6 @@ int Game::DrawGLScene(StereoSide side) { static float texcoordwidth, texcoordheight; static float texviewwidth, texviewheight; - static int l; static XYZ checkpoint; static float tempmult; float tutorialopac; @@ -299,10 +298,10 @@ int Game::DrawGLScene(StereoSide side) static XYZ point; static float size, opacity, rotation; rotation = 0; - for (unsigned k = 0; k < Person::players.size(); k++) { + for (unsigned int k = 0; k < Person::players.size(); k++) { if (!Person::players[k]->skeleton.free && Person::players[k]->playerdetail && Person::players[k]->howactive < typesleeping) if (frustum.SphereInFrustum(Person::players[k]->coords.x, Person::players[k]->coords.y + Person::players[k]->scale * 3, Person::players[k]->coords.z, Person::players[k]->scale * 7) && Person::players[k]->occluded < 25) - for (unsigned i = 0; i < Person::players[k]->skeleton.joints.size(); i++) { + for (unsigned int i = 0; i < Person::players[k]->skeleton.joints.size(); i++) { if (Person::players[k]->skeleton.joints[i].label == leftknee || Person::players[k]->skeleton.joints[i].label == rightknee || Person::players[k]->skeleton.joints[i].label == groin) { point = DoRotation(Person::players[k]->skeleton.joints[i].position, 0, Person::players[k]->yaw, 0) * Person::players[k]->scale + Person::players[k]->coords; size = .4f; @@ -311,8 +310,8 @@ int Game::DrawGLScene(StereoSide side) opacity = .2 + .2 * sin(smoketex * 6 + i) - Person::players[k]->skeleton.joints[i].position.y * Person::players[k]->scale / 5 - (Person::players[k]->coords.y - terrain.getHeight(Person::players[k]->coords.x, Person::players[k]->coords.z)) / 10; } terrain.MakeDecal(shadowdecal, point, size, opacity, rotation); - for (l = 0; l < terrain.patchobjectnum[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz]; l++) { - int j = terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz][l]; + for (unsigned int l = 0; l < terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz].size(); l++) { + unsigned int j = terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz][l]; if (Object::objects[j]->position.y < Person::players[k]->coords.y || Object::objects[j]->type == tunneltype || Object::objects[j]->type == weirdtype) { point = DoRotation(DoRotation(Person::players[k]->skeleton.joints[i].position, 0, Person::players[k]->yaw, 0) * Person::players[k]->scale + Person::players[k]->coords - Object::objects[j]->position, 0, -Object::objects[j]->yaw, 0); size = .4f; @@ -339,8 +338,8 @@ int Game::DrawGLScene(StereoSide side) opacity = .2 + .2 * sin(smoketex * 6 + i) - Person::players[k]->skeleton.joints[i].position.y * Person::players[k]->scale / 5 - (Person::players[k]->coords.y - terrain.getHeight(Person::players[k]->coords.x, Person::players[k]->coords.z)) / 10; } terrain.MakeDecal(shadowdecal, point, size, opacity * .7, rotation); - for (l = 0; l < terrain.patchobjectnum[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz]; l++) { - int j = terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz][l]; + for (unsigned int l = 0; l < terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz].size(); l++) { + unsigned int j = terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz][l]; if (Object::objects[j]->position.y < Person::players[k]->coords.y || Object::objects[j]->type == tunneltype || Object::objects[j]->type == weirdtype) { if (Person::players[k]->skeleton.free) point = DoRotation(Person::players[k]->skeleton.joints[i].position * Person::players[k]->scale + Person::players[k]->coords - Object::objects[j]->position, 0, -Object::objects[j]->yaw, 0); @@ -363,8 +362,8 @@ int Game::DrawGLScene(StereoSide side) size = .7; opacity = .4 - (Person::players[k]->coords.y - terrain.getHeight(Person::players[k]->coords.x, Person::players[k]->coords.z)) / 5; terrain.MakeDecal(shadowdecal, point, size, opacity * .7, rotation); - for (l = 0; l < terrain.patchobjectnum[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz]; l++) { - int j = terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz][l]; + for (unsigned int l = 0; l < terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz].size(); l++) { + unsigned int j = terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz][l]; point = DoRotation(Person::players[k]->coords - Object::objects[j]->position, 0, -Object::objects[j]->yaw, 0); size = .7; opacity = .4f; diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index 9b98e78..b94d2e8 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -618,7 +618,7 @@ bool Game::LoadLevel(const std::string& name, bool tutorial) for (int i = 0; i < subdivision; i++) { for (int j = 0; j < subdivision; j++) { - terrain.patchobjectnum[i][j] = 0; + terrain.patchobjects[i][j].clear(); } } Game::LoadingScreen(); @@ -1750,8 +1750,8 @@ void doAerialAcrobatics() //clip to terrain Person::players[k]->coords.y = max(Person::players[k]->coords.y, terrain.getHeight(Person::players[k]->coords.x, Person::players[k]->coords.z)); - for (int l = 0; l < terrain.patchobjectnum[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz]; l++) { - int i = terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz][l]; + for (unsigned int l = 0; l < terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz].size(); l++) { + unsigned int i = terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz][l]; if (Object::objects[i]->type != rocktype || Object::objects[i]->scale > .5 && Person::players[k]->aitype == playercontrolled || Object::objects[i]->position.y > Person::players[k]->coords.y) { @@ -1900,7 +1900,7 @@ void doAerialAcrobatics() } if (tempcollide) - for (int l = 0; l < terrain.patchobjectnum[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz]; l++) { + for (unsigned int l = 0; l < terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz].size(); l++) { int i = terrain.patchobjects[Person::players[k]->whichpatchx][Person::players[k]->whichpatchz][l]; lowpoint = Person::players[k]->coords; lowpoint.y += 1.35; @@ -4628,22 +4628,21 @@ void Game::TickOnceAfter() colviewer = viewer; coltarget = cameraloc; Object::SphereCheckPossible(&colviewer, findDistance(&colviewer, &coltarget)); - if (terrain.patchobjectnum[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz]) - for (int j = 0; j < terrain.patchobjectnum[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz]; j++) { - int i = terrain.patchobjects[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz][j]; - colviewer = viewer; - coltarget = cameraloc; - if (Object::objects[i]->model.LineCheckPossible(&colviewer, &coltarget, &col, &Object::objects[i]->position, &Object::objects[i]->yaw) != -1) - viewer = col; + for (unsigned int j = 0; j < terrain.patchobjects[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz].size(); j++) { + unsigned int i = terrain.patchobjects[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz][j]; + colviewer = viewer; + coltarget = cameraloc; + if (Object::objects[i]->model.LineCheckPossible(&colviewer, &coltarget, &col, &Object::objects[i]->position, &Object::objects[i]->yaw) != -1) { + viewer = col; } - if (terrain.patchobjectnum[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz]) - for (int j = 0; j < terrain.patchobjectnum[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz]; j++) { - int i = terrain.patchobjects[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz][j]; - colviewer = viewer; - if (Object::objects[i]->model.SphereCheck(&colviewer, .15, &col, &Object::objects[i]->position, &Object::objects[i]->yaw) != -1) { - viewer = colviewer; - } + } + for (unsigned int j = 0; j < terrain.patchobjects[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz].size(); j++) { + unsigned int i = terrain.patchobjects[Person::players[0]->whichpatchx][Person::players[0]->whichpatchz][j]; + colviewer = viewer; + if (Object::objects[i]->model.SphereCheck(&colviewer, .15, &col, &Object::objects[i]->position, &Object::objects[i]->yaw) != -1) { + viewer = colviewer; } + } cameradist = findDistance(&viewer, &target); viewer.y = max((double)viewer.y, terrain.getHeight(viewer.x, viewer.z) + .6); if (cameraloc.y < terrain.getHeight(cameraloc.x, cameraloc.z)) { diff --git a/Source/Graphic/Sprite.cpp b/Source/Graphic/Sprite.cpp index c775323..862d4bb 100644 --- a/Source/Graphic/Sprite.cpp +++ b/Source/Graphic/Sprite.cpp @@ -349,27 +349,28 @@ void Sprite::Draw() whichpatchx = sprites[i]->position.x / (terrain.size / subdivision * terrain.scale); whichpatchz = sprites[i]->position.z / (terrain.size / subdivision * terrain.scale); - if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision) - if (terrain.patchobjectnum[whichpatchx][whichpatchz]) { - if (!spritehit) - for (int j = 0; j < terrain.patchobjectnum[whichpatchx][whichpatchz]; j++) { - k = terrain.patchobjects[whichpatchx][whichpatchz][j]; - start = sprites[i]->oldposition; - end = sprites[i]->position; - if (!spritehit) - if (Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw) != -1) { - if (detail == 2 || (detail == 1 && abs(Random() % 4) == 0) || (detail == 0 && abs(Random() % 8) == 0)) - Object::objects[k]->model.MakeDecal(blooddecalfast, DoRotation(colpoint - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), sprites[i]->size * 1.6, .5, Random() % 360); - DeleteSprite(i); - spritehit = 1; - } - } + if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision) { + if (!spritehit) { + for (unsigned int j = 0; j < terrain.patchobjects[whichpatchx][whichpatchz].size(); j++) { + k = terrain.patchobjects[whichpatchx][whichpatchz][j]; + start = sprites[i]->oldposition; + end = sprites[i]->position; + if (!spritehit) + if (Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw) != -1) { + if (detail == 2 || (detail == 1 && abs(Random() % 4) == 0) || (detail == 0 && abs(Random() % 8) == 0)) + Object::objects[k]->model.MakeDecal(blooddecalfast, DoRotation(colpoint - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), sprites[i]->size * 1.6, .5, Random() % 360); + DeleteSprite(i); + spritehit = 1; + } + } } - if (!spritehit) + } + if (!spritehit) { if (sprites[i]->position.y < terrain.getHeight(sprites[i]->position.x, sprites[i]->position.z)) { terrain.MakeDecal(blooddecalfast, sprites[i]->position, sprites[i]->size * 1.6, .6, Random() % 360); DeleteSprite(i); } + } } } if (sprites[i]->type == splintersprite) { diff --git a/Source/Objects/Object.cpp b/Source/Objects/Object.cpp index 796e6ef..13f124c 100644 --- a/Source/Objects/Object.cpp +++ b/Source/Objects/Object.cpp @@ -206,15 +206,13 @@ void Object::doShadows(XYZ lightloc) patchx = terrainpoint.x / (terrain.size / subdivision * terrain.scale); patchz = terrainpoint.z / (terrain.size / subdivision * terrain.scale); if (patchx >= 0 && patchz >= 0 && patchx < subdivision && patchz < subdivision) { - if (terrain.patchobjectnum[patchx][patchz]) { - for (int k = 0; k < terrain.patchobjectnum[patchx][patchz]; k++) { - int l = terrain.patchobjects[patchx][patchz][k]; - if (objects[l]->type != treetrunktype) { - testpoint = terrainpoint; - testpoint2 = terrainpoint + lightloc * 50 * (1 - shadowed); - if (objects[l]->model.LineCheck(&testpoint, &testpoint2, &col, &objects[l]->position, &objects[l]->yaw) != -1) { - shadowed = 1 - (findDistance(&terrainpoint, &col) / 50); - } + for (unsigned int k = 0; k < terrain.patchobjects[patchx][patchz].size(); k++) { + unsigned int l = terrain.patchobjects[patchx][patchz][k]; + if (objects[l]->type != treetrunktype) { + testpoint = terrainpoint; + testpoint2 = terrainpoint + lightloc * 50 * (1 - shadowed); + if (objects[l]->model.LineCheck(&testpoint, &testpoint2, &col, &objects[l]->position, &objects[l]->yaw) != -1) { + shadowed = 1 - (findDistance(&terrainpoint, &col) / 50); } } } @@ -569,9 +567,9 @@ void Object::SphereCheckPossible(XYZ *p1, float radius) int whichpatchz = p1->z / (terrain.size / subdivision * terrain.scale); if (whichpatchx >= 0 && whichpatchz >= 0 && whichpatchx < subdivision && whichpatchz < subdivision) { - if (terrain.patchobjectnum[whichpatchx][whichpatchz] > 0 && terrain.patchobjectnum[whichpatchx][whichpatchz] < 500) { - for (int j = 0; j < terrain.patchobjectnum[whichpatchx][whichpatchz]; j++) { - int i = terrain.patchobjects[whichpatchx][whichpatchz][j]; + if (terrain.patchobjects[whichpatchx][whichpatchz].size() < 500) { + for (unsigned int j = 0; j < terrain.patchobjects[whichpatchx][whichpatchz].size(); j++) { + unsigned int i = terrain.patchobjects[whichpatchx][whichpatchz][j]; objects[i]->possible = false; if (objects[i]->model.SphereCheckPossible(p1, radius, &objects[i]->position, &objects[i]->yaw) != -1) { objects[i]->possible = true; diff --git a/Source/Objects/Person.cpp b/Source/Objects/Person.cpp index 95cbf52..ba26f80 100644 --- a/Source/Objects/Person.cpp +++ b/Source/Objects/Person.cpp @@ -1700,7 +1700,7 @@ void Person::DoHead() void Person::RagDoll(bool checkcollision) { static XYZ change; - static int l, i; + static int i; static float speed; if (!skeleton.free) { if (id == 0) @@ -1800,16 +1800,15 @@ void Person::RagDoll(bool checkcollision) whichpatchx = coords.x / (terrain.size / subdivision * terrain.scale); whichpatchz = coords.z / (terrain.size / subdivision * terrain.scale); - if (terrain.patchobjectnum[whichpatchx][whichpatchz]) - for (l = 0; l < terrain.patchobjectnum[whichpatchx][whichpatchz]; l++) { - i = terrain.patchobjects[whichpatchx][whichpatchz][l]; - lowpoint = coords; - lowpoint.y += 1; - if (SphereCheck(&lowpoint, 3, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw, &Object::objects[i]->model) != -1) { - coords.x = lowpoint.x; - coords.z = lowpoint.z; - } + for (unsigned int l = 0; l < terrain.patchobjects[whichpatchx][whichpatchz].size(); l++) { + i = terrain.patchobjects[whichpatchx][whichpatchz][l]; + lowpoint = coords; + lowpoint.y += 1; + if (SphereCheck(&lowpoint, 3, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw, &Object::objects[i]->model) != -1) { + coords.x = lowpoint.x; + coords.z = lowpoint.z; } + } } yaw = 0; @@ -4291,7 +4290,6 @@ void Person::DoStuff() static XYZ flatfacing; static XYZ flatvelocity; static float flatvelspeed; - static int l; static int bloodsize; static int startx, starty, endx, endy; static GLubyte color; @@ -4836,16 +4834,15 @@ void Person::DoStuff() headpoint = coords; if (bloodtoggle && !bled) { terrain.MakeDecal(blooddecalslow, headpoint, .8, .5, 0); - } - if (bloodtoggle && !bled) - for (l = 0; l < terrain.patchobjectnum[whichpatchx][whichpatchz]; l++) { - int j = terrain.patchobjects[whichpatchx][whichpatchz][l]; + for (unsigned int l = 0; l < terrain.patchobjects[whichpatchx][whichpatchz].size(); l++) { + unsigned int j = terrain.patchobjects[whichpatchx][whichpatchz][l]; XYZ point = DoRotation(headpoint - Object::objects[j]->position, 0, -Object::objects[j]->yaw, 0); float size = .8; float opacity = .6; float yaw = 0; Object::objects[j]->model.MakeDecal(blooddecalslow, &point, &size, &opacity, &yaw); } + } bled = 1; } @@ -5045,16 +5042,15 @@ void Person::DoStuff() DoBlood(1, 255); if (bloodtoggle && !bled) { terrain.MakeDecal(blooddecal, headpoint, .2 * 1.2, .5, 0); - } - if (bloodtoggle && !bled) - for (l = 0; l < terrain.patchobjectnum[whichpatchx][whichpatchz]; l++) { - int j = terrain.patchobjects[whichpatchx][whichpatchz][l]; + for (unsigned int l = 0; l < terrain.patchobjects[whichpatchx][whichpatchz].size(); l++) { + unsigned int j = terrain.patchobjects[whichpatchx][whichpatchz][l]; XYZ point = DoRotation(headpoint - Object::objects[j]->position, 0, -Object::objects[j]->yaw, 0); float size = .2 * 1.2; float opacity = .6; float yaw = 0; Object::objects[j]->model.MakeDecal(blooddecal, &point, &size, &opacity, &yaw); } + } bled = 1; } if (dead == 2 && bloodloss >= damagetolerance) { @@ -5064,16 +5060,15 @@ void Person::DoStuff() DoBlood(1, 255); if (bloodtoggle && !bled) { terrain.MakeDecal(blooddecalslow, headpoint, .8, .5, 0); - } - if (bloodtoggle && !bled) - for (l = 0; l < terrain.patchobjectnum[whichpatchx][whichpatchz]; l++) { - int j = terrain.patchobjects[whichpatchx][whichpatchz][l]; + for (unsigned int l = 0; l < terrain.patchobjects[whichpatchx][whichpatchz].size(); l++) { + unsigned int j = terrain.patchobjects[whichpatchx][whichpatchz][l]; XYZ point = DoRotation(headpoint - Object::objects[j]->position, 0, -Object::objects[j]->yaw, 0); float size = .8; float opacity = .6; float yaw = 0; Object::objects[j]->model.MakeDecal(blooddecalslow, &point, &size, &opacity, &yaw); } + } bled = 1; } } diff --git a/Source/Objects/Weapons.cpp b/Source/Objects/Weapons.cpp index 1fe6011..f1ba770 100644 --- a/Source/Objects/Weapons.cpp +++ b/Source/Objects/Weapons.cpp @@ -194,57 +194,55 @@ void Weapon::doStuff(int i) whichpatchx = position.x / (terrain.size / subdivision * terrain.scale); whichpatchz = position.z / (terrain.size / subdivision * terrain.scale); if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision) { - if (terrain.patchobjectnum[whichpatchx][whichpatchz]) { // if there are Object::objects where the weapon is - for (int j = 0; j < terrain.patchobjectnum[whichpatchx][whichpatchz]; j++) { // check for collision - int k = terrain.patchobjects[whichpatchx][whichpatchz][j]; - start = oldtippoint; - end = tippoint; - whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); - if (whichhit != -1) { - if (Object::objects[k]->type == treetrunktype) { - Object::objects[k]->model.MakeDecal(breakdecal, DoRotation(colpoint - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), .1, 1, Random() % 360); - normalrot = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0); - velocity = 0; - if (type == knife) - position = colpoint - normalrot * .1; - else if (type == sword) - position = colpoint - normalrot * .2; - else if (type == staff) - position = colpoint - normalrot * .2; - XYZ temppoint1, temppoint2; - float distance; - - temppoint1 = 0; - temppoint2 = normalrot; - distance = findDistance(&temppoint1, &temppoint2); - rotation2 = asin((temppoint1.y - temppoint2.y) / distance); - rotation2 *= 360 / 6.28; - temppoint1.y = 0; - temppoint2.y = 0; - rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2)); - rotation1 *= 360 / 6.28; - if (temppoint1.x > temppoint2.x) - rotation1 = 360 - rotation1; - - rotation3 = 0; - smallrotation = 90; - smallrotation2 = 0; - bigtilt = 0; - bigtilt2 = 0; - bigrotation = 0; - - emit_sound_at(knifesheathesound, position, 128.); - - bloody = 0; - - Sprite::MakeSprite(cloudimpactsprite, position, velocity, 1, 1, 1, .8, .3); - } else { - physics = 1; - firstfree = 1; - position -= velocity * multiplier; - tippoint -= velocity * multiplier; - tipvelocity = velocity; - } + for (unsigned int j = 0; j < terrain.patchobjects[whichpatchx][whichpatchz].size(); j++) { // check for collision + unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][j]; + start = oldtippoint; + end = tippoint; + whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); + if (whichhit != -1) { + if (Object::objects[k]->type == treetrunktype) { + Object::objects[k]->model.MakeDecal(breakdecal, DoRotation(colpoint - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), .1, 1, Random() % 360); + normalrot = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0); + velocity = 0; + if (type == knife) + position = colpoint - normalrot * .1; + else if (type == sword) + position = colpoint - normalrot * .2; + else if (type == staff) + position = colpoint - normalrot * .2; + XYZ temppoint1, temppoint2; + float distance; + + temppoint1 = 0; + temppoint2 = normalrot; + distance = findDistance(&temppoint1, &temppoint2); + rotation2 = asin((temppoint1.y - temppoint2.y) / distance); + rotation2 *= 360 / 6.28; + temppoint1.y = 0; + temppoint2.y = 0; + rotation1 = acos((temppoint1.z - temppoint2.z) / findDistance(&temppoint1, &temppoint2)); + rotation1 *= 360 / 6.28; + if (temppoint1.x > temppoint2.x) + rotation1 = 360 - rotation1; + + rotation3 = 0; + smallrotation = 90; + smallrotation2 = 0; + bigtilt = 0; + bigtilt2 = 0; + bigrotation = 0; + + emit_sound_at(knifesheathesound, position, 128.); + + bloody = 0; + + Sprite::MakeSprite(cloudimpactsprite, position, velocity, 1, 1, 1, .8, .3); + } else { + physics = 1; + firstfree = 1; + position -= velocity * multiplier; + tippoint -= velocity * multiplier; + tipvelocity = velocity; } } } @@ -443,220 +441,219 @@ void Weapon::doStuff(int i) //Object collisions whichpatchx = (position.x) / (terrain.size / subdivision * terrain.scale); whichpatchz = (position.z) / (terrain.size / subdivision * terrain.scale); - if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision) - if (terrain.patchobjectnum[whichpatchx][whichpatchz]) { - for (int j = 0; j < terrain.patchobjectnum[whichpatchx][whichpatchz]; j++) { - int k = terrain.patchobjects[whichpatchx][whichpatchz][j]; - - if (firstfree) { - if (type == staff) { - start = tippoint - (position - tippoint) / 5; - end = position + (position - tippoint) / 30; - whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); - if (whichhit != -1) { - XYZ diff; - diff = (colpoint - position); - Normalise(&diff); - hitsomething = 1; - - tippoint += (colpoint - position) + diff * .05; - position = colpoint + diff * .05; - oldtippoint = tippoint; - oldposition = tippoint; - } - } else { - start = position - (tippoint - position) / 5; - end = tippoint + (tippoint - position) / 30; - whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); - if (whichhit != -1) { - XYZ diff; - diff = (colpoint - tippoint); - Normalise(&diff); - hitsomething = 1; - - position += (colpoint - tippoint) + diff * .05; - tippoint = colpoint + diff * .05; - oldposition = position; - oldtippoint = tippoint; - } - } - } + if (whichpatchx > 0 && whichpatchz > 0 && whichpatchx < subdivision && whichpatchz < subdivision) { + for (unsigned int j = 0; j < terrain.patchobjects[whichpatchx][whichpatchz].size(); j++) { + unsigned int k = terrain.patchobjects[whichpatchx][whichpatchz][j]; + + if (firstfree) { + if (type == staff) { + start = tippoint - (position - tippoint) / 5; + end = position + (position - tippoint) / 30; + whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); + if (whichhit != -1) { + XYZ diff; + diff = (colpoint - position); + Normalise(&diff); + hitsomething = 1; - start = oldposition; - end = position; - whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); - if (whichhit != -1) { - hitsomething = 1; - position = colpoint; - terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1; - ReflectVector(&velocity, &terrainnormal); - position += terrainnormal * .002; - - bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal))); - if (findLengthfast(&velocity) < findLengthfast(&bounceness)) - bounceness = 0; - frictionness = abs(normaldotproduct(velocity, terrainnormal)); - velocity -= bounceness; - if (1 - friction * frictionness > 0) - velocity *= 1 - friction * frictionness; - else - velocity = 0; - velocity += bounceness * elasticity; + tippoint += (colpoint - position) + diff * .05; + position = colpoint + diff * .05; + oldtippoint = tippoint; + oldposition = tippoint; + } + } else { + start = position - (tippoint - position) / 5; + end = tippoint + (tippoint - position) / 30; + whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); + if (whichhit != -1) { + XYZ diff; + diff = (colpoint - tippoint); + Normalise(&diff); + hitsomething = 1; - if (findLengthfast(&bounceness) > 1) { - int whichsound; - if (type == staff) - whichsound = footstepsound3 + abs(Random() % 2); - else - whichsound = clank1sound + abs(Random() % 4); - emit_sound_at(whichsound, position, 128 * findLengthfast(&bounceness)); + position += (colpoint - tippoint) + diff * .05; + tippoint = colpoint + diff * .05; + oldposition = position; + oldtippoint = tippoint; } } - start = oldtippoint; - end = tippoint; - whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); - if (whichhit != -1) { - hitsomething = 1; - tippoint = colpoint; - terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1; - ReflectVector(&tipvelocity, &terrainnormal); - tippoint += terrainnormal * .002; - - bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal))); - if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness)) - bounceness = 0; - frictionness = abs(normaldotproduct(tipvelocity, terrainnormal)); - tipvelocity -= bounceness; - if (1 - friction * frictionness > 0) - tipvelocity *= 1 - friction * frictionness; + } + + start = oldposition; + end = position; + whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); + if (whichhit != -1) { + hitsomething = 1; + position = colpoint; + terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1; + ReflectVector(&velocity, &terrainnormal); + position += terrainnormal * .002; + + bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal))); + if (findLengthfast(&velocity) < findLengthfast(&bounceness)) + bounceness = 0; + frictionness = abs(normaldotproduct(velocity, terrainnormal)); + velocity -= bounceness; + if (1 - friction * frictionness > 0) + velocity *= 1 - friction * frictionness; + else + velocity = 0; + velocity += bounceness * elasticity; + + if (findLengthfast(&bounceness) > 1) { + int whichsound; + if (type == staff) + whichsound = footstepsound3 + abs(Random() % 2); else - tipvelocity = 0; - tipvelocity += bounceness * elasticity; + whichsound = clank1sound + abs(Random() % 4); + emit_sound_at(whichsound, position, 128 * findLengthfast(&bounceness)); + } + } + start = oldtippoint; + end = tippoint; + whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); + if (whichhit != -1) { + hitsomething = 1; + tippoint = colpoint; + terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1; + ReflectVector(&tipvelocity, &terrainnormal); + tippoint += terrainnormal * .002; + + bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal))); + if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness)) + bounceness = 0; + frictionness = abs(normaldotproduct(tipvelocity, terrainnormal)); + tipvelocity -= bounceness; + if (1 - friction * frictionness > 0) + tipvelocity *= 1 - friction * frictionness; + else + tipvelocity = 0; + tipvelocity += bounceness * elasticity; - if (findLengthfast(&bounceness) > 1) { - int whichsound; - if (type == staff) - whichsound = footstepsound3 + abs(Random() % 2); - else - whichsound = clank1sound + abs(Random() % 4); - emit_sound_at(whichsound, position, 128 * findLengthfast(&bounceness)); - } + if (findLengthfast(&bounceness) > 1) { + int whichsound; + if (type == staff) + whichsound = footstepsound3 + abs(Random() % 2); + else + whichsound = clank1sound + abs(Random() % 4); + emit_sound_at(whichsound, position, 128 * findLengthfast(&bounceness)); } + } - if ((Object::objects[k]->type != boxtype && Object::objects[k]->type != platformtype && Object::objects[k]->type != walltype && Object::objects[k]->type != weirdtype) || Object::objects[k]->pitch != 0) - for (int m = 0; m < 2; m++) { - mid = (position * (21 + (float)m * 10) + tippoint * (19 - (float)m * 10)) / 40; - oldmid2 = mid; - oldmid = (oldposition * (21 + (float)m * 10) + oldtippoint * (19 - (float)m * 10)) / 40; - - start = oldmid; - end = mid; - whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); - if (whichhit != -1) { - hitsomething = 1; - mid = colpoint; - terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1; - ReflectVector(&velocity, &terrainnormal); - - bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal))); - if (findLengthfast(&velocity) < findLengthfast(&bounceness)) - bounceness = 0; - frictionness = abs(normaldotproduct(velocity, terrainnormal)); - velocity -= bounceness; - if (1 - friction * frictionness > 0) - velocity *= 1 - friction * frictionness; - else - velocity = 0; - velocity += bounceness * elasticity; - - if (findLengthfast(&bounceness) > 1) { - int whichsound; - if (type == staff) - whichsound = footstepsound3 + abs(Random() % 2); - else - whichsound = clank1sound + abs(Random() % 4); - emit_sound_at(whichsound, mid, 128 * findLengthfast(&bounceness)); - } - position += (mid - oldmid2) * (20 / (1 + (float)m * 10)); - } + if ((Object::objects[k]->type != boxtype && Object::objects[k]->type != platformtype && Object::objects[k]->type != walltype && Object::objects[k]->type != weirdtype) || Object::objects[k]->pitch != 0) + for (int m = 0; m < 2; m++) { + mid = (position * (21 + (float)m * 10) + tippoint * (19 - (float)m * 10)) / 40; + oldmid2 = mid; + oldmid = (oldposition * (21 + (float)m * 10) + oldtippoint * (19 - (float)m * 10)) / 40; - mid = (position * (19 - (float)m * 10) + tippoint * (21 + (float)m * 10)) / 40; - oldmid2 = mid; - oldmid = (oldposition * (19 - (float)m * 10) + oldtippoint * (21 + (float)m * 10)) / 40; - - start = oldmid; - end = mid; - whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); - if (whichhit != -1) { - hitsomething = 1; - mid = colpoint; - terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1; - ReflectVector(&tipvelocity, &terrainnormal); - - bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal))); - if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness)) - bounceness = 0; - frictionness = abs(normaldotproduct(tipvelocity, terrainnormal)); - tipvelocity -= bounceness; - if (1 - friction * frictionness > 0) - tipvelocity *= 1 - friction * frictionness; + start = oldmid; + end = mid; + whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); + if (whichhit != -1) { + hitsomething = 1; + mid = colpoint; + terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1; + ReflectVector(&velocity, &terrainnormal); + + bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal))); + if (findLengthfast(&velocity) < findLengthfast(&bounceness)) + bounceness = 0; + frictionness = abs(normaldotproduct(velocity, terrainnormal)); + velocity -= bounceness; + if (1 - friction * frictionness > 0) + velocity *= 1 - friction * frictionness; + else + velocity = 0; + velocity += bounceness * elasticity; + + if (findLengthfast(&bounceness) > 1) { + int whichsound; + if (type == staff) + whichsound = footstepsound3 + abs(Random() % 2); else - tipvelocity = 0; - tipvelocity += bounceness * elasticity; - - if (findLengthfast(&bounceness) > 1) { - int whichsound; - if (type == staff) - whichsound = footstepsound3 + abs(Random() % 2); - else - whichsound = clank1sound + abs(Random() % 4); - emit_sound_at(whichsound, mid, 128 * findLengthfast(&bounceness)); - } - tippoint += (mid - oldmid2) * (20 / (1 + (float)m * 10)); + whichsound = clank1sound + abs(Random() % 4); + emit_sound_at(whichsound, mid, 128 * findLengthfast(&bounceness)); } + position += (mid - oldmid2) * (20 / (1 + (float)m * 10)); } - else { - start = position; - end = tippoint; + + mid = (position * (19 - (float)m * 10) + tippoint * (21 + (float)m * 10)) / 40; + oldmid2 = mid; + oldmid = (oldposition * (19 - (float)m * 10) + oldtippoint * (21 + (float)m * 10)) / 40; + + start = oldmid; + end = mid; whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); if (whichhit != -1) { hitsomething = 1; - closestdistance = -1; - closestswordpoint = colpoint; - point[0] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 0), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position; - point[1] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 1), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position; - point[2] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 2), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position; - if (DistancePointLine(&closestswordpoint, &point[0], &point[1], &distance, &colpoint )) { - if (distance < closestdistance || closestdistance == -1) { - closestpoint = colpoint; - closestdistance = distance; - } + mid = colpoint; + terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1; + ReflectVector(&tipvelocity, &terrainnormal); + + bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal))); + if (findLengthfast(&tipvelocity) < findLengthfast(&bounceness)) + bounceness = 0; + frictionness = abs(normaldotproduct(tipvelocity, terrainnormal)); + tipvelocity -= bounceness; + if (1 - friction * frictionness > 0) + tipvelocity *= 1 - friction * frictionness; + else + tipvelocity = 0; + tipvelocity += bounceness * elasticity; + + if (findLengthfast(&bounceness) > 1) { + int whichsound; + if (type == staff) + whichsound = footstepsound3 + abs(Random() % 2); + else + whichsound = clank1sound + abs(Random() % 4); + emit_sound_at(whichsound, mid, 128 * findLengthfast(&bounceness)); } - if (DistancePointLine(&closestswordpoint, &point[1], &point[2], &distance, &colpoint )) { - if (distance < closestdistance || closestdistance == -1) { - closestpoint = colpoint; - closestdistance = distance; - } + tippoint += (mid - oldmid2) * (20 / (1 + (float)m * 10)); + } + } + else { + start = position; + end = tippoint; + whichhit = Object::objects[k]->model.LineCheck(&start, &end, &colpoint, &Object::objects[k]->position, &Object::objects[k]->yaw); + if (whichhit != -1) { + hitsomething = 1; + closestdistance = -1; + closestswordpoint = colpoint; + point[0] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 0), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position; + point[1] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 1), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position; + point[2] = DoRotation(Object::objects[k]->model.getTriangleVertex(whichhit, 2), 0, Object::objects[k]->yaw, 0) + Object::objects[k]->position; + if (DistancePointLine(&closestswordpoint, &point[0], &point[1], &distance, &colpoint )) { + if (distance < closestdistance || closestdistance == -1) { + closestpoint = colpoint; + closestdistance = distance; + } + } + if (DistancePointLine(&closestswordpoint, &point[1], &point[2], &distance, &colpoint )) { + if (distance < closestdistance || closestdistance == -1) { + closestpoint = colpoint; + closestdistance = distance; } - if (DistancePointLine(&closestswordpoint, &point[2], &point[0], &distance, &colpoint )) { - if (distance < closestdistance || closestdistance == -1) { - closestpoint = colpoint; - closestdistance = distance; - } + } + if (DistancePointLine(&closestswordpoint, &point[2], &point[0], &distance, &colpoint )) { + if (distance < closestdistance || closestdistance == -1) { + closestpoint = colpoint; + closestdistance = distance; } - if (closestdistance != -1 && isnormal(closestdistance)) { - if (DistancePointLine(&closestpoint, &position, &tippoint, &distance, &colpoint )) { - closestswordpoint = colpoint; - velocity += (closestpoint - closestswordpoint); - tipvelocity += (closestpoint - closestswordpoint); - position += (closestpoint - closestswordpoint); - tippoint += (closestpoint - closestswordpoint); - } + } + if (closestdistance != -1 && isnormal(closestdistance)) { + if (DistancePointLine(&closestpoint, &position, &tippoint, &distance, &colpoint )) { + closestswordpoint = colpoint; + velocity += (closestpoint - closestswordpoint); + tipvelocity += (closestpoint - closestswordpoint); + position += (closestpoint - closestswordpoint); + tippoint += (closestpoint - closestswordpoint); } } } } } + } //Terrain collisions whichhit = terrain.lineTerrain(oldposition, position, &colpoint); if (whichhit != -1 || position.y < terrain.getHeight(position.x, position.z)) {