1 #include "supplyDrop.h"
2 #include "spaceship.h"
3 #include "playerInfo.h"
4 #include "playerSpaceship.h"
5 #include "main.h"
6 
7 #include "scriptInterface.h"
8 
9 /// A supply drop.
REGISTER_SCRIPT_SUBCLASS(SupplyDrop,SpaceObject)10 REGISTER_SCRIPT_SUBCLASS(SupplyDrop, SpaceObject)
11 {
12     REGISTER_SCRIPT_CLASS_FUNCTION(SupplyDrop, setEnergy);
13     REGISTER_SCRIPT_CLASS_FUNCTION(SupplyDrop, setWeaponStorage);
14     /// Set a function that will be called if a player picks up the supply drop.
15     /// First argument given to the function will be the supply drop, the second the player.
16     REGISTER_SCRIPT_CLASS_FUNCTION(SupplyDrop, onPickUp);
17 }
18 
19 REGISTER_MULTIPLAYER_CLASS(SupplyDrop, "SupplyDrop");
20 
SupplyDrop()21 SupplyDrop::SupplyDrop()
22 : SpaceObject(100, "SupplyDrop")
23 {
24     for(int n=0; n<MW_Count; n++)
25         weapon_storage[n] = 0;
26 
27     energy = 0.0;
28     setRadarSignatureInfo(0.0, 0.1, 0.1);
29 
30     model_info.setData("ammo_box");
31 }
32 
drawOnRadar(sf::RenderTarget & window,sf::Vector2f position,float scale,float rotation,bool long_range)33 void SupplyDrop::drawOnRadar(sf::RenderTarget& window, sf::Vector2f position, float scale, float rotation, bool long_range)
34 {
35     sf::Sprite object_sprite;
36     textureManager.setTexture(object_sprite, "RadarBlip.png");
37     object_sprite.setRotation(getRotation());
38     object_sprite.setPosition(position);
39     if (my_spaceship && !my_spaceship->isFriendly(this))
40         object_sprite.setColor(sf::Color(200, 50, 50));
41     else
42         object_sprite.setColor(sf::Color(100, 200, 255));
43     float size = 0.5;
44     object_sprite.setScale(size, size);
45     window.draw(object_sprite);
46 }
47 
collide(Collisionable * target,float force)48 void SupplyDrop::collide(Collisionable* target, float force)
49 {
50     P<SpaceShip> ship = P<Collisionable>(target);
51     if (ship && isFriendly(ship))
52     {
53         bool picked_up = false;
54         P<PlayerSpaceship> player = ship;
55         if (player)
56         {
57             player->energy_level += energy;
58             picked_up = true;
59         }
60         for(int n=0; n<MW_Count; n++)
61         {
62             uint8_t delta = std::min(int(weapon_storage[n]), ship->weapon_storage_max[n] - ship->weapon_storage[n]);
63             if (delta > 0)
64             {
65                 ship->weapon_storage[n] += delta;
66                 weapon_storage[n] -= delta;
67                 picked_up = true;
68             }
69         }
70         if (on_pickup_callback.isSet())
71         {
72             on_pickup_callback.call<void>(P<SupplyDrop>(this), player);
73             picked_up = true;
74         }
75 
76         if (picked_up)
77             destroy();
78     }
79 }
80 
onPickUp(ScriptSimpleCallback callback)81 void SupplyDrop::onPickUp(ScriptSimpleCallback callback)
82 {
83     this->on_pickup_callback = callback;
84 }
85 
setEnergy(float amount)86 void SupplyDrop::setEnergy(float amount)
87 {
88     energy = amount;
89     setRadarSignatureInfo(getRadarSignatureGravity(), getRadarSignatureElectrical() + (amount / 1000.0f), getRadarSignatureBiological());
90 }
91 
setWeaponStorage(EMissileWeapons weapon,int amount)92 void SupplyDrop::setWeaponStorage(EMissileWeapons weapon, int amount)
93 {
94     if (weapon != MW_None)
95     {
96         weapon_storage[weapon] = amount;
97         setRadarSignatureInfo(getRadarSignatureGravity() + (0.05f * amount), getRadarSignatureElectrical(), getRadarSignatureBiological());
98     }
99 }
100 
getExportLine()101 string SupplyDrop::getExportLine()
102 {
103     string ret = "SupplyDrop():setFaction(\"" + getFaction() + "\"):setPosition(" + string(getPosition().x, 0) + ", " + string(getPosition().y, 0) + ")";
104     if (energy > 0)
105         ret += ":setEnergy(" + string(energy, 0) + ")";
106     for(int n=0; n<MW_Count; n++)
107         if (weapon_storage[n] > 0)
108             ret += ":setWeaponStorage(\"" + getMissileWeaponName(EMissileWeapons(n)) + "\", " + string(weapon_storage[n]) + ")";
109     return ret;
110 }
111