Пример #1
0
VAttrList VReadFile (FILE *f, VReadFileFilterProc *filter)
{
    VAttrList list;
    long offset;
    int i;

    /* Ensure that the correct FIL_Vista data file header is there: */
    if (! ReadHeader (f))
	return NULL;

    /* Read all attributes in the file: */
    if (! (list = ReadAttrList (f)))
	return NULL;

    /* Swallow the delimiter and read the binary data following it: */
    offset = 0;
    if (! ReadDelimiter (f) || ! ReadData (f, list, filter, &offset)) {
	VDestroyAttrList (list);
	return NULL;
    }

    /* Now we should be at the end of the file: */
    i = fgetc (f);
    if (i != EOF) {
	ungetc (i, f);
	VWarning ("VReadFile: File continues beyond expected EOF");
    }
    return list;
}
Пример #2
0
void CSelectionKeyHandler::LoadSelectionKeys()
{
	std::ifstream ifs(filesystem.LocateFile("selectkeys.txt").c_str());

	selectNumber = 0;

	while (ifs.peek() != EOF && !ifs.eof()) {
		std::string key, sel;
		ifs >> key;

		if (ifs.peek() == EOF || ifs.eof())
			break;

		ifs >> sel;

		bool shift=false;
		bool control=false;
		bool alt=false;
		unsigned char keyname=0;

		while(true){
			std::string s=ReadToken(key);

			if(s=="Shift"){
				shift=true;
			} else if (s=="Control"){
				control=true;
			} else if (s=="Alt"){
				alt=true;
			} else {
				char c=s[0];
				if(c>='A' && c<='Z')
					keyname=SDLK_a + (c - 'A');

				if(c>='0' && c<='9')
					keyname=SDLK_0 + (c -'0');

				break;
			}
			ReadDelimiter(key);
		}
		std::string keybindstring;
		if ( alt ) keybindstring += "Alt";
		if ( control ) {
			if ( keybindstring.size() != 0 ) keybindstring += "+";
			keybindstring += "Ctrl";
		}
		if ( shift ) {
			if ( keybindstring.size() != 0 ) keybindstring += "+";
			keybindstring += "Shift";
		}
		if ( keybindstring.size() != 0 ) keybindstring += "+";
		keybindstring += keyname;
		keyBindings->Command( "bind " + keybindstring + " select " + sel );
	}
}
Пример #3
0
EXPORT_VISTA VistaIOAttrList VistaIOReadFile (FILE * f, VistaIOReadFileFilterProc * filter)
{
	VistaIOAttrList list;
	int i;
 	ReadStringBuf sbuf = {0,0}; 
	
#ifdef SupportUbcIff

	/* If the first byte of the file is "I", it looks like a UBC IFF file: */
	{
		i = fgetc (f);
		ungetc (i, f);
		if (i == 'I') {
			VistaIOImage image = VistaIOReadUbcIff (f);

			if (!image)
				return FALSE;
			list = VistaIOCreateAttrList ();
			VistaIOSetAttr (list, "image", NULL, VistaIOImageRepn, image);
			return list;
		}
	}

#endif

	/* Ensure that the correct Vista data file header is there: */
	if (!ReadHeader (f))
		return NULL;

	list = ReadAttrList (f, &sbuf); 
	if (sbuf.max_len)
		VistaIOFree(sbuf.buf); 
	/* Read all attributes in the file: */
	if (!list)
		return NULL;
	
	/* Swallow the delimiter and read the binary data following it: */
	offset = 0;
	if (!ReadDelimiter (f) || !ReadData (f, list, filter)) {
		VistaIODestroyAttrList (list);
		return NULL;
	}

	/* Now we should be at the end of the file: */
	i = fgetc (f);
	if (i != EOF) {
		ungetc (i, f);
		VistaIOWarning ("VistaIOReadFile: File continues beyond expected EOF");
	}
	return list;
}
CSelectionKeyHandler::CSelectionKeyHandler(void)
{
	selectNumber=0;
	boost::filesystem::path fn("selectkeys.txt");
	std::ifstream ifs(fn.native_file_string().c_str());

	char buf[10000];

	while(ifs.peek()!=EOF && !ifs.eof()){
		ifs >> buf;
		string key(buf);

		if(ifs.peek()==EOF || ifs.eof())
			break;

		ifs >> buf;
		string sel(buf);

		HotKey hk;

		hk.select=sel;
		hk.shift=false;
		hk.control=false;
		hk.alt=false;

		while(true){
			string s=ReadToken(key);

			if(s=="Shift"){
				hk.shift=true;
			} else if (s=="Control"){
				hk.control=true;
			} else if (s=="Alt"){
				hk.alt=true;
			} else {
				char c=s[0];
				if(c>='A' && c<='Z')
					hk.key=SDLK_a + (c - 'A');

				if(c>='0' && c<='9')
					hk.key=SDLK_0 + (c -'0');

				break;
			}
			ReadDelimiter(key);
		}

		hotkeys.push_back(hk);
	}
}
void CSelectionKeyHandler::DoSelection(string selectString)
{
	list<CUnit*> selection;

//	guicontroller->AddText(selectString.c_str());
	string s=ReadToken(selectString);

	if(s=="AllMap"){
		set<CUnit*>* tu=&gs->Team(gu->myTeam)->units;
		for(set<CUnit*>::iterator ui=tu->begin();ui!=tu->end();++ui){
			selection.push_back(*ui);
		}
	} else if(s=="Visible"){
		set<CUnit*>* tu=&gs->Team(gu->myTeam)->units;
		for(set<CUnit*>::iterator ui=tu->begin();ui!=tu->end();++ui){
			if(camera->InView((*ui)->midPos,(*ui)->radius))
				selection.push_back(*ui);
		}

	} else if(s=="FromMouse"){
		ReadDelimiter(selectString);
		float maxDist=atof(ReadToken(selectString).c_str());

		float dist=ground->LineGroundCol(camera->pos,camera->pos+mouse->dir*8000);
		float3 mp=camera->pos+mouse->dir*dist;

		set<CUnit*>* tu=&gs->Team(gu->myTeam)->units;
		for(set<CUnit*>::iterator ui=tu->begin();ui!=tu->end();++ui){
			if(mp.distance((*ui)->pos)<maxDist)
				selection.push_back(*ui);
		}
	} else if(s=="PrevSelection"){
		set<CUnit*>* tu=&selectedUnits.selectedUnits;
		for(set<CUnit*>::iterator ui=tu->begin();ui!=tu->end();++ui){
			selection.push_back(*ui);
		}
	} else {
		info->AddLine("Unknown source token %s",s.c_str());
		return;
	}

	ReadDelimiter(selectString);

	while(true){
		string s=ReadDelimiter(selectString);
		if(s=="+")
			break;

		s=ReadToken(selectString);

		bool _not=false;

		if(s=="Not"){
			_not=true;
			ReadDelimiter(selectString);
			s=ReadToken(selectString);
		}

		if(s=="Builder"){
			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if((*ui)->unitDef->buildSpeed>0){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="Building"){
			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if(dynamic_cast<CBuilding*>(*ui)){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="Commander"){
			unsigned int comCat=CCategoryHandler::Instance()->GetCategory("COMMANDER");

			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if((*ui)->category & comCat){	//fix with better test for commander
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="Transport"){
			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if((*ui)->unitDef->transportCapacity>0){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="Aircraft"){
			unsigned int acCat=CCategoryHandler::Instance()->GetCategory("VTOL");

			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if((*ui)->category & acCat){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="Weapons"){
			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if(!(*ui)->weapons.empty()){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="WeaponRange"){
			ReadDelimiter(selectString);
			float minRange=atof(ReadToken(selectString).c_str());

			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if((*ui)->maxRange>minRange){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="AbsoluteHealth"){
			ReadDelimiter(selectString);
			float minHealth=atof(ReadToken(selectString).c_str());

			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if((*ui)->health>minHealth){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="RelativeHealth"){
			ReadDelimiter(selectString);
			float minHealth=atof(ReadToken(selectString).c_str())*0.01;//convert from percent

			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if((*ui)->health/(*ui)->maxHealth > minHealth){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="InPrevSel"){
			set<int> prevTypes;
			set<CUnit*>* tu=&selectedUnits.selectedUnits;
			for(set<CUnit*>::iterator si=tu->begin();si!=tu->end();++si){
				prevTypes.insert((*si)->aihint);
			}
			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if(prevTypes.find((*ui)->aihint)!=prevTypes.end()){		//should move away from aihint
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="NameContain"){
			ReadDelimiter(selectString);
			string name=ReadToken(selectString);

			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if((*ui)->unitDef->humanName.find(name)!=string::npos){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="Idle"){
			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if((*ui)->commandAI->commandQue.empty()){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="Radar"){
			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if((*ui)->radarRadius || (*ui)->sonarRadius || (*ui)->jammerRadius){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}
		} else if(s=="Category"){
			ReadDelimiter(selectString);
			string catname=ReadToken(selectString);
			unsigned int cat=CCategoryHandler::Instance()->GetCategory(catname);

			list<CUnit*>::iterator ui=selection.begin();
			while(ui!=selection.end()){
				bool filterTrue=false;
				if((*ui)->category==cat){
					filterTrue=true;
				}
				if(filterTrue ^ _not){
					++ui;
				} else {
					list<CUnit*>::iterator prev=ui++;
					selection.erase(prev);
				}
			}

		} else {
			info->AddLine("Unknown token in filter %s",s.c_str());
			return;
		}
	}

	ReadDelimiter(selectString);
	s=ReadToken(selectString);

	if(s=="ClearSelection"){
		selectedUnits.ClearSelected();

		ReadDelimiter(selectString);
		s=ReadToken(selectString);
	}
		
	if(s=="SelectAll"){
		for(list<CUnit*>::iterator ui=selection.begin();ui!=selection.end();++ui)
			selectedUnits.AddUnit(*ui);
	} else if(s=="SelectOne"){
		if(selection.empty())
			return;
		if(++selectNumber>=selection.size())
			selectNumber=0;

		CUnit* sel;
		int a=0;
		for(list<CUnit*>::iterator ui=selection.begin();ui!=selection.end() && a<=selectNumber;++ui,++a)
			sel=*ui;

		selectedUnits.AddUnit(sel);
		mouse->inStateTransit=true;
		mouse->transitSpeed=0.7;
		if(mouse->currentCamController!=mouse->camControllers[0]){
			mouse->currentCamController->SetPos(sel->pos);
		} else {	//fps camera
			
			if(camera->rot.x>-1)
				camera->rot.x=-1;

			float3 wantedCamDir;
			wantedCamDir.x=(float)(sin(camera->rot.y)*cos(camera->rot.x));
			wantedCamDir.y=(float)(sin(camera->rot.x));
			wantedCamDir.z=(float)(cos(camera->rot.y)*cos(camera->rot.x));
			wantedCamDir.Normalize();

			((CFPSController*)mouse->camControllers[0])->pos=sel->pos - wantedCamDir*800;
		}
	} else if(s=="SelectNum"){
		ReadDelimiter(selectString);
		int num=atoi(ReadToken(selectString).c_str());

		if(selection.empty())
			return;

		if(selectNumber>=selection.size())
			selectNumber=0;

		list<CUnit*>::iterator ui=selection.begin();
		for(int a=0;a<selectNumber;++a)
			++ui;
		for(int a=0;a<num;++ui,++a){
			if(ui==selection.end())
				ui=selection.begin();
			selectedUnits.AddUnit(*ui);
		}

		selectNumber+=num;
	} else if(s=="SelectPart"){
		ReadDelimiter(selectString);
		float part=atof(ReadToken(selectString).c_str())*0.01;//convert from percent
		int num=(int)(selection.size()*part);

		if(selection.empty())
			return;

		if(selectNumber>=selection.size())
			selectNumber=0;

		list<CUnit*>::iterator ui=selection.begin();
		for(int a=0;a<selectNumber;++a)
			++ui;
		for(int a=0;a<num;++ui,++a){
			if(ui==selection.end())
				ui=selection.begin();
			selectedUnits.AddUnit(*ui);
		}

		selectNumber+=num;
	} else {
		info->AddLine("Unknown token in conclusion %s",s.c_str());
		return;
	}
}