From 058f40a2e6dfd7e3498e7506c3ca82606f9b792e Mon Sep 17 00:00:00 2001
From: Stijn Buys <ingar@osirion.org>
Date: Tue, 5 Nov 2013 16:42:01 +0000
Subject: Have NPCs get cargo.

---
 src/game/base/game.cc   |  9 +++++++--
 src/game/base/patrol.cc | 41 +++++++++++++++++++++++++++++++++++++++--
 src/game/base/patrol.h  |  9 +++++----
 src/game/base/ship.cc   |  8 ++++----
 4 files changed, 55 insertions(+), 12 deletions(-)

(limited to 'src/game/base')

diff --git a/src/game/base/game.cc b/src/game/base/game.cc
index 6921f72..f227c2f 100644
--- a/src/game/base/game.cc
+++ b/src/game/base/game.cc
@@ -2168,8 +2168,13 @@ bool Game::load_zone(core::Zone *zone)
 				} else if (zoneini.got_key_string("target", strval)) {
 					patrol_waypoint->set_target_label(strval);
 					continue;
-				} else if (zoneini.got_key_string("buy", strval)) {
-					patrol_waypoint->set_buy_label(strval);
+				} else if (zoneini.got_key_label("cargo", strval)) {
+					Cargo *cargo = Cargo::find(strval);
+					if (!cargo) {
+						zoneini.unknown_error("unknown cargo '" + strval + "'");
+					} else {
+						patrol_waypoint->set_cargo(cargo);
+					}
 					continue;
 				} else if (zoneini.got_key_bool("dock", b)) {
 					patrol_waypoint->set_dock(b);
diff --git a/src/game/base/patrol.cc b/src/game/base/patrol.cc
index 4ad16df..a50abea 100644
--- a/src/game/base/patrol.cc
+++ b/src/game/base/patrol.cc
@@ -14,11 +14,13 @@ namespace game {
 	
 Patrol::WayPoint::WayPoint() {
 	waypoint_target = 0;
+	waypoint_cargo = 0;
 	waypoint_dock = false;
 }
 
 Patrol::WayPoint::~WayPoint() {
 	waypoint_target = 0;
+	waypoint_cargo = 0;
 }
 
 void Patrol::WayPoint::set_target_label(const std::string &label)
@@ -36,9 +38,9 @@ void Patrol::WayPoint::set_dock(const bool dock)
 	waypoint_dock = dock;
 }
 
-void Patrol::WayPoint::set_buy_label(const std::string &label)
+void Patrol::WayPoint::set_cargo(Cargo *cargo)
 {
-	waypoint_buy_label.assign(label);
+	waypoint_cargo = cargo;
 }
 
 /* --- Patrol ------------------------------------------------------ */
@@ -327,11 +329,46 @@ void Patrol::frame(const unsigned long elapsed)
 					if (group_docked) {
 						if (patrol_launch_timeout > 0 ) {
 							if (core::server()->timestamp() > patrol_launch_timeout) {
+								
+								// launch everyone
+								for (Members::iterator mit = patrol_members.begin(); mit != patrol_members.end(); ++mit) {
+									NPC *member = (*mit);
+									if (member->state() == core::Entity::Docked) {
+										
+										// buy cargo if requested
+										if (waypoint()->dock() && waypoint()->cargo() && member->inventory()) {
+											
+											// erase all cargo from inventory
+											for (core::Inventory::Items::iterator iit = member->inventory()->items().begin(); iit != member->inventory()->items().end(); ) {
+												
+												if ((*iit)->info()->type() == Cargo::infotype()) {
+													member->inventory()->items().erase(iit++);
+												} else {
+													++iit;
+												}
+			
+											}
+											
+											member->inventory()->recalculate();
+											
+											core::Item *item = new core::Item(waypoint()->cargo());
+											item->set_amount( (long)(member->inventory()->capacity_available() / waypoint()->cargo()->volume()));
+											member->inventory()->add(item);
+
+											member->inventory()->recalculate();
+										}
+										
+										member->launch();
+									}
+								}
+								
 								patrol_waypoint_current++;
 								if (patrol_waypoint_current == patrol_waypoints.end()) {
 									patrol_waypoint_current = patrol_waypoints.begin();
 								}
 								patrol_leader->set_autopilot_target(waypoint()->target());
+								
+								
 							}
 						} else {
 							patrol_launch_timeout = core::server()->timestamp() +  15000 + (unsigned long) math::randomi(30000);
diff --git a/src/game/base/patrol.h b/src/game/base/patrol.h
index debd0e4..4c612fe 100644
--- a/src/game/base/patrol.h
+++ b/src/game/base/patrol.h
@@ -9,6 +9,7 @@
 
 #include "core/entity.h"
 #include "base/faction.h"
+#include "base/cargo.h"
 #include "base/npc.h"
 
 namespace game
@@ -49,15 +50,15 @@ public:
 			return waypoint_dock;
 		}
 		
-		inline const std::string & buy_label() const {
-			return waypoint_buy_label;
+		inline const Cargo *cargo() const {
+			return waypoint_cargo;
 		}
 		
 		void set_target(core::Entity *entity);
 		
  		void set_target_label(const std::string &label);
 		
-		void set_buy_label(const std::string &label);
+		void set_cargo(Cargo *cargo);
 		
 		void set_dock(const bool dock);
 
@@ -66,7 +67,7 @@ public:
 		
 		core::Entity	*waypoint_target;
 		
-		std::string	waypoint_buy_label;
+		Cargo		*waypoint_cargo;
 		
 		bool 		waypoint_dock;
 	};
diff --git a/src/game/base/ship.cc b/src/game/base/ship.cc
index cdc860a..5df0ced 100644
--- a/src/game/base/ship.cc
+++ b/src/game/base/ship.cc
@@ -88,12 +88,12 @@ Ship::Ship(core::Player *owner, const ShipModel *shipmodel) : core::EntityContro
 		str.assign(aux::text_strip(owner->name()));
 		aux::text_strip(str);
 		set_name(str);
-		
-		// add an inventory
-		add_inventory();
-		inventory()->set_capacity(ship_shipmodel->maxcargo());
 	}
 	
+	// add an inventory
+	add_inventory();
+	inventory()->set_capacity(ship_shipmodel->maxcargo());
+	
 	if (model()) {
 		add_slots();
 		slots()->load(model());
-- 
cgit v1.2.3