00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <movement/TargetMovementEntrySpline.h>
00022 #include <common/Defines.h>
00023 #include <common/RandomGenerator.h>
00024 #include <engine/ScorchedContext.h>
00025 #include <target/Target.h>
00026 #include <target/TargetLife.h>
00027 #include <target/TargetState.h>
00028 #include <landscapemap/LandscapeMaps.h>
00029 #include <landscapedef/LandscapeTex.h>
00030 #include <landscapedef/LandscapeMovement.h>
00031
00032 TargetMovementEntrySpline::TargetMovementEntrySpline() : context_(0)
00033 {
00034 }
00035
00036 TargetMovementEntrySpline::~TargetMovementEntrySpline()
00037 {
00038 }
00039
00040 void TargetMovementEntrySpline::generate(ScorchedContext &context,
00041 RandomGenerator &random, LandscapeMovementType *movementType)
00042 {
00043
00044
00045 LandscapeMovementTypeSpline *splineGroup =
00046 (LandscapeMovementTypeSpline *) movementType;
00047 context_ = &context;
00048 groundOnly_ = splineGroup->groundonly;
00049
00050
00051 std::vector<FixedVector> controlPoints;
00052 controlPoints.push_back(FixedVector::getNullVector());
00053 controlPoints.insert(controlPoints.end(), splineGroup->points.begin(), splineGroup->points.end());
00054
00055
00056 FixedVector midPt = (controlPoints[1] + controlPoints.back()) / 2;
00057 controlPoints.push_back(midPt);
00058 controlPoints.front() = midPt;
00059
00060
00061 if (groundOnly_)
00062 {
00063 std::vector<FixedVector>::iterator itor;
00064 for (itor = controlPoints.begin();
00065 itor != controlPoints.end();
00066 itor++)
00067 {
00068 FixedVector &point = (*itor);
00069 point[2] = context.getLandscapeMaps().getGroundMaps().getInterpHeight(
00070 point[0], point[1]);
00071 }
00072 }
00073
00074
00075 path_.generate(controlPoints, 200, 3, splineGroup->speed);
00076 path_.simulate(splineGroup->starttime);
00077
00078
00079 groupEntry_ = context.getLandscapeMaps().getGroundMaps().getGroups().
00080 getGroup(splineGroup->groupname.c_str());
00081 if (!groupEntry_)
00082 {
00083 S3D::dialogExit("TargetMovementEntrySpline",
00084 S3D::formatStringBuffer("Group entry %s has no objects defined for it",
00085 splineGroup->groupname.c_str()));
00086 }
00087
00088
00089 std::map<unsigned int, TargetGroup *> &objects = groupEntry_->getObjects();
00090 std::map<unsigned int, TargetGroup *>::iterator itor;
00091 for (itor = objects.begin();
00092 itor != objects.end();
00093 itor++)
00094 {
00095 unsigned int playerId = (*itor).first;
00096 TargetGroup *entry = (*itor).second;
00097
00098 if (!entry->getTarget()->isTarget() ||
00099 entry->getTarget()->getPlayerId() >= TargetID::MIN_TARGET_TRANSIENT_ID)
00100 {
00101 S3D::dialogExit("TargetMovementEntrySpline",
00102 "Movement can be assigned to level targets only (no tanks)");
00103 }
00104 if (entry->getTarget()->getTargetState().getMovement())
00105 {
00106 S3D::dialogExit("TargetMovementEntryBoids",
00107 "Only one movement can be assigned to each target");
00108 }
00109
00110
00111 entry->getTarget()->getTargetState().setMovement(new TargetStateMovement());
00112 }
00113 }
00114
00115 void TargetMovementEntrySpline::simulate(ScorchedContext &context, fixed frameTime)
00116 {
00117
00118 path_.simulate(frameTime);
00119
00120
00121 FixedVector position;
00122 FixedVector direction;
00123 path_.getPathAttrs(position, direction);
00124 FixedVector directionPerp = direction.get2DPerp();
00125
00126
00127 if (groundOnly_)
00128 {
00129 position[2] = context_->getLandscapeMaps().getGroundMaps().getInterpHeight(
00130 position[0], position[1]);
00131 }
00132
00133
00134 std::map<unsigned int, TargetGroup *> &objects = groupEntry_->getObjects();
00135 std::map<unsigned int, TargetGroup *>::iterator itor;
00136 for (itor = objects.begin();
00137 itor != objects.end();
00138 itor++)
00139 {
00140 unsigned int playerId = (*itor).first;
00141 TargetGroup *groupEntry = (*itor).second;
00142
00143 fixed angle = atan2x(direction[1], direction[0]);
00144 fixed angleDegs = (angle / fixed::XPI) * 180 - 90;
00145
00146
00147 groupEntry->getTarget()->getLife().setTargetPositionAndRotation(
00148 position, angleDegs);
00149 groupEntry->getTarget()->getLife().setVelocity(direction);
00150 }
00151 }
00152
00153 bool TargetMovementEntrySpline::writeMessage(NetBuffer &buffer)
00154 {
00155 fixed pathTime = path_.getPathTime();
00156 buffer.addToBuffer(pathTime);
00157 return true;
00158 }
00159
00160 bool TargetMovementEntrySpline::readMessage(NetBufferReader &reader)
00161 {
00162 fixed pathTime = 0;
00163 if (!reader.getFromBuffer(pathTime)) return false;
00164 path_.setPathTime(pathTime);
00165 return true;
00166 }
00167
00168 void TargetMovementEntrySpline::draw()
00169 {
00170 #ifndef S3D_SERVER
00171 path_.draw();
00172 #endif
00173 }