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

#include "auxiliary/functions.h"
#include "base/game.h"
#include "base/planet.h"
#include "base/shipdealer.h"
#include "base/station.h"
#include "sys/sys.h"

namespace game {

ShipDealer::ShipDealer()
{
}

ShipDealer::~ShipDealer()
{
	dealer_models.clear();
}

ShipModel *ShipDealer::add(const std::string &modelname)
{
	ShipModel *model = ShipModel::find(modelname);

	if (!model) {
		con_warn << "Ship model '" + modelname + "' not found" << std::endl;
		return 0;
	}

	for (Models::iterator it = dealer_models.begin(); it != dealer_models.end(); it++) {
		if ((*it) == model) {
			con_warn << "Ship dealer already has " + model->name() << std::endl;
			return 0;
		}
	}
	dealer_models.push_back(model);
	return model;
}

ShipModel *ShipDealer::find(const std::string &modelname) const
{
	for (Models::const_iterator it = dealer_models.begin(); it != dealer_models.end(); it++) {
		if ((*it)->label().compare(modelname) == 0) {
			return (*it);
		}
	}

	return 0;
}

ShipModel *ShipDealer::find(ShipModel *shipmodel) const
{
	for (Models::const_iterator it = dealer_models.begin(); it != dealer_models.end(); it++) {
		if ((*it) == shipmodel) {
			return (*it);
		}
	}

	return 0;
}

// a player buys a ship
void ShipDealer::func_buy(core::Player *player, const std::string &args)
{
	core::Entity *dock = player->view();
	
	ShipDealer *shipdealer = 0;

	// find the ship model
	ShipModel *shipmodel = ShipModel::find(args);
	if (!shipmodel) {
		if (!Game::g_devel->value()) {
			player->send("Cheats disabled");
			return;
		} else {
			shipmodel = ShipModel::search(args);
		}
	}

	if (!shipmodel) {
		std::string helpstr;
		for (ShipModel::iterator smit = ShipModel::registry.begin(); smit != ShipModel::registry.end(); smit++) {
			if (helpstr.size())
				helpstr.append("^N|^B");
			helpstr.append((*smit).second->label());
		}
		player->send("Usage: buy ship [^B" + helpstr + "^N]");
		return;
	}

	// find the ship dealer we're buying the ship from
	if (player->view()) {
		if (player->view()->moduletype() == station_enttype) {
			shipdealer = static_cast<Station *>(player->view())->shipdealer();
		} else if (player->view()->moduletype() == planet_enttype) {
			shipdealer = static_cast<Planet *>(player->view())->shipdealer();
		}
	}

	if (!Game::g_devel->value()) {
		
		if (!shipdealer) {
			player->send("No ship dealer available");
			return;
		}

		if (!shipdealer->find(shipmodel)) {
			player->send("Ship dealer does not sell the " + shipmodel->name());
			return;
		}

		// check price
		if (shipmodel->price() > player->credits()) {
			std::stringstream msgstr;
			msgstr << "You require " <<  (shipmodel->price() - player->credits()) << " additional credits to buy the " << shipmodel->name();
			player->send(msgstr.str());
			return;
		}

		player->add_credits(-shipmodel->price());
	}

	// player has only ship for now
	if (player->control()) {
		player->remove_asset(player->control());
	}

	Ship * ship = new Ship(player, shipmodel);
	if (dock) {
		ship->set_zone(dock->zone());
		ship->location().assign(dock->location());
		ship->set_state(core::Entity::Docked);
		ship->entity_axis.assign(dock->axis());
 		ship->entity_axis.change_direction(180.0f);
		player->set_control(ship);
		player->set_view(dock);
	} else {
		ship->set_zone(player->zone());
		player->set_control(ship);
	}

	// send the ship purchased message
	std::stringstream msgstr;
	msgstr << "^BPurchased " << aux::article(shipmodel->name());
	if (!Game::g_devel->value()) {
		msgstr << " for " << shipmodel->price() << " credits";
	}		
	player->send(msgstr.str());
	player->sound("game/buy-ship");	
}


}