diff options
Diffstat (limited to 'src/game/base/game.cc')
-rw-r--r-- | src/game/base/game.cc | 376 |
1 files changed, 193 insertions, 183 deletions
diff --git a/src/game/base/game.cc b/src/game/base/game.cc index 803657d..fcecf58 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -261,7 +261,7 @@ void Game::func_give(core::Player *player, const std::string &args) } else { player->set_credits(0); } - + player->set_dirty(); player->sound("game/buy"); } else { @@ -335,7 +335,12 @@ void Game::func_buy(core::Player *player, const std::string &args) amount = 0; if (typestr.compare("ship") == 0) { - ShipDealer::func_buy(player, labelstr); + ShipModel *shipmodel = ShipModel::find(labelstr); + if (shipmodel) { + shipmodel->buy(player->control(), player->view()); + } else { + player->send("Unkown ship type '" + labelstr + "'"); + } } else if (typestr.compare("cargo") == 0) { Cargo *cargo = Cargo::find(labelstr); if (cargo) { @@ -672,16 +677,20 @@ bool Game::load_zone(core::Zone *zone) size_t count = 0; - Station *station = 0; - Planet *planet = 0; - Star *star = 0; - NavPoint *navpoint = 0; - JumpPoint *jumppoint = 0; - RaceTrack *racetrack = 0; - CheckPoint *checkpoint = 0; - core::Entity *entity = 0; + core::Entity *entity = 0; + core::Inventory *inventory = 0; + core::Item *item = 0; + + Station *station = 0; + Planet *planet = 0; + Star *star = 0; + NavPoint *navpoint = 0; + JumpPoint *jumppoint = 0; + RaceTrack *racetrack = 0; + CheckPoint *checkpoint = 0; bool b; + long l; std::string strval; @@ -696,41 +705,49 @@ bool Game::load_zone(core::Zone *zone) } else if (zoneini.got_section("star")) { star = new Star(); + entity = star; star->set_zone(zone); count ++; } else if (zoneini.got_section("navpoint")) { navpoint = new NavPoint(); + entity = navpoint; navpoint->set_zone(zone); count ++; } else if (zoneini.got_section("jumpgate")) { jumppoint = new JumpGate(); + entity = jumppoint; jumppoint->set_zone(zone); count ++; } else if (zoneini.got_section("jumppoint")) { jumppoint = new JumpPoint(); + entity = jumppoint; jumppoint->set_zone(zone); count ++; } else if (zoneini.got_section("racetrack")) { racetrack = new RaceTrack(); + entity = racetrack; racetrack->set_zone(zone); } else if (zoneini.got_section("checkpoint")) { checkpoint = new CheckPoint(racetrack); + entity = checkpoint; if (!racetrack) { - con_warn << zoneini.name() << " checkpoint without racetrack at line " << zoneini.line() << std::endl; + zoneini.unknown_error("checkpoint without racetrack"); } } else if (zoneini.got_section("planet")) { planet = new Planet(); + entity = planet; planet->set_zone(zone); count ++; } else if (zoneini.got_section("station")) { station = new Station(); + entity = station; station->set_zone(zone); count ++; @@ -739,7 +756,41 @@ bool Game::load_zone(core::Zone *zone) entity->set_flag(core::Entity::Static); entity->set_zone(zone); count ++; - + + } else if (zoneini.got_section("cargo")) { + // new cargo trading definition for the current base + item = 0; + inventory = 0; + + if (!entity) { + zoneini.unknown_error("cargo definition without entity"); + } else if ((entity->moduletype() != planet_enttype) && (entity->moduletype() != station_enttype)) { + zoneini.unknown_error("cargo definition for invalid entity type"); + } else { + inventory = entity->inventory(); + if (!inventory) { + inventory = new core::Inventory(); + entity->set_inventory(inventory); + } + } + + } else if (zoneini.got_section("ship")) { + // new ship trading definition for the current base + item = 0; + inventory = 0; + + if (!entity) { + zoneini.unknown_error("ship definition without entity"); + } else if ((entity->moduletype() != planet_enttype) && (entity->moduletype() != station_enttype)) { + zoneini.unknown_error("ship definition for invalid entity type"); + } else { + inventory = entity->inventory(); + if (!inventory) { + inventory = new core::Inventory(); + entity->set_inventory(inventory); + } + } + } else { zoneini.unknown_section(); } @@ -841,8 +892,68 @@ bool Game::load_zone(core::Zone *zone) } else { zoneini.unkown_key(); } - } - + + } else if (zoneini.in_section("cargo")) { + // cargo definition for a station or planet + if (!entity || !inventory) { + continue; + } + + if (zoneini.got_key_label("label", strval)) { + Cargo *cargo = Cargo::find(strval); + if (cargo) { + item = inventory->find(cargo); + if (!item) { + item = new core::Item(cargo); + item->set_amount(-1); + item->set_price(cargo->price()); + inventory->add(item); + } + } else { + zoneini.unknown_error("unkown cargo type '" + strval + "'"); + } + + } else if (zoneini.got_key_long("price", l)) { + if (item) { + item->set_price(l); + } + } else if (zoneini.got_key_long("amount", l)) { + if (item) { + item->set_amount(l); + } + } else { + zoneini.unkown_key(); + } + + } else if (zoneini.in_section("ship")) { + // ship definition for a station or planet + if (!entity || !inventory) { + continue; + } + + if (zoneini.got_key_label("label", strval)) { + ShipModel *shipmodel= ShipModel::find(strval); + if (shipmodel) { + item = inventory->find(shipmodel); + if (!item) { + item = new core::Item(shipmodel); + item->set_amount(-1); + item->set_price(shipmodel->price()); + inventory->add(item); + } + } else { + zoneini.unknown_error("unkown ship type '" + strval + "'"); + } + + } else if (zoneini.got_key_long("price", l)) { + if (item) { + item->set_price(l); + } + + } else { + zoneini.unkown_key(); + } + } } } zoneini.close(); @@ -874,204 +985,103 @@ bool Game::validate_zone(core::Zone *zone) station->init_physics(0.0f); } - if ((entity->flags() & core::Entity::Dockable) == core::Entity::Dockable) - load_menus(entity, "zones/" + zone->label() + "/" + entity->label()); + if ((entity->flags() & core::Entity::Dockable) == core::Entity::Dockable) { + generate_entity_menus(entity); + } } } return true; } -bool Game::load_menus(core::Entity *entity, const std::string &menufilename) +bool Game::generate_entity_menus(core::Entity *entity) { using core::MenuDescription; using core::ButtonDescription; if ((entity->moduletype() != planet_enttype) && (entity->moduletype() != station_enttype)) { + //con_warn << "Can not generate menus for entity '" << entity->label() << "'" << std::endl; + // not dockable return false; } - - filesystem::IniFile inifile; - - std::string strval; - long l; - + MenuDescription *menu_dealer = 0; ButtonDescription *button = 0; - ShipDealer *shipdealer = 0; - core::Inventory *inventory = 0; - core::Item *item = 0; - - inifile.open(menufilename); - - if (inifile.is_open()) { - while (inifile.getline()) { - - if (inifile.got_section()) { - - item = 0; - - if (inifile.got_section("dealer")) { - // TODO replace [dealer] section with individual [ship] sections - // dealer menu - if (!menu_dealer) { - menu_dealer = new MenuDescription(); - menu_dealer->set_label("ships"); - menu_dealer->set_text("Ships"); - - shipdealer = new ShipDealer(); - if (entity->moduletype() == planet_enttype) { - static_cast<Planet *>(entity)->set_shipdealer(shipdealer); - } else if (entity->moduletype() == station_enttype) { - static_cast<Station *>(entity)->set_shipdealer(shipdealer); - } - } - - } else if (inifile.got_section("cargo")) { - - } else if (inifile.got_section("ship")) { - - } else { - inifile.unknown_section(); - } - - } else if (inifile.got_key()) { - - if (inifile.in_section("dealer")) { - - if (inifile.got_key_string("ship", strval)) { - aux::to_label(strval); - ShipModel *model = shipdealer->add(strval); - if (model) { - button = new ButtonDescription(); - button->set_text("buy " + model->name()); - std::ostringstream str(""); - str << "buy " << model->id(); - button->set_command(str.str() , ButtonDescription::CommandMenu); - button->set_info(model); - button->set_alignment(ButtonDescription::Left); - menu_dealer->add_button(button); - } else { - std::string msg("unknown ship type '"); - msg.append(strval); - msg.append("'"); - inifile.unknown_error(msg); - } - } else { - inifile.unkown_key(); - } - - } else if (inifile.in_section("cargo")) { - - if (inifile.got_key_label("label", strval)) { - Cargo *cargo = Cargo::find(strval); - if (cargo) { - if (!inventory) { - inventory = new core::Inventory(); - } - item = inventory->find(cargo); - if (!item) { - item = new core::Item(cargo); - item->set_amount(-1); - item->set_price(cargo->price()); - inventory->add(item); - } - } else { - std::string msg("unkown cargo type '"); - msg.append(strval); - msg.append("'"); - inifile.unknown_error(msg); - } - - } else if (inifile.got_key_long("price", l)) { - if (item) { - item->set_price(l); - } - } else if (inifile.got_key_long("amount", l)) { - if (item) { - item->set_amount(l); - } - } else { - inifile.unkown_key(); - } - - } else if (inifile.in_section("ship")) { - - if (inifile.got_key_label("label", strval)) { - ShipModel *shipmodel = ShipModel::find(strval); - if (shipmodel) { - if (!inventory) { - inventory = new core::Inventory(); - } - item = inventory->find(shipmodel); - if (!item) { - item = new core::Item(shipmodel); - item->set_price(shipmodel->price()); - item->set_amount(-1); - inventory->add(item); - } - } else { - std::string msg("unknown ship type '"); - msg.append(strval); - msg.append("'"); - inifile.unknown_error(msg); - } - } else if (inifile.got_key_long("price", l)) { - if (item) { - item->set_price(l); - } - } else { - inifile.unkown_key(); - } - } - } - } - - } - - if (inventory) - entity->set_inventory(inventory); - + // dockable entity + // add main menu MenuDescription *menu_main = new MenuDescription(); menu_main->set_label("main"); menu_main->set_text("Launch area"); entity->add_menu(menu_main); + // add launch button button = new ButtonDescription(); button->set_text("Launch"); button->set_command("launch", ButtonDescription::CommandGame); button->set_alignment(ButtonDescription::Center); menu_main->add_button(button); - button = new ButtonDescription(); - button->set_text("Trade"); - button->set_command("trade cargo", ButtonDescription::CommandMenu); - button->set_alignment(ButtonDescription::Center); - menu_main->add_button(button); - - if (menu_dealer) { - button = new ButtonDescription(); - button->set_text("Return"); - button->set_command("main", ButtonDescription::CommandMenu); - button->set_alignment(ButtonDescription::Center); - menu_dealer->add_button(button); - - entity->add_menu(menu_dealer); - - button = new ButtonDescription(); - button->set_text("Ships"); - button->set_command("ships", ButtonDescription::CommandMenu); - button->set_alignment(ButtonDescription::Center); - menu_main->add_button(button); - } + // add trade menus + if (entity->inventory()) { + size_t nbcargo = 0; + size_t nbships = 0; + + for (core::Inventory::Items::const_iterator it = entity->inventory()->items().begin(); it != entity->inventory()->items().end(); it++) { + core::Item *item = (*it); + + if (item->info()->type() == Cargo::cargo_infotype) { + + nbcargo++; + + } else if (item->info()->type() == ShipModel::shipmodel_infotype) { + if (!menu_dealer) { + menu_dealer = new MenuDescription(); + menu_dealer->set_label("ships"); + menu_dealer->set_text("Ships"); + } + + button = new ButtonDescription(); + button->set_text("buy " + item->info()->name()); + + std::ostringstream str(""); + str << "buy " << item->info()->id(); + button->set_command(str.str() , ButtonDescription::CommandMenu); + button->set_info(item->info()); + button->set_alignment(ButtonDescription::Left); + + menu_dealer->add_button(button); + nbships++; + } + } + + if (nbcargo > 0) { + con_debug << " " << entity->label() << " " << nbcargo << " cargo " << aux::plural("type", nbcargo) << std::endl; + button = new ButtonDescription(); + button->set_text("Trade"); + button->set_command("trade cargo", ButtonDescription::CommandMenu); + button->set_alignment(ButtonDescription::Center); + menu_main->add_button(button); + } - if (inifile.is_open()) { - size_t n = entity->menus().size(); - con_debug << " " << inifile.name() << " " << n << " " << aux::plural("menu", n) << std::endl; - inifile.close(); + if (nbships > 0) { + con_debug << " " << entity->label() << " " << nbcargo << " ship " << aux::plural("type", nbcargo) << std::endl; + button = new ButtonDescription(); + button->set_text("Return"); + button->set_command("main", ButtonDescription::CommandMenu); + button->set_alignment(ButtonDescription::Center); + menu_dealer->add_button(button); + + entity->add_menu(menu_dealer); + + button = new ButtonDescription(); + button->set_text("Ships"); + button->set_command("ships", ButtonDescription::CommandMenu); + button->set_alignment(ButtonDescription::Center); + menu_main->add_button(button); + } } - + return true; } |