Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: db13260c35644a9abd8c5d88ae3ad010e1e08d0c (plain)
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
/*
   base/patrol.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "base/patrol.h"
#include "base/game.h"

namespace game {

/* --- WayPoint atrol ------------------------------------------------------ */
	
Patrol::WayPoint::WayPoint() {
	waypoint_target = 0;
	waypoint_dock = false;
}

Patrol::WayPoint::~WayPoint() {
	waypoint_target = 0;
}

void Patrol::WayPoint::set_target_label(const std::string &label)
{
	waypoint_target_label.assign(label);
}

void Patrol::WayPoint::set_target(core::Entity *entity)
{
	waypoint_target = entity;
}

void Patrol::WayPoint::set_dock(const bool dock)
{
	waypoint_dock = dock;
}

void Patrol::WayPoint::set_buy_label(const std::string &label)
{
	waypoint_buy_label.assign(label);
}

/* --- Patrol ------------------------------------------------------ */

Patrol::Patrol() : core::Entity()
{
	// this is a server-side only entity
	set_serverside(true);
	
	unset_flag(core::Entity::ShowOnMap);
	set_flag(core::Entity::NonSolid);
	
	entity_moduletypeid = patrol_enttype;
	
	set_label("patrol");
	set_radius(1.0f);
	
	patrol_profile = NPC::ProfilePatrol;
}


Patrol::~Patrol()
{
	for (WayPoints::iterator it = patrol_waypoints.begin(); it != patrol_waypoints.end(); ++it) {
		delete (*it);
		(*it) = 0;
	}
	patrol_waypoints.clear();
}

void Patrol::set_profile(const NPC::Profile profile)
{
	patrol_profile = profile;
}

void Patrol::validate()
{
	int waypoint_counter = 1;
	
	for (WayPoints::iterator it = patrol_waypoints.begin(); it != patrol_waypoints.end(); ++it) {
		WayPoint *waypoint = (*it);

		if (waypoint->target_label().size() == 0) {
			con_warn << "  Patrol '" << label() << "' WayPoint " << waypoint_counter << " has no target" << "'\n";
			break;
		}
		
		std::string entitylabel;
		std::string zonelabel;
		
		core::Zone *targetzone = 0;

		size_t pos = waypoint->target_label().find(':');		
		if ((pos == std::string::npos) || (pos < 1) || (pos >= (waypoint->target_label().size() - 1))) {
			targetzone = zone();
			
			zonelabel.assign(zone()->label());
			entitylabel.assign(waypoint->target_label());
		} else {
			zonelabel.assign(waypoint->target_label().substr(0, pos));
			entitylabel.assign(waypoint->target_label().substr(pos + 1, waypoint->target_label().size() - pos));
		
			core::Zone::find(zonelabel);
			if (!targetzone) {
				con_warn << "  Patrol '" << label() << "' waypoint " << waypoint_counter << " has invalid target zone '" << zonelabel << "'\n";
				break;
			}
		}
		
		core::Entity *targetentity = targetzone->find_entity(entitylabel);
		if (!targetentity) {
			con_warn << "  Patrol '" << label() << "' waypoint " << waypoint_counter << " has unknown target '" << entitylabel << "' in zone '" << zonelabel << "'\n";
			break;
		}

		if(waypoint->dock() && !targetentity->has_flag(core::Entity::Dockable)) {
			con_warn << "  Patrol '" << label() << "' waypoint " << waypoint_counter << " set to dock at non-doackable target '" << entitylabel << "' in zone '" << zonelabel << "'\n";
		}
		
		waypoint->set_target(targetentity);
	}
	
	// remove invalid waypoints
	for (WayPoints::iterator it = patrol_waypoints.begin(); it != patrol_waypoints.end(); ) {
		WayPoint *waypoint = (*it);
		
		if (!waypoint->target()) {
			delete waypoint;
			(*it) = 0;
			
			patrol_waypoints.erase(it++);
		} else {
			++it;
		}
	}
	
	size_t nbships = 0;
	for (core::Inventory::Items::const_iterator it = inventory()->items().begin(); it != inventory()->items().end(); it++) {
		core::Item *item = (*it);
	
		if (item->info()->type() == ShipModel::infotype()) {
			nbships++;
		}
	}
	
	if (patrol_waypoints.size() == 0) {
		con_warn << "  Patrol '" << label() << "' without waypoints" << "\n";
		die();
		
	} else if (nbships == 0) {
		con_warn << "  Patrol '" << label() << "' without ship types" << "\n";
		die();
		
	} else {
		con_debug << "  " << label() << " patrol with " << patrol_waypoints.size() << "waypoints" << " and " << nbships << " ship " << aux::plural("type", nbships) << std::endl;
	}
}

Patrol::WayPoint *Patrol::add_waypoint()
{
	WayPoint *waypoint = new WayPoint();

	patrol_waypoints.push_back(waypoint);
	
	return waypoint;
}

void Patrol::frame(const unsigned long elapsed)
{

}

}