Esempio n. 1
0
bool within_mvt(char fc, int fr, char tc, int tr, int stepc, int stepr, bool multistep, int mvmt)
{
	/*
	fc - from-column A-H
	fr - from-row 1-8
	stepc - mvt per step in column
	stepr - mvt per step in row
	multistep - just move once, or keep moving until finding obstacles
	*/
	bool is_capture = iscol(fc, fr, true) != iscol(tc, tr, true);
	if ((is_capture && !(mvmt&CAPT)) ||
		(!is_capture && !(mvmt&MOVE)))
		return false;

	while ((fc >= 'A' && fc <= 'H' && fr >= 1 && fr <= 8) && (tc >= 'A' && tc <= 'H' && tr >= 1 && tr <= 8))
	{
		fc += stepc;
		fr += stepr;

		if (fc == tc && fr == tr)
		{
			return CBD(tc, tr) == EMP || is_capture;
		}
		if (CBD(fc, fr) != EMP)
		{
			break;
		}

		if (!multistep)
		{
			break;
		}
	}
	return false;
}
Esempio n. 2
0
		static unsigned int Call(Args&&... args) {
			constexpr ScriptCallbackData const& data = CBD(I);
			static_assert(data.callback.matches(TypeString<typename std::remove_reference<Args>::type...>::value), "Wrong number or types of arguments");

			unsigned int count = 0;

			for (auto& script : scripts)
			{
				if (!script->callbacks_.count(I))
					script->callbacks_.emplace(I, script->GetScript<FunctionEllipsis<void>>(data.name));

				auto callback = script->callbacks_[I];

				if (!callback)
					continue;

				if (script->cpp_script)
					reinterpret_cast<FunctionEllipsis<CBR<I>>>(callback)(std::forward<Args>(args)...);
				else
					PAWN::Call(script->amx, data.name, data.callback.types, B, std::forward<Args>(args)...);

				++count;
			}

			return count;
		}
