Skip to content

Commit 8bb77bf

Browse files
committed
1 parent 854001a commit 8bb77bf

File tree

2 files changed

+214
-127
lines changed

2 files changed

+214
-127
lines changed

OpenSR/scripts/definitions/pickups.as

Lines changed: 0 additions & 127 deletions
This file was deleted.
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
import pickups;
2+
import camps;
3+
import saving;
4+
import regions.regions;
5+
import attributes;
6+
7+
tidy class PickupScript {
8+
void postInit(Pickup& obj) {
9+
obj.initPickup();
10+
}
11+
12+
void syncInitial(const Pickup& obj, Message& msg) {
13+
msg << obj.PickupType;
14+
obj.writePickup(msg);
15+
}
16+
17+
bool syncDelta(const Pickup& obj, Message& msg) {
18+
if(obj.claimPickupDelta()) {
19+
obj.writePickup(msg);
20+
return true;
21+
}
22+
return false;
23+
}
24+
25+
void destroy(Pickup& obj) {
26+
leaveRegion(obj);
27+
}
28+
29+
void load(Pickup& obj, SaveFile& msg) {
30+
cast<PickupControl>(obj.PickupControl).load(obj, msg);
31+
}
32+
33+
void postLoad(Pickup& obj) {
34+
cast<PickupControl>(obj.PickupControl).postLoad(obj);
35+
}
36+
37+
void save(Pickup& obj, SaveFile& msg) {
38+
cast<PickupControl>(obj.PickupControl).save(obj, msg);
39+
}
40+
41+
double tick(Pickup& obj, double time) {
42+
updateRegion(obj);
43+
obj.tickPickup(time);
44+
return 0.5;
45+
}
46+
};
47+
48+
tidy class PickupControl : Component_PickupControl {
49+
const PickupType@ type;
50+
Object@[] protectors;
51+
vec3d offset;
52+
bool pickupDelta = false;
53+
54+
const CampType@ campType;
55+
56+
PickupControl() {
57+
}
58+
59+
void generateMesh(Object& obj) {
60+
MeshDesc mesh;
61+
@mesh.model = type.model;
62+
@mesh.material = type.material;
63+
@mesh.iconSheet = type.iconSheet;
64+
mesh.iconIndex = type.iconIndex;
65+
mesh.memorable = true;
66+
bindMesh(obj, mesh);
67+
68+
Node@ node = obj.getNode();
69+
node.customColor = true;
70+
node.color = Color(0x998888ff);
71+
}
72+
73+
void setCampType(uint id) {
74+
@campType = getCreepCamp(id);
75+
}
76+
77+
void load(Pickup& obj, SaveFile& msg) {
78+
uint tid = msg.readIdentifier(SI_Pickup);
79+
@type = getPickupType(tid);
80+
81+
uint cnt = 0;
82+
msg >> cnt;
83+
protectors.length = cnt;
84+
for(uint i = 0; i < cnt; ++i)
85+
msg >> protectors[i];
86+
87+
obj.PickupType = type.id;
88+
generateMesh(obj);
89+
90+
if(msg >= SV_0095) {
91+
if(msg.readBit())
92+
@campType = getCreepCamp(msg.readIdentifier(SI_CreepCamp));
93+
}
94+
}
95+
96+
void postLoad(Pickup& obj) {
97+
if(protectors.length != 0)
98+
offset = protectors[0].position - obj.position;
99+
}
100+
101+
void save(Pickup& obj, SaveFile& msg) {
102+
msg.writeIdentifier(SI_Pickup, type.id);
103+
uint cnt = protectors.length;
104+
msg << cnt;
105+
for(uint i = 0; i < cnt; ++i)
106+
msg << protectors[i];
107+
108+
msg.writeBit(campType !is null);
109+
if(campType !is null)
110+
msg.writeIdentifier(SI_CreepCamp, campType.id);
111+
}
112+
113+
void initPickup(Object& obj) {
114+
Pickup@ pickup = cast<Pickup>(obj);
115+
@type = getPickupType(pickup.PickupType);
116+
117+
pickup.name = type.getName(pickup);
118+
pickup.radius = type.physicalSize;
119+
generateMesh(obj);
120+
}
121+
122+
bool claimPickupDelta() {
123+
if(pickupDelta) {
124+
pickupDelta = false;
125+
return true;
126+
}
127+
return false;
128+
}
129+
130+
bool get_isPickupProtected() {
131+
return protectors.length != 0;
132+
}
133+
134+
void addPickupProtector(Object& obj, Object& protector) {
135+
protectors.insertLast(protector);
136+
if(protectors.length == 1)
137+
offset = protectors[0].position - obj.position;
138+
}
139+
140+
Object@ getProtector() {
141+
if(protectors.length == 0)
142+
return null;
143+
return protectors[0];
144+
}
145+
146+
void tickPickup(Object& pickup, double time) {
147+
bool wasProtected = protectors.length != 0;
148+
if(protectors.length != 0) {
149+
auto@ prot = protectors[0];
150+
pickup.position = prot.position + offset;
151+
pickup.velocity = prot.velocity;
152+
pickup.acceleration = prot.acceleration;
153+
}
154+
else {
155+
pickup.velocity = vec3d();
156+
pickup.acceleration = vec3d();
157+
}
158+
159+
Object@ lastHit;
160+
for(uint i = 0, cnt = protectors.length; i < cnt; ++i) {
161+
Object@ prot = protectors[i];
162+
if(!prot.valid || prot.owner.major) {
163+
protectors.removeAt(i);
164+
pickupDelta = true;
165+
--i; --cnt;
166+
if(cnt == 0) {
167+
if(prot.isShip)
168+
@lastHit = getObjectByID(cast<Ship>(prot).lastHit);
169+
}
170+
}
171+
}
172+
173+
if(wasProtected && protectors.length == 0) {
174+
if(gameTime <= 5.0) {
175+
pickup.destroy();
176+
return;
177+
}
178+
179+
if(lastHit !is null) {
180+
Empire@ hitEmp = lastHit.owner;
181+
if(hitEmp !is null)
182+
hitEmp.modAttribute(EA_RemnantsCleared, AC_Add, 1.0);
183+
184+
if(lastHit.hasSupportAI)
185+
@lastHit = cast<Ship>(lastHit).Leader;
186+
if(lastHit !is null)
187+
type.onClear(cast<Pickup>(pickup), lastHit);
188+
}
189+
190+
Region@ reg = pickup.region;
191+
if(campType !is null && reg !is null) {
192+
for(uint i = 0, cnt = campType.region_statuses.length; i < cnt; ++i)
193+
reg.removeRegionStatus(null, campType.region_statuses[i].id);
194+
}
195+
}
196+
}
197+
198+
void pickupPickup(Object& obj, Object& by) {
199+
Pickup@ pickup = cast<Pickup>(obj);
200+
if(!type.canPickup(pickup, by))
201+
return;
202+
if(isPickupProtected)
203+
return;
204+
type.onPickup(pickup, by);
205+
obj.destroy();
206+
}
207+
208+
void writePickup(Message& msg) {
209+
uint cnt = protectors.length;
210+
msg << cnt;
211+
for(uint i = 0; i < cnt; ++i)
212+
msg << protectors[i];
213+
}
214+
};

0 commit comments

Comments
 (0)