From b32409dfb18edd67eb7fdafbe2f0862b45c4c7ca Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Sat, 14 Apr 2018 17:13:45 +0100 Subject: [PATCH] [Move] AI stuff could be useful for autopilot, so moved to Ship. --- src/Makefile.am | 4 +-- src/ai_ship.cpp | 77 ------------------------------------------------- src/ai_ship.h | 32 -------------------- src/body.cpp | 4 --- src/main.cpp | 5 ++-- src/object.h | 2 +- src/ship.cpp | 58 +++++++++++++++++++++++++++++++++++++ src/ship.h | 13 +++++++++ 8 files changed, 76 insertions(+), 119 deletions(-) delete mode 100644 src/ai_ship.cpp delete mode 100644 src/ai_ship.h diff --git a/src/Makefile.am b/src/Makefile.am index 66e5d4a..bb1f974 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,7 @@ include_HEADERS = body.h frame.h generic_system_view.h glfreetype.h gui_button.h planet.h player.h dynamic_body.h sector.h sector_view.h ship_cpanel.h ship.h space.h star.h star_system.h system_info_view.h \ system_view.h vector3.h view.h world_view.h date.h space_station.h space_station_view.h model_body.h gui_iselectable.h \ ship_type.h object.h info_view.h model_coll_mesh_data.h object_viewer_view.h fixed.h custom_starsystems.h gameconsts.h \ - aabb.h serializer.h sfx.h ai_ship.h + aabb.h serializer.h sfx.h libgui_a_SOURCES = gui_button.cpp gui.cpp gui_fixed.cpp gui_screen.cpp gui_label.cpp gui_toggle_button.cpp gui_radio_button.cpp \ gui_radio_group.cpp gui_image_button.cpp gui_image.cpp gui_image_radio_button.cpp gui_multi_state_image_button.cpp gui_widget.cpp \ @@ -20,7 +20,7 @@ Lephisto3D_SOURCES = main.cpp glfreetype.cpp body.cpp space.cpp ship.cpp player. star.cpp frame.cpp ship_cpanel.cpp sector_view.cpp mtrand.cpp world_view.cpp system_view.cpp \ star_system.cpp sector.cpp system_info_view.cpp generic_system_view.cpp date.cpp space_station.cpp \ space_station_view.cpp model_body.cpp ship_type.cpp info_view.cpp model_coll_mesh_data.cpp \ - object_viewer_view.cpp custom_starsystems.cpp serializer.cpp sfx.cpp ai_ship.cpp + object_viewer_view.cpp custom_starsystems.cpp serializer.cpp sfx.cpp Lephisto3D_LDADD = sbre/libsbre.a collider/libcollider.a libgui.a ModelViewer_SOURCES = sbre_viewer.cpp glfreetype.cpp diff --git a/src/ai_ship.cpp b/src/ai_ship.cpp deleted file mode 100644 index eb149ca..0000000 --- a/src/ai_ship.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "libs.h" -#include "ai_ship.h" -#include "serializer.h" -#include "l3d.h" - -bool AIShip::DoKill(const Ship* enemy) { - /* Need to deal with frames, large distances, and success! */ - if(GetFrame() == enemy->GetFrame()) { - vector3d dir = vector3d::Normalize(enemy->GetPosition() - GetPosition()); - AIFaceDirection(dir); - } - return false; -} - -void AIShip::TimeStepUpdate(const float timeStep) { - bool done = false; - - if(m_todo.size() != 0) { - Instruction& inst = m_todo.front(); - switch(inst.cmd) { - case DO_KILL: - done = DoKill(static_cast(inst.arg)); - break; - case DO_NOTHING: done = true; break; - } - } - if(done) { - printf("AI '%s' successfully executed %d:'%s'\n", GetLabel().c_str(), m_todo.front().cmd, - static_cast(m_todo.front().arg)->GetLabel().c_str()); - m_todo.pop_front(); - } - - Ship::TimeStepUpdate(timeStep); -} - -void AIShip::Save(void) { - using namespace Serializer::Write; - Ship::Save(); - wr_int(m_todo.size()); - for(std::list::iterator i = m_todo.begin(); i != m_todo.end(); ++i) { - wr_int((int)(*i).cmd); - switch((*i).cmd) { - case DO_KILL: - wr_int(Serializer::LookupBody(static_cast((*i).arg))); - case DO_NOTHING: wr_int(0); break; - } - } -} - -void AIShip::Load(void) { - using namespace Serializer::Read; - Ship::Load(); - int num = rd_int(); - while(num-- > 0) { - Command c= (Command)rd_int(); - void* arg = (void*)rd_int(); - printf("COMMAND %d:%p\n", c, arg); - m_todo.push_back(Instruction(c, arg)); - } -} - -void AIShip::PostLoadFixup(void) { - Ship::PostLoadFixup(); - for(std::list::iterator i = m_todo.begin(); i != m_todo.end(); ++i) { - switch((*i).cmd) { - case DO_KILL: - (*i).arg = Serializer::LookupBody((size_t)(*i).arg); - break; - case DO_NOTHING: break; - } - } -} - -void AIShip::Instruct(enum Command cmd, void* arg) { - m_todo.push_back(Instruction(cmd, arg)); -} - diff --git a/src/ai_ship.h b/src/ai_ship.h deleted file mode 100644 index 6b08328..0000000 --- a/src/ai_ship.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include -#include "ship.h" -#include "ship_type.h" - -class AIShip: public Ship { -public: - OBJDEF(AIShip, Ship, AISHIP); - AIShip(ShipType::Type shipType): Ship(shipType) { } - AIShip(void): Ship() { } - void TimeStepUpdate(const float timeStep); - - enum Command { DO_NOTHING, DO_KILL }; - void Instruct(enum Command, void* arg); - void ClearInstructions(void) { m_todo.clear(); } - virtual void PostLoadFixup(void); - -protected: - virtual void Save(void); - virtual void Load(void); -private: - bool DoKill(const Ship*); - class Instruction { - public: - Command cmd; - void* arg; - Instruction(Command c, void* a): cmd(c), arg(a) { } - }; - - std::list m_todo; -}; - diff --git a/src/body.cpp b/src/body.cpp index 6714b1d..3db9ced 100644 --- a/src/body.cpp +++ b/src/body.cpp @@ -6,7 +6,6 @@ #include "planet.h" #include "space_station.h" #include "ship.h" -#include "ai_ship.h" #include "player.h" #include "sfx.h" @@ -49,7 +48,6 @@ void Body::Serialize(void) { case Object::PLANET: case Object::SPACESTATION: case Object::SHIP: - case Object::AISHIP: case Object::PLAYER: case Object::SFX: Save(); @@ -76,8 +74,6 @@ Body* Body::Unserialize(void) { b = new SpaceStation(); break; case Object::SHIP: b = new Ship(); break; - case Object::AISHIP: - b = new AIShip(); break; case Object::PLAYER: b = new Player(); break; case Object::SFX: diff --git a/src/main.cpp b/src/main.cpp index 5bf4786..8f647b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,7 +19,6 @@ #include "space_station_view.h" #include "info_view.h" #include "serializer.h" -#include "ai_ship.h" float L3D::timeAccel = 1.0f; int L3D::scrWidth; @@ -202,8 +201,8 @@ void L3D::HandleEvents(void) { station->SetPosition(L3D::player->GetPosition()+5000.0*dir); Space::AddBody(station); } else { - AIShip* ship = new AIShip(ShipType::LADYBIRD); - ship->Instruct(AIShip::DO_KILL, L3D::player); + Ship* ship = new Ship(ShipType::LADYBIRD); + ship->AIInstruct(Ship::DO_KILL, L3D::player); ship->SetLabel("A friend?"); ship->SetFrame(L3D::player->GetFrame()); ship->SetPosition(L3D::player->GetPosition()+100.0*dir); diff --git a/src/object.h b/src/object.h index ca3fc48..53cb1ce 100644 --- a/src/object.h +++ b/src/object.h @@ -2,7 +2,7 @@ class Object { public: - enum Type { OBJECT, BODY, MODELBODY, DYNAMICBODY, SHIP, PLAYER, SPACESTATION, PLANET, STAR, SFX, AISHIP }; + enum Type { OBJECT, BODY, MODELBODY, DYNAMICBODY, SHIP, PLAYER, SPACESTATION, PLANET, STAR, SFX }; virtual Type GetType(void) { return OBJECT; } virtual bool IsType(Type c) { return GetType() == c; } }; diff --git a/src/ship.cpp b/src/ship.cpp index 6a0badd..7a7a0a5 100644 --- a/src/ship.cpp +++ b/src/ship.cpp @@ -46,6 +46,16 @@ void Ship::Save(void) { wr_int(Serializer::LookupBody(m_dockedWith)); printf("TODO: NOT SAVING SHIP EQUIPMENT YET!!\n"); wr_float(m_stats.hull_mass_left); + wr_int(m_todo.size()); + for(std::list::iterator i = m_todo.begin(); i != m_todo.end(); ++i) { + wr_int((int)(*i).cmd); + switch((*i).cmd) { + case DO_KILL: + wr_int(Serializer::LookupBody(static_cast((*i).arg))); + break; + case DO_NOTHING: wr_int(0); break; + } + } } void Ship::Load(void) { @@ -74,6 +84,13 @@ void Ship::Load(void) { m_equipment = EquipSet(m_shipType); Init(); m_stats.hull_mass_left = rd_float(); /* Must be after Init();.. */ + int num = rd_int(); + while(num-- > 0) { + AICommand c = (AICommand)rd_int(); + void* arg = (void*)rd_int(); + printf("COMMAND %d:%p\n", c, arg); + m_todo.push_back(AIInstruction(c, arg)); + } } void Ship::Init(void) { @@ -88,6 +105,14 @@ void Ship::PostLoadFixup(void) { m_combatTarget = Serializer::LookupBody((size_t)m_combatTarget); m_navTarget = Serializer::LookupBody((size_t)m_navTarget); m_dockedWith = (SpaceStation*)Serializer::LookupBody((size_t)m_dockedWith); + for(std::list::iterator i = m_todo.begin(); i != m_todo.end(); ++i) { + switch((*i).cmd) { + case DO_KILL: + (*i).arg = Serializer::LookupBody((size_t)(*i).arg); + break; + case DO_NOTHING: break; + } + } } Ship::Ship(ShipType::Type shipType) : DynamicBody() { @@ -284,6 +309,7 @@ void Ship::TestLanded(void) { void Ship::TimeStepUpdate(const float timeStep) { DynamicBody::TimeStepUpdate(timeStep); + AITimeStep(timeStep); m_dockingTimer = (m_dockingTimer-timeStep > 0 ? m_dockingTimer-timeStep : 0); @@ -347,6 +373,38 @@ void Ship::TimeStepUpdate(const float timeStep) { if(m_testLanded) TestLanded(); } +void Ship::AITimeStep(const float timeStep) { + bool done = false; + + if(m_todo.size() != 0) { + AIInstruction& inst = m_todo.front(); + switch(inst.cmd) { + case DO_KILL: + done = AICmdKill(static_cast(inst.arg)); + break; + case DO_NOTHING: done = true; break; + } + } + if(done) { + printf("AI '%s' succesfully executed %d:'%s'\n", GetLabel().c_str(), m_todo.front().cmd, + static_cast(m_todo.front().arg)->GetLabel().c_str()); + m_todo.pop_front(); + } +} + +bool Ship::AICmdKill(const Ship* enemy) { + /* Needs to deal with frames, large distances, and success. */ + if(GetFrame() == enemy->GetFrame()) { + vector3d dir = vector3d::Normalize(enemy->GetPosition() - GetPosition()); + AIFaceDirection(dir); + } + return false; +} + +void Ship::AIInstruct(enum AICommand cmd, void* arg) { + m_todo.push_back(AIInstruction(cmd, arg)); +} + void Ship::NotifyDeath(const Body* const dyingBody) { if(GetNavTarget() == dyingBody) SetNavTarget(0); diff --git a/src/ship.h b/src/ship.h index e4117f5..4b6dec1 100644 --- a/src/ship.h +++ b/src/ship.h @@ -1,4 +1,5 @@ #pragma once +#include #include "libs.h" #include "dynamic_body.h" #include "ship_type.h" @@ -50,6 +51,9 @@ public: EquipSet m_equipment; + enum AICommand { DO_NOTHING, DO_KILL }; + void AIInstruct(enum AICommand, void* arg); + void AiClearInstruction(void) { m_todo.clear(); } virtual void PostLoadFixup(void); protected: virtual void Save(void); @@ -61,6 +65,7 @@ protected: enum ShipType::Type m_shipType; Uint32 m_gunState[ShipType::GUNMOUNT_MAX]; private: + void AITimeStep(const float timeStep); void Init(void); bool IsFiringLasers(void); void TestLanded(void); @@ -78,5 +83,13 @@ private: Body* m_navTarget; Body* m_combatTarget; shipstats_t m_stats; + class AIInstruction { + public: + AICommand cmd; + void* arg; + AIInstruction(AICommand c, void* a): cmd(c), arg(a) { } + }; + std::list m_todo; + bool AICmdKill(const Ship*); };