void create_spin_degrees(int direction, int degrees, int speed) {
	speed = (int) (speed * MILLIMETERS_PER_DEGREE);
	create_spin(speed, direction);
	_wait_degrees(direction * degrees);
	create_halt();
	CBD();
}
void create_drive_distance(int direction, float centimeters, float speed) {
	float overshoot = (.1 * speed) - 1;
	int millimeters = (int) ((centimeters - overshoot) * 10);
	create_drive_OI(speed * 10, direction);
	_wait_distance((millimeters * direction) - overshoot);
	create_halt();
	CBD();
}
Esempio n. 5
0
int make_move(char fc, int fr, char tc, int tr)
{
	fc = toupper(fc);
	tc = toupper(tc);
	if (fc<'A' || fc>'H' || tc<'A' || tc>'H' || fr > 8 || fr < 1 || tr>8 || tr < 1)
	{
		return MV_INVALID;
	}
	int mv_type = get_move_type(fc, fr, tc, tr);

	if (mv_type == MT_MOVE)
	{
		CBD(tc, tr) = CBD(fc, fr);
		CBD(fc, fr) = EMP;
		white_turn = !white_turn;
		return MV_OK;
	}
	else if (mv_type == MT_CASTLE_SRT || mv_type == MT_CASTLE_LNG)
	{
		char rook_from = mv_type == MT_CASTLE_LNG ? 'A' : 'H',
			rook_to = mv_type == MT_CASTLE_LNG ? 'D' : 'F';
		int op_row = white_turn ? 1 : 8;
		CBD(tc, tr) = CBD(fc, fr);
		CBD(fc, fr) = EMP;
		//move the rook!
		CBD(rook_to, op_row) = CBD(rook_from, op_row);
		CBD(rook_from, op_row) = EMP;
		if (white_turn)
		{
			white_can_castle_long = white_can_castle_short = false;
		}
		else
		{
			black_can_castle_long = black_can_castle_short = false;
		}
		white_turn = !white_turn;
		return MV_OK;
	}
	return MV_ILLEGAL;

}
Esempio n. 6
0
int get_move_type(char fc, int fr, char tc, int tr)
{
	if ((iscol(fc, fr, true) && iscol(tc, tr, true)) || (iscol(fc, fr, false) && iscol(tc, tr, false)))
	{
		return false;
	}
	if (!iscol(fc, fr, white_turn)) return false;
	switch (toupper(CBD(fc, fr)))
	{
	case 'P':
		//emp check 
		//cap chk
		break;
	case 'N':
		return KNCHK ? MT_MOVE : MT_ILLEGAL;
		break;
	case 'B':
		return BSCHK ? MT_MOVE : MT_ILLEGAL;
		break;
	case 'R':
		return RKCHK ? MT_MOVE : MT_ILLEGAL;
		break;
	case 'Q':
		return QNCHK ? MT_MOVE : MT_ILLEGAL;
		break;
	case 'K':
		if (KGCHK)
			return MT_MOVE;
		if (castle_check(fc, fr, tc, tr, white_turn))
		{
			return tc == 'G' ? MT_CASTLE_SRT : MT_CASTLE_LNG;
		}
		break;
	case ' ':
		return MT_ILLEGAL;
	default:
		return MT_ILLEGAL;
	}
	return MT_MOVE;
}
Esempio n. 7
0
int bamfixmatecoordinates(::libmaus::util::ArgInfo const & arginfo)
{
	::libmaus::util::TempFileRemovalContainer::setup();
	::libmaus::timing::RealTimeClock rtc; rtc.start();
	
	bool const verbose = arginfo.getValue<unsigned int>("verbose",getDefaultVerbose());
	unsigned int const colhashbits = arginfo.getValue<unsigned int>("colhashbits",getDefaultColHashBits());
	unsigned int const collistsize = arginfo.getValue<unsigned int>("collistsize",getDefaultColListSize());
	int const level = arginfo.getValue<int>("level",getDefaultLevel());
	std::string const tmpfilenamebase = arginfo.getValue<std::string>("tmpfile",arginfo.getDefaultTmpFileName());
	
	switch ( level )
	{
		case Z_NO_COMPRESSION:
		case Z_BEST_SPEED:
		case Z_BEST_COMPRESSION:
		case Z_DEFAULT_COMPRESSION:
			break;
		default:
		{
			::libmaus::exception::LibMausException se;
			se.getStream()
				<< "Unknown compression level, please use"
				<< " level=" << Z_DEFAULT_COMPRESSION << " (default) or"
				<< " level=" << Z_BEST_SPEED << " (fast) or"
				<< " level=" << Z_BEST_COMPRESSION << " (best) or"
				<< " level=" << Z_NO_COMPRESSION << " (no compression)" << std::endl;
			se.finish();
			throw se;
		}
			break;
	}


	std::string const tmpfilename = tmpfilenamebase + "_bamcollate";
	::libmaus::util::TempFileRemovalContainer::addTempFile(tmpfilename);
	
	::libmaus::bambam::CollatingBamDecoder CBD(std::cin,tmpfilename,false /* put rank */,colhashbits/*hash bits*/,collistsize/*size of output list*/);
	::libmaus::bambam::BamFormatAuxiliary auxdata;
	::libmaus::bambam::BamHeader const & bamheader = CBD.getHeader();
	
	// add PG line to header
	std::string const upheadtext = ::libmaus::bambam::ProgramHeaderLineSet::addProgramLine(
		bamheader.text,
		"bamfixmatecoordinates", // ID
		"bamfixmatecoordinates", // PN
		arginfo.commandline, // CL
		::libmaus::bambam::ProgramHeaderLineSet(bamheader.text).getLastIdInChain(), // PP
		std::string(PACKAGE_VERSION) // VN			
	);
	// construct new header
	::libmaus::bambam::BamHeader uphead(upheadtext);
	
	if ( uphead.getSortOrder() != "queryname" )
		uphead.changeSortOrder("unknown");

	/*
	 * start index/md5 callbacks
	 */
	std::string const tmpfileindex = tmpfilenamebase + "_index";
	::libmaus::util::TempFileRemovalContainer::addTempFile(tmpfileindex);

	std::string md5filename;
	std::string indexfilename;

	std::vector< ::libmaus::lz::BgzfDeflateOutputCallback * > cbs;
	::libmaus::lz::BgzfDeflateOutputCallbackMD5::unique_ptr_type Pmd5cb;
	if ( arginfo.getValue<unsigned int>("md5",getDefaultMD5()) )
	{
		if ( arginfo.hasArg("md5filename") &&  arginfo.getUnparsedValue("md5filename","") != "" )
			md5filename = arginfo.getUnparsedValue("md5filename","");
		else
			std::cerr << "[V] no filename for md5 given, not creating hash" << std::endl;

		if ( md5filename.size() )
		{
			::libmaus::lz::BgzfDeflateOutputCallbackMD5::unique_ptr_type Tmd5cb(new ::libmaus::lz::BgzfDeflateOutputCallbackMD5);
			Pmd5cb = UNIQUE_PTR_MOVE(Tmd5cb);
			cbs.push_back(Pmd5cb.get());
		}
	}
	libmaus::bambam::BgzfDeflateOutputCallbackBamIndex::unique_ptr_type Pindex;
	if ( arginfo.getValue<unsigned int>("index",getDefaultIndex()) )
	{
		if ( arginfo.hasArg("indexfilename") &&  arginfo.getUnparsedValue("indexfilename","") != "" )
			indexfilename = arginfo.getUnparsedValue("indexfilename","");
		else
			std::cerr << "[V] no filename for index given, not creating index" << std::endl;

		if ( indexfilename.size() )
		{
			libmaus::bambam::BgzfDeflateOutputCallbackBamIndex::unique_ptr_type Tindex(new libmaus::bambam::BgzfDeflateOutputCallbackBamIndex(tmpfileindex));
			Pindex = UNIQUE_PTR_MOVE(Tindex);
			cbs.push_back(Pindex.get());
		}
	}
	std::vector< ::libmaus::lz::BgzfDeflateOutputCallback * > * Pcbs = 0;
	if ( cbs.size() )
		Pcbs = &cbs;
	/*
	 * end md5/index callbacks
	 */
	
	// setup bam writer
	::libmaus::bambam::BamWriter::unique_ptr_type writer(new ::libmaus::bambam::BamWriter(std::cout,uphead,level,Pcbs));
	
	#if 0
	::libmaus::bambam::ProgramHeaderLineSet PHLS(bamheader.text);
	std::cerr << "Last id in PG chain: " << PHLS.getLastIdInChain() << std::endl;
	#endif

	// std::cout << bamheader.text;

	typedef ::libmaus::bambam::CollatingBamDecoder::alignment_ptr_type alignment_ptr_type;
	std::pair<alignment_ptr_type,alignment_ptr_type> P;
	uint64_t const mod = 1024*1024;
	uint64_t proc = 0;
	uint64_t lastproc = 0;
	uint64_t paircnt = 0;
	
	while ( CBD.tryPair(P) )
	{
		uint64_t const mapcnt = getMapCnt(P.first) + getMapCnt(P.second);
		
		if ( mapcnt == 1 )
		{
			int32_t refid = -1;
			int32_t pos = -1;
			
			if ( P.first )
			{
				refid = P.first->getRefID();
				pos = P.first->getPos();
			}
			else
			{
				assert ( P.second );

				refid = P.second->getRefID();
				pos = P.second->getPos();
			}
			
			P.first->putRefId(refid);
			P.first->putPos(pos);
			P.first->putNextRefId(refid);
			P.first->putNextPos(pos);
			P.second->putRefId(refid);
			P.second->putPos(pos);
			P.second->putNextRefId(refid);
			P.second->putNextPos(pos);
		}
		
		if ( P.first )
		{
			P.first->serialise(writer->getStream());
			++proc;
		}
		if ( P.second )
		{
			P.second->serialise(writer->getStream());
			++proc;
		}
		if ( P.first && P.second )
		{
			paircnt++;
		}
		
		if ( verbose && (proc/mod != lastproc/mod) )
		{
			std::cerr 
				<< "Processed " << proc << " fragments, " << paircnt << " pairs, " 
				<< proc/rtc.getElapsedSeconds() << " al/s"
				<< std::endl;
			lastproc = proc;
		}
	}		

	if ( verbose )
		std::cerr 	
			<< "Processed " << proc << " fragments, " << paircnt << " pairs, " 
			<< proc/rtc.getElapsedSeconds() << " al/s"
			<< std::endl;

	writer.reset();

	if ( Pmd5cb )
	{
		Pmd5cb->saveDigestAsFile(md5filename);
	}
	if ( Pindex )
	{
		Pindex->flush(std::string(indexfilename));
	}

	return EXIT_SUCCESS;
}
Esempio n. 8
0
		static constexpr ScriptCallbackData const& CBD(const unsigned int I, const unsigned int N = 0) {
			return callbacks[N].index == I ? callbacks[N] : CBD(I, N + 1);
		}