File: AirScript.cpp

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (183 lines) | stat: -rw-r--r-- 4,588 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "StdAfx.h"
#include "mmgr.h"

#include "AirScript.h"
#include "Sim/Units/CommandAI/CommandAI.h"
#include "Sim/Units/UnitLoader.h"
#include "Sim/Units/CommandAI/Command.h"
#include "Sim/Units/Unit.h"
#include "Sim/Units/UnitHandler.h"
#include "Sim/Units/UnitDefHandler.h"
#include "Game/Camera.h"
#include "Sim/MoveTypes/AirMoveType.h"
#include "ConfigHandler.h"
#include "LogOutput.h"
#include "Map/Ground.h"
#include "GlobalUnsynced.h"



const char* armhawk_name[] = { "arm_hawk", "ARMHAWK", NULL };
const char* armfig_name [] = { "arm_freedom_fighter", "ARMFIG", NULL };
const char* corvamp_name[] = { "core_vamp", "CORVAMP", NULL };
const char* corape_name [] = { "core_rapier", "CORAPE", NULL };

CAirScript::CAirScript()
: CScript(std::string("Air combat test")),
	curPlane(0),
	lastUpdateTime(0),
	tcp(500,100,500),
	tcf(0,0,1),
	oldCamDir(1,0,0),
	oldCamPos(500,500,500),
	doRoll(false),
	timeOut(15)
{
	for(int a=0;a<32;++a)
		oldUp[a]=UpVector;
}

CAirScript::~CAirScript()
{
}

static const char* FindUnit(const char** name)
{
	for (int i = 0; name[i]; ++i) {
		if (unitDefHandler->GetUnitDefByName(name[i]))
			return name[i];
	}
	return name[0];
}

void CAirScript::GameStart()
{
	doRoll=!configHandler->Get("ReflectiveWater",1);

	tcp=camera->pos;
	tcf=camera->forward;

	for(int a=0;a<10;++a){
		CUnit* u;
		if(gs->randFloat()<0.5f)
			u=unitLoader.LoadUnit(FindUnit(armhawk_name),float3(1650,300,2100+a*150),0,false,0,NULL);
		else
			u=unitLoader.LoadUnit(FindUnit(armfig_name),float3(1650,300,2100+a*150),0,false,0,NULL);
		u->pos.y=350;
		u->experience=0.3f;
		((CAirMoveType*)u->moveType)->SetState(AAirMoveType::AIRCRAFT_FLYING);
		planes.push_back(u->id);
		Command c2;
		c2.id=CMD_MOVE_STATE;
		c2.params.push_back(1);
		u->commandAI->GiveCommand(c2);

		Command c;
		c.id=CMD_PATROL;
		c.options=0;
		c.params.push_back(6570);
		c.params.push_back(0);
		c.params.push_back(2560);
		u->commandAI->GiveCommand(c);

		if(gs->randFloat()<0.5f){
			u=unitLoader.LoadUnit(FindUnit(corvamp_name),float3(3880,300,2100+a*150),1,false,0,NULL);
			((CAirMoveType*)u->moveType)->SetState(AAirMoveType::AIRCRAFT_FLYING);
		}else{
			u=unitLoader.LoadUnit(FindUnit(corape_name),float3(3880,300,2100+a*150),1,false,0,NULL);
		}
		u->pos.y=350;
		u->experience=0.3f;
		planes.push_back(u->id);
		u->commandAI->GiveCommand(c2);
		c.params[0]=500;
		u->commandAI->GiveCommand(c);
	}
}

void CAirScript::Update()
{
	std::deque<int>::iterator pi;
	int num=0;
	for(pi=planes.begin();pi!=planes.end();++pi){
		if(uh->units[*pi]==0){
			CUnit* u;
			if(!(num&1))
				u=unitLoader.LoadUnit(FindUnit(armhawk_name),float3(1000+(num&1)*5000,500,2100+num*120),(num&1),false,0,NULL);
			else
				u=unitLoader.LoadUnit(FindUnit(corvamp_name),float3(1000+(num&1)*5000,500,2100+num*120),(num&1),false,0,NULL);
			u->pos.y=ground->GetHeight(1000+(num&1)*5000,2100+num*120)+350;
			u->experience=0.3f;
			u->speed.x=2.8f;
			((CAirMoveType*)u->moveType)->SetState(AAirMoveType::AIRCRAFT_FLYING);
			*pi=u->id;

			Command c;
			c.id=CMD_PATROL;
			c.options=0;
			c.params.push_back(7000-(num&1)*6500);
			c.params.push_back(200);
			c.params.push_back(2500+num*60);

			Command c2;
			c2.id=CMD_MOVE_STATE;
			c2.params.push_back(1);
			u->commandAI->GiveCommand(c2);
			u->commandAI->GiveCommand(c);
		}
		num++;
	}
}

void CAirScript::SetCamera(void)
{
	if(curPlane==0 || uh->units[curPlane]==0){
		std::deque<int>::iterator pi;
		for(pi=planes.begin();pi!=planes.end();++pi){
			if(uh->units[*pi]!=0){
				curPlane=*pi;
				break;
			}
		}
		timeOut=std::min(1,timeOut+1);
	}
	if(timeOut>0){
		timeOut++;
		camera->forward=oldCamDir;
		camera->pos=oldCamPos;
		if(timeOut>15)
			timeOut=0;
		return;
	}

	if(uh->units[curPlane]==0)
		return;

	//CAircraft* u=(CAircraft*)uh->units[curPlane];
	CUnit *u = uh->units[curPlane];

	float deltaTime=gs->frameNum+gu->timeOffset-lastUpdateTime;
	lastUpdateTime=gs->frameNum+gu->timeOffset;

	float3 modPlanePos=u->pos-u->frontdir*25+u->speed*gu->timeOffset;
	tcp+=(modPlanePos-tcp)*(1-pow(0.95f,deltaTime));
	tcf+=(u->frontdir-tcf)*(1-pow(0.9f,deltaTime));
	tcf.ANormalize();

	camera->pos=tcp;
	//camera->forward=((u->pos+u->speed*gu->timeOffset-camera->pos).Normalize()+u->frontdir).ANormalize();
	camera->forward=((u->pos+u->speed*gu->timeOffset-camera->pos).ANormalize()+tcf.ANormalize());
	camera->forward.ANormalize();

	if(doRoll){
		oldUp[gs->frameNum%32]=u->updir;
		float3 up(0,0,0);
		for(int a=0;a<32;++a)
			up+=oldUp[a];
		camera->up=up;
	} else {
		camera->up=UpVector;
	}
	oldCamDir=camera->forward;
	oldCamPos=camera->pos;
}