cobinstance.cpp
来自「这是整套横扫千军3D版游戏的源码」· C++ 代码 · 共 1,724 行 · 第 1/4 页
CPP
1,724 行
return -1;
}
}
case WEAPON_RELOADTIME: {
if (p1 > 0 && p1 <= unit->weapons.size()) {
return unit->weapons[p1-1]->reloadTime;
}
else if (p1 < 0 && p1 >= 0 - unit->weapons.size()) {
int old = unit->weapons[-p1-1]->reloadTime;
unit->weapons[-p1-1]->reloadTime = p2;
return old;
}
else {
return -1;
}
}
case WEAPON_ACCURACY: {
if (p1 > 0 && p1 <= unit->weapons.size()) {
return int(unit->weapons[p1-1]->accuracy * COBSCALE);
}
else if (p1 < 0 && p1 >= 0 - unit->weapons.size()) {
int old = int(unit->weapons[-p1-1]->accuracy * COBSCALE);
unit->weapons[-p1-1]->accuracy = float(p2) / COBSCALE;
return old;
}
else {
return -1;
}
}
case WEAPON_SPRAY: {
if (p1 > 0 && p1 <= unit->weapons.size()) {
return int(unit->weapons[p1-1]->sprayangle * COBSCALE);
}
else if (p1 < 0 && p1 >= 0 - unit->weapons.size()) {
int old = int(unit->weapons[-p1-1]->sprayangle * COBSCALE);
unit->weapons[-p1-1]->sprayangle = float(p2) / COBSCALE;
return old;
}
else {
return -1;
}
}
case WEAPON_RANGE: {
if (p1 > 0 && p1 <= unit->weapons.size()) {
return int(unit->weapons[p1-1]->range * COBSCALE);
}
else if (p1 < 0 && p1 >= 0 - unit->weapons.size()) {
int old = int(unit->weapons[-p1-1]->range * COBSCALE);
unit->weapons[-p1-1]->range = float(p2) / COBSCALE;
return old;
}
else {
return -1;
}
}
case WEAPON_PROJECTILE_SPEED: {
if (p1 > 0 && p1 <= unit->weapons.size()) {
return int(unit->weapons[p1-1]->projectileSpeed * COBSCALE);
}
else if (p1 < 0 && p1 >= 0 - unit->weapons.size()) {
int old = int(unit->weapons[-p1-1]->projectileSpeed * COBSCALE);
unit->weapons[-p1-1]->projectileSpeed = float(p2) / COBSCALE;
return old;
}
else {
return -1;
}
}
default:
if ((val >= GLOBAL_VAR_START) && (val <= GLOBAL_VAR_END)) {
return globalVars[val - GLOBAL_VAR_START];
}
else if ((val >= TEAM_VAR_START) && (val <= TEAM_VAR_END)) {
return teamVars[unit->team][val - TEAM_VAR_START];
}
else if ((val >= ALLY_VAR_START) && (val <= ALLY_VAR_END)) {
return allyVars[unit->allyteam][val - ALLY_VAR_START];
}
else if ((val >= UNIT_VAR_START) && (val <= UNIT_VAR_END)) {
const int varID = val - UNIT_VAR_START;
if (p1 == 0) {
return unitVars[varID];
}
else if (p1 > 0) {
// get the unit var for another unit
const CUnit *u = (p1 < MAX_UNITS) ? uh->units[p1] : NULL;
return (u == NULL) ? 0 : u->cob->unitVars[varID];
}
else {
// set the unit var for another unit
p1 = -p1;
const CUnit *u = (p1 < MAX_UNITS) ? uh->units[p1] : NULL;
if (u != NULL) {
u->cob->unitVars[varID] = p2;
return 1;
}
return 0;
}
}
else {
logOutput.Print("CobError: Unknown get constant %d (params = %d %d %d %d)",
val, p1, p2, p3, p4);
}
}
#endif
return 0;
}
void CCobInstance::SetUnitVal(int val, int param)
{
#ifndef _CONSOLE
switch(val) {
case ACTIVATION: {
Command c;
c.id = CMD_ONOFF;
c.params.push_back(param == 0 ? 0 : 1);
unit->commandAI->GiveCommand(c);
break;
}
case STANDINGMOVEORDERS: {
if (param >= 0 && param <= 2) {
Command c;
c.id = CMD_MOVE_STATE;
c.params.push_back(param);
unit->commandAI->GiveCommand(c);
}
break;
}
case STANDINGFIREORDERS: {
if (param >= 0 && param <= 2) {
Command c;
c.id = CMD_FIRE_STATE;
c.params.push_back(param);
unit->commandAI->GiveCommand(c);
}
break;
}
case HEALTH: {
break;
}
case INBUILDSTANCE: {
//logOutput.Print("buildstance %d", param);
unit->inBuildStance = (param != 0);
break;
}
case BUSY: {
busy = (param != 0);
break;
}
case PIECE_XZ: {
break;
}
case PIECE_Y: {
break;
}
case UNIT_XZ: {
break;
}
case UNIT_Y: {
break;
}
case UNIT_HEIGHT: {
break;
}
case XZ_ATAN: {
break;
}
case XZ_HYPOT: {
break;
}
case ATAN: {
break;
}
case HYPOT: {
break;
}
case GROUND_HEIGHT: {
break;
}
case GROUND_WATER_HEIGHT: {
break;
}
case BUILD_PERCENT_LEFT: {
break;
}
case YARD_OPEN: {
if (param == 0) {
if (uh->CanCloseYard(unit)) {
yardOpen = false;
}
}
else {
yardOpen = true;
}
break;
}
case BUGGER_OFF: {
if (param != 0) {
helper->BuggerOff(unit->pos+unit->frontdir*unit->radius,unit->radius*1.5f);
}
//yardOpen = (param != 0);
break;
}
case ARMORED: {
if(param){
unit->curArmorMultiple=unit->armoredMultiple;
} else {
unit->curArmorMultiple=1;
}
unit->armoredState = (param != 0);
break;
}
case VETERAN_LEVEL: {
unit->experience=param*0.01f;
break;
}
case MAX_SPEED: {
if(unit->moveType && param > 0){
// find the first CMD_SET_WANTED_MAX_SPEED and modify it if need be
for (CCommandQueue::iterator it = unit->commandAI->commandQue.begin();
it != unit->commandAI->commandQue.end(); ++it) {
Command &c = *it;
if (c.id == CMD_SET_WANTED_MAX_SPEED && c.params[0] == unit->maxSpeed) {
c.params[0] = param/(float)COBSCALE;
break;
}
}
unit->moveType->SetMaxSpeed(param/(float)COBSCALE);
unit->maxSpeed = param/(float)COBSCALE;
}
break;
}
case CLOAKED: {
unit->wantCloak = !!param;
break;
}
case WANT_CLOAK: {
unit->wantCloak = !!param;
break;
}
case UPRIGHT: {
unit->upright = !!param;
break;
}
case HEADING: {
unit->heading = param % COBSCALE;
unit->SetDirectionFromHeading();
break;
}
case LOS_RADIUS: {
unit->ChangeLos(param, unit->realAirLosRadius);
unit->realLosRadius = param;
break;
}
case AIR_LOS_RADIUS: {
unit->ChangeLos(unit->realLosRadius, param);
unit->realAirLosRadius = param;
break;
}
case RADAR_RADIUS: {
radarhandler->RemoveUnit(unit);
unit->radarRadius = param;
radarhandler->MoveUnit(unit);
break;
}
case JAMMER_RADIUS: {
radarhandler->RemoveUnit(unit);
unit->jammerRadius = param;
radarhandler->MoveUnit(unit);
break;
}
case SONAR_RADIUS: {
radarhandler->RemoveUnit(unit);
unit->sonarRadius = param;
radarhandler->MoveUnit(unit);
break;
}
case SONAR_JAM_RADIUS: {
radarhandler->RemoveUnit(unit);
unit->sonarJamRadius = param;
radarhandler->MoveUnit(unit);
break;
}
case SEISMIC_RADIUS: {
radarhandler->RemoveUnit(unit);
unit->seismicRadius = param;
radarhandler->MoveUnit(unit);
break;
}
case CURRENT_FUEL: {
unit->currentFuel = param / (float) COBSCALE;
break;
}
case SHIELD_POWER: {
if (unit->shieldWeapon != NULL) {
CPlasmaRepulser* shield = (CPlasmaRepulser*)unit->shieldWeapon;
shield->curPower = max(0.0f, float(param) / float(COBSCALE));
}
break;
}
case STEALTH: {
unit->stealth = !!param;
break;
}
case CRASHING: {
if(dynamic_cast<CAirMoveType*>(unit->moveType)){
if(!!param){
((CAirMoveType*)unit->moveType)->SetState(CAirMoveType::AIRCRAFT_CRASHING);
} else {
unit->crashing=false;
((CAirMoveType*)unit->moveType)->aircraftState=CAirMoveType::AIRCRAFT_TAKEOFF;
((CAirMoveType*)unit->moveType)->SetState(CAirMoveType::AIRCRAFT_FLYING);
}
}
break;
}
case CHANGE_TARGET: {
unit->weapons[param - 1]->avoidTarget = true;
break;
}
case ALPHA_THRESHOLD: {
unit->alphaThreshold = float(param) / 255.0f;
break;
}
case CEG_DAMAGE: {
unit->cegDamage = param;
break;
}
case FLANK_B_MODE:
unit->flankingBonusMode = param;
break;
case FLANK_B_MOBILITY_ADD:
unit->flankingBonusMobilityAdd = (param / (float)COBSCALE);
break;
case FLANK_B_MAX_DAMAGE: {
float mindamage = unit->flankingBonusAvgDamage - unit->flankingBonusDifDamage;
unit->flankingBonusAvgDamage = (param / (float)COBSCALE + mindamage)*0.5f;
unit->flankingBonusDifDamage = (param / (float)COBSCALE - mindamage)*0.5f;
break;
}
case FLANK_B_MIN_DAMAGE: {
float maxdamage = unit->flankingBonusAvgDamage + unit->flankingBonusDifDamage;
unit->flankingBonusAvgDamage = (maxdamage + param / (float)COBSCALE)*0.5f;
unit->flankingBonusDifDamage = (maxdamage - param / (float)COBSCALE)*0.5f;
break;
}
default: {
if ((val >= GLOBAL_VAR_START) && (val <= GLOBAL_VAR_END)) {
globalVars[val - GLOBAL_VAR_START] = param;
}
else if ((val >= TEAM_VAR_START) && (val <= TEAM_VAR_END)) {
teamVars[unit->team][val - TEAM_VAR_START] = param;
}
else if ((val >= ALLY_VAR_START) && (val <= ALLY_VAR_END)) {
allyVars[unit->allyteam][val - ALLY_VAR_START] = param;
}
else if ((val >= UNIT_VAR_START) && (val <= UNIT_VAR_END)) {
unitVars[val - UNIT_VAR_START] = param;
}
else {
logOutput.Print("CobError: Unknown set constant %d", val);
}
}
}
#endif
}
bool CCobInstance::HasScriptFunction(int id)
{
return (script.scriptIndex[id] >= 0);
}
void CCobInstance::MoveSmooth(int piece, int axis, int destination, int delta, int deltaTime)
{
//Make sure we do not overwrite animations of non-interpolated origin
AnimInfo *ai = FindAnim(AMove, piece, axis);
if (ai) {
if (!ai->interpolated) {
//logOutput.Print("Anim move overwrite");
MoveNow(piece, axis, destination);
return;
}
}
int cur = pieces[piece].coords[axis];
int dist = abs(destination - cur);
int timeFactor = (1000 * 1000) / (deltaTime * deltaTime);
int speed = (dist * timeFactor) / delta;
//logOutput.Print("Move %d got %d %d", cur, destination, speed);
Move(piece, axis, speed, destination, true);
}
void CCobInstance::TurnSmooth(int piece, int axis, int destination, int delta, int deltaTime)
{
AnimInfo *ai = FindAnim(ATurn, piece, axis);
if (ai) {
if (!ai->interpolated) {
//logOutput.Print("Anim turn overwrite");
TurnNow(piece, axis, destination);
return;
}
}
int cur = pieces[piece].rot[axis];
short int dist = destination - cur;
int timeFactor = (1000 * 1000) / (deltaTime * deltaTime);
dist = abs(dist);
int speed = (dist * timeFactor) / delta;
//logOutput.Print("Turnx %d:%d cur %d got %d %d dist %d", piece, axis, cur, destination, speed, dist);
Turn(piece, axis, speed, destination, true);
}
bool CCobInstance::FunctionExist(int id)
{
if(script.scriptIndex[id]==-1)
return false;
return true;
}
int CCobInstance::GetFunctionId(const string& funcName) const
{
return script.getFunctionId(funcName);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?