Fixed produce() and NewScript not marking programs as loaded from level files (closes #613); refactored NewScript to std::vector and std::string

master
krzys-h 2015-08-29 16:52:07 +02:00
parent bd9a56fe7b
commit 6711154762
4 changed files with 23 additions and 34 deletions

View File

@ -4372,45 +4372,34 @@ bool CRobotMain::ReadFileStack(CObject *obj, FILE *file, int objRank)
//! Empty the list
bool CRobotMain::FlushNewScriptName()
void CRobotMain::FlushNewScriptName()
{
for (int i = 0; i < MAXNEWSCRIPTNAME; i++)
m_newScriptName[i].used = false;
return true;
m_newScriptName.clear();
}
//! Adds a script name
bool CRobotMain::AddNewScriptName(ObjectType type, char *name)
void CRobotMain::AddNewScriptName(ObjectType type, const std::string& name)
{
for (int i = 0; i < MAXNEWSCRIPTNAME; i++)
{
if (!m_newScriptName[i].used)
{
m_newScriptName[i].used = true;
m_newScriptName[i].type = type;
strcpy(m_newScriptName[i].name, name);
return true;
}
}
return false;
NewScriptName newscript;
newscript.type = type;
newscript.name = name;
m_newScriptName.push_back(newscript);
}
//! Seeks a script name for a given type
char* CRobotMain::GetNewScriptName(ObjectType type, int rank)
std::string CRobotMain::GetNewScriptName(ObjectType type, int rank)
{
for (int i = 0; i < MAXNEWSCRIPTNAME; i++)
for (unsigned int i = 0; i < m_newScriptName.size(); i++)
{
if (m_newScriptName[i].used &&
(m_newScriptName[i].type == type ||
m_newScriptName[i].type == OBJECT_NULL))
if (m_newScriptName[i].type == type ||
m_newScriptName[i].type == OBJECT_NULL )
{
if (rank == 0) return m_newScriptName[i].name;
else rank --;
}
}
return nullptr;
return "";
}

View File

@ -112,13 +112,10 @@ class CDisplayText;
class CDisplayInfo;
}
const int MAXNEWSCRIPTNAME = 20;
struct NewScriptName
{
bool used = false;
ObjectType type = OBJECT_NULL;
char name[40] = {};
std::string name = "";
};
@ -280,9 +277,9 @@ public:
bool SaveFileStack(CObject *pObj, FILE *file, int objRank);
bool ReadFileStack(CObject *pObj, FILE *file, int objRank);
bool FlushNewScriptName();
bool AddNewScriptName(ObjectType type, char *name);
char* GetNewScriptName(ObjectType type, int rank);
void FlushNewScriptName();
void AddNewScriptName(ObjectType type, const std::string& name);
std::string GetNewScriptName(ObjectType type, int rank);
void SelectPlayer(std::string playerName);
CPlayerProfile* GetPlayerProfile();
@ -534,7 +531,7 @@ protected:
std::map<int, std::string> m_teamNames;
NewScriptName m_newScriptName[MAXNEWSCRIPTNAME];
std::vector<NewScriptName> m_newScriptName;
float m_cameraPan = 0.0f;
float m_cameraZoom = 0.0f;

View File

@ -26,6 +26,7 @@
#include "level/robotmain.h"
#include "level/parser/parser.h"
#include "level/parser/parserline.h"
#include "level/parser/parserparam.h"
@ -665,11 +666,12 @@ bool CAutoFactory::CreateVehicle()
CProgramStorageObject* programStorage = dynamic_cast<CProgramStorageObject*>(vehicle);
for ( int i=0 ; ; i++ )
{
char* name = m_main->GetNewScriptName(m_type, i);
if ( name == nullptr ) break;
std::string name = m_main->GetNewScriptName(m_type, i);
if (name.empty()) break;
Program* prog = programStorage->GetOrAddProgram(i);
programStorage->ReadProgram(prog, name);
programStorage->ReadProgram(prog, InjectLevelPathsForCurrentLevel(name));
prog->readOnly = true;
prog->filename = name;
}
}

View File

@ -1607,6 +1607,7 @@ bool CScriptFunctions::rProduce(CBotVar* var, CBotVar* result, int& exception, v
CProgramStorageObject* programStorage = dynamic_cast<CProgramStorageObject*>(object);
Program* program = programStorage->AddProgram();
programStorage->ReadProgram(program, name2.c_str());
program->filename = name;
dynamic_cast<CProgrammableObject*>(object)->RunProgram(program);
}
}