Example #1
0
BOOL C4Shape::LineConnect(int32_t tx, int32_t ty, int32_t cvtx, int32_t ld,
                          int32_t oldx, int32_t oldy) {
#ifdef C4ENGINE

  if (VtxNum < 2) return FALSE;

  // No modification
  if ((VtxX[cvtx] == tx) && (VtxY[cvtx] == ty)) return TRUE;

  // Check new path
  int32_t ix, iy;
  if (PathFree(tx, ty, VtxX[cvtx + ld], VtxY[cvtx + ld], &ix, &iy)) {
    // Okay, set vertex
    VtxX[cvtx] = tx;
    VtxY[cvtx] = ty;
    return TRUE;
  } else {
    // Intersected, find bend vertex
    bool found = false;
    int32_t cix;
    int32_t ciy;
    for (int irange = 4; irange <= 12; irange += 4)
      for (cix = ix - irange / 2; cix <= ix + irange; cix += irange)
        for (ciy = iy - irange / 2; ciy <= iy + irange; ciy += irange) {
          if (PathFree(cix, ciy, tx, ty) &&
              PathFree(cix, ciy, VtxX[cvtx + ld], VtxY[cvtx + ld])) {
            found = true;
            goto out;
          }
        }
  out:
    if (!found) {
      // try bending directly at path the line took
      // allow going through vehicle in this case to allow lines through castles
      // and elevator shafts
      cix = oldx;
      ciy = oldy;
      if (!PathFreeIgnoreVehicle(cix, ciy, tx, ty) ||
          !PathFreeIgnoreVehicle(cix, ciy, VtxX[cvtx + ld], VtxY[cvtx + ld]))
        if (!PathFreeIgnoreVehicle(cix, ciy, tx, ty) ||
            !PathFreeIgnoreVehicle(cix, ciy, VtxX[cvtx + ld], VtxY[cvtx + ld]))
          return FALSE;  // Found no bend vertex
    }
    // Insert bend vertex
    if (ld > 0) {
      if (!InsertVertex(cvtx + 1, cix, ciy)) return FALSE;
    } else {
      if (!InsertVertex(cvtx, cix, ciy)) return FALSE;
      cvtx++;
    }
    // Okay, set vertex
    VtxX[cvtx] = tx;
    VtxY[cvtx] = ty;
    return TRUE;
  }
#endif

  return FALSE;
}
Example #2
0
/*--------------------------------------------------------------------*/
static int convert_single_path_to_label (char* fname, char* ofname) {
  int     err;
  int     num_paths;
  PATH**  paths       = NULL;
  LABEL*  label       = NULL;
  int     path_index;

  /* Read the paths file. */
  err = PathReadMany (fname, &num_paths, &paths);
  if (ERROR_NONE != err) {
    ErrorReturn (ERROR_BADFILE,
                 (ERROR_BADFILE, "Couldn't read %s", fname));
  }

  /* Warn if we have more than one path. */
  if (num_paths != 1) {
    printf ("WARNING: Found multiple paths in label file.\n"
	    "Maybe you didn't mean to use the single option?\n"
	    "Converting it to a single path.\n");
  }

  /* Convert our first path. */
  err = PathConvertToLabel (paths[0], &label);
  if (ERROR_NONE != err) {
    for (path_index = 0; path_index < num_paths; path_index++)
      PathFree (&paths[path_index]);
    free (paths);
    return err;
  }

  /* Delete all the paths. */
  for (path_index = 0; path_index < num_paths; path_index++) {
    PathFree (&paths[path_index]);
  }

  /* Free our paths variable. */
  free (paths);

  /* Write the label file. */
  LabelWrite (label, ofname);

  /* Free the label. */
  LabelFree (&label);

  return (ERROR_NONE);
}
Example #3
0
static int convert_single_label_to_path (char* fname, char* ofname) {
  LABEL* label           = NULL;
  int    label_vno;
  int    num_paths;
  PATH** paths           = NULL;
  int    err;

  /* Read the label file. */
  label = LabelRead (NULL, fname);
  if (NULL == label) {
    ErrorReturn (ERROR_BADFILE,
                 (ERROR_BADFILE, "Couldn't read %s", fname));
  }

  /* Count the number of sentinels, -99999 vnos in the label; we only
     want one path, so this is just to check if this is a
     multiple-path label file. */
  num_paths = 0;
  for (label_vno = 0; label_vno < label->n_points; label_vno++) {
    if (-99999 == label->lv[label_vno].vno)
      num_paths++;
  }

  /* Make sure we only got one paths. */
  if (num_paths > 1) {
    printf ("WARNING: Found multiple paths in label file.\n"
	    "Maybe you didn't mean to use the single option?\n"
	    "Converting it to a single path.\n");
  }

  /* Allocate path objects. */
  paths = (PATH**) calloc (1, sizeof(PATH*));
  if (NULL == paths) {
    ErrorReturn (ERROR_NO_MEMORY,
                 (ERROR_NO_MEMORY, "Couldn't allocate %d paths", 1));
  }

  err = PathCreateFromLabel (label, &paths[0]);
  if (ERROR_NONE != err) {
    free (paths);
    return err;
  }

  /* Write the path file. */
  err = PathWriteMany (ofname, 1, paths);
  if (0 != err) {
    ErrorReturn (ERROR_BADFILE,
                 (ERROR_BADFILE, "Couldn't write to %s", ofname));
  }

  PathFree (&paths[0]);
  free (paths);

  return (ERROR_NONE);
}
Example #4
0
func Timer()
{
	var pDead2, pNewTarget;
	if(!pTarget)
	{
	for(var pDead in FindObjects(Find_Action("Dead"), Find_Distance(100)))
	{
		if(GetPhase(pDead) < 4)
			if(PathFree(GetX(), GetY(), pDead->GetX(), pDead->GetY()))
			{
				pDead2 = pDead;
				break;
			}
	}
	pDead = pDead2;
	if(pDead)
	{
		for(pNewTarget in FindObjects(Find_OCF(OCF_Alive), Find_Distance(100), Find_Hostile(GetOwner(pDead))))
		{
			if(PathFree(GetX(), GetY(), pNewTarget->GetX(), pNewTarget->GetY()))
			{
				iTimer = 0;
				pTarget = pNewTarget;
			}
		}
	}
	}
	if(pTarget)
	{
		iTimer++;
		if(iTimer%10 != 0) return;
		if(iTimer > 100) pTarget = 0;
		else if(!PathFree(GetX(), GetY(), pTarget->GetX(), pTarget->GetY()) && !pTarget->Contained()) pTarget = 0;
		if(!pTarget) return;
		Message("Töte niemanden an diesem heiligen Ort!", this);
		DrawLightning(GetX(),GetY(),pTarget->GetX(), pTarget->GetY());
		Punch(pTarget, 5);
	}
}
Example #5
0
global func CyclopsFindTarget(fx)
{
	// Consider hostile clonks, or all clonks if the AI does not have an owner.
	var hostile_criteria = Find_Hostile(fx.cyclops->GetOwner());
	if (fx.cyclops->GetOwner() == NO_OWNER)
		hostile_criteria = Find_Not(Find_Owner(fx.cyclops->GetOwner()));

	for (var target in FindObjects(Find_InRect(fx.guard_range.x,fx.guard_range.y,fx.guard_range.wdt,fx.guard_range.hgt), Find_OCF(OCF_CrewMember), hostile_criteria, Find_NoContainer(), Sort_Random()))
		if (PathFree(fx.cyclops->GetX(),fx.cyclops->GetY(),target->GetX(),target->GetY()))
			return target;

	// Nothing found.
	return;
}
Example #6
0
bool CPathFinderRay::CheckBackRayShorten()
{
	CPathFinderRay *pRay,*pRay2;
	int iX,iY;
	for (pRay=From; pRay; pRay=pRay->From)
	{
		if (pRay==From) continue;
		iX=X; iY=Y;
		if (PathFree(iX,iY,pRay->X,pRay->Y))
		{
			// Delete jumped rays
			for (pRay2=From; pRay2!=pRay; pRay2=pRay2->From)
				pRay2->Status=PF_Ray_Deleted;
			// Shorten pRay to this
			pRay->X2=X; pRay->Y2=Y;
			From=pRay;
			// Success
			return true;
		}
	}
	return false;
}
Example #7
0
extern inline Http *ProtocolHttp( Socket* sock, char* data, unsigned int length )
{
	Http *response = NULL;
	DEBUG("HTTP Callback called\n");
	
	if( length <= 0 )
	{
		DEBUG("RESULT<0 http400\n");
		
		struct TagItem tags[] = {
			{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicateN( "close", 5 ) },
			{TAG_DONE, TAG_DONE}
		};
		
		response = HttpNewSimple( HTTP_400_BAD_REQUEST,  tags );
	
		//HttpWriteAndFree( response );
	}
	
	// Get the current request we're working on, or start a new one
	Http* request = (Http*)sock->data;
	if( !request )
	{
		request = HttpNew( sock );
		request->timestamp = time( NULL );
		sock->data = (void*)request;
	}
	
	//DEBUG("Checking timeout, data %s\n", data );

	//DEBUG("time %ld\nreqtimestamp %ld\nreqtimestamp %ld\n",
	//	  time( NULL ), request->timestamp, HTTP_REQUEST_TIMEOUT );
	// Timeout
	if( time( NULL ) > request->timestamp + HTTP_REQUEST_TIMEOUT )
	{
		struct TagItem tags[] = {
			{ HTTP_HEADER_CONTENT_TYPE, (ULONG)  StringDuplicate( "text/plain" ) },
			{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
			{TAG_DONE, TAG_DONE}
		};
		
		response = HttpNewSimple( HTTP_408_REQUEST_TIME_OUT,  tags );
		
		HttpAddTextContent( response, "408 Request Timeout\n" );
		//HttpWriteAndFree( response );
		HttpFreeRequest( request );
		sock->data = NULL;
		DEBUG("HTTP TIMER\n");
		return response;
	}
	

	// Continue parsing the request
	int result = HttpParsePartialRequest( request, data, length );
	
	// Protocol error
	if( result < 0 )
	{
		DEBUG("RESULT<0 http400\n");
		struct TagItem tags[] = {
			{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
			{TAG_DONE, TAG_DONE}
		};
		
		response = HttpNewSimple( HTTP_400_BAD_REQUEST,  tags );
	
		//HttpWriteAndFree( response );
	}
	// Request not fully parsed yet. Return and wait for more data
	else if( result == -1 )
	{
		DEBUG( " <- (%d): Waiting for more data\n", sock->fd );
		HttpFreeRequest( request );
		return response;
	}
	// Request parsed without errors!
	else if( result == 1 )
	{
		Uri* uri = request->uri;
		
		// Disallow proxy requests
		if( uri && ( uri->scheme || uri->authority ) )
		{
			struct TagItem tags[] = {
				{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
				{TAG_DONE, TAG_DONE}
			};
		
			response = HttpNewSimple( HTTP_403_FORBIDDEN,  tags );
	
			result = 403;
		}

		// Cross-domain requests uses a pre-flight OPTIONS call
		if( !request->errorCode && request->method && strcmp( request->method, "OPTIONS" ) == 0 )
		{
			struct TagItem tags[] = {
				{ HTTP_HEADER_CONTROL_ALLOW_ORIGIN, (ULONG)StringDuplicateN( "*", 1 ) },
				{ HTTP_HEADER_CONTROL_ALLOW_HEADERS, (ULONG)StringDuplicateN( "Origin, X-Requested-With, Content-Type, Accept, Method", 54 ) },
				{ HTTP_HEADER_CONTROL_ALLOW_METHODS,  (ULONG)StringDuplicateN( "GET, POST, OPTIONS", 18 ) },
				{ HTTP_HEADER_CONNECTION, (ULONG)StringDuplicateN( "close", 5 ) },
				{TAG_DONE, TAG_DONE}
			};
		
			if( response != NULL ) ERROR("Response != NULL\n");
			response = HttpNewSimple( HTTP_200_OK,  tags );

			result = 200;
		}		
		// Check for connection upgrade
		else if( !request->errorCode && HttpHeaderContains( request, "connection", "Upgrade", false ) )
		{
			struct TagItem tags[] = {
				{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
				{TAG_DONE, TAG_DONE}
			};
		
			response = HttpNewSimple(  HTTP_400_BAD_REQUEST, tags );
	
		}
		else
		{
			Path* path = NULL;
			if( uri->path->raw )
			{
				int nlen = 0;
				for( ; ; nlen++ )
				{
					if( !uri->path->raw[nlen] )
						break;
				}
				DEBUG("Want to parse path: %s (%d)\n", uri->path->raw, nlen );
				path = PathNew( uri->path->raw );
				if( path ) PathResolve( path );  // Resolve checks for "../"'s, and removes as many as it can.
			}
			if( !path || !path->resolved ) // If it cannot remove all, path->resolved == false.
			{
				DEBUG( "We have no path..\n" );
				struct TagItem tags[] = {
					{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
					{TAG_DONE, TAG_DONE}
				};
		
				response = HttpNewSimple( HTTP_403_FORBIDDEN,  tags );
	
				result = 403;
			}
			else
			{
				DEBUG( "We got through. %s\n", path->parts[ 0 ] );
				
				if( path->size >= 2 && StringCheckExtension( path->parts[0], "library" ) == 0 )
				{
					// system.library is main library and should be use for most things
					// we open it and close in main
					//DEBUG("systemlib found\n");
					DEBUG("Calling systemlib\n");
					
					if( strcmp( path->parts[ 0 ], "system.library" ) == 0 )
					{
						DEBUG( "%s\n", path->parts[1] );
						response = SLIB->SysWebRequest( SLIB, &(path->parts[1]), request );
						
						if( response == NULL )
						{
							struct TagItem tags[] = {
								{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
								{TAG_DONE, TAG_DONE}
							};	
		
							response = HttpNewSimple(  HTTP_500_INTERNAL_SERVER_ERROR,  tags );
	
							result = 500;
						}
					}
					else
					{
						FriendCoreInstance_t *fci = (FriendCoreInstance_t *) sock->s_Data;
						Library* lib = FriendCoreGetLibrary( fci, path->parts[0], 1 );
						if( lib && lib->WebRequest )
						{
							response =(Http *) lib->WebRequest( lib, path->parts[1], request );
							if( response == NULL )
							{
								struct TagItem tags[] = {
									{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
									{TAG_DONE, TAG_DONE}
								};	
		
								response = HttpNewSimple( HTTP_500_INTERNAL_SERVER_ERROR,  tags );
	
								result = 500;
							}
						}
						else
						{
							struct TagItem tags[] = {
								{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
								{TAG_DONE, TAG_DONE}
							};	
		
							response = HttpNewSimple( HTTP_404_NOT_FOUND,  tags );
	
							result = 404;
						}
					}
				}
				// We're calling on a static file.
				else
				{
					//DEBUG("Getting resources\n");
					// Read the file
					Path *base = PathNew( "resources" );
					Path* complete = PathJoin( base, path );
					BOOL freeFile = FALSE;
					
					LocFile* file = CacheManagerFileGet( SLIB->cm, complete->raw );
					if( file == NULL )
					{
						file = LocFileNew( complete->raw, FILE_READ_NOW | FILE_CACHEABLE );
						if( file != NULL )
						{
							if( CacheManagerFilePut( SLIB->cm, file ) != 0 )
							{
								freeFile = TRUE;
							}
						}
					}

					// Send reply
					if( file != NULL )
					{
						char* mime = NULL;
						
						if(  file->buffer == NULL )
						{
							ERROR("File is empty %s\n", complete->raw );
						}

						if( complete->extension )
						{
							mime = StringDuplicate( MimeFromExtension( complete->extension ) );
						}
						else
						{
							mime = StringDuplicate( "text/plain" );
						}
						
						struct TagItem tags[] = {
							{ HTTP_HEADER_CONTENT_TYPE, (ULONG)  mime },
							{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
							{TAG_DONE, TAG_DONE}
						};
		
						response = HttpNewSimple( HTTP_200_OK, tags );

						
						//DEBUG("Before returning data\n");
						
						HttpSetContent( response, file->buffer, file->bufferSize );
						
						// write here and set data to NULL!!!!!
						// retusn response
						HttpWrite( response, sock );
						result = 200;
						
						INFO("--------------------------------------------------------------%d\n", freeFile );
						if( freeFile == TRUE )
						{
							//ERROR("\n\n\n\nFREEEEEEFILE\n");
							LocFileFree( file );
						}
						response->content = NULL;
						response->sizeOfContent = 0;
						
						response->h_WriteType = FREE_ONLY;
					}
					else
					{
						DEBUG( "[ProtocolHttp] Going ahead with %s.\n", path->parts ? path->parts[0] : "No path part.." );
						
						// Try to fall back on module
						// TODO: Make this behaviour configurable
						char command[255];
						sprintf( command, "php \"php/catch_all.php\" \"%s\";", uri->path->raw ); 
						DEBUG( "[ProtocolHttp] Executing %s\n", command );
						ListString *bs = RunPHPScript( command );
						int phpRun = FALSE;
						if( bs )
						{
							if( bs->ls_Size > 0 )
							{
								struct TagItem tags[] = {
									{ HTTP_HEADER_CONTENT_TYPE, (ULONG)  StringDuplicate( "text/html" ) },
									{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
									{TAG_DONE, TAG_DONE}
								};
		
								response = HttpNewSimple(  HTTP_200_OK,  tags );
						
								HttpSetContent( response, bs->ls_Data, bs->ls_Size );
								
								result = 200;
								phpRun = TRUE;
								bs->ls_Data = NULL;
							}
							ListStringDelete( bs );
						}
						
						if( !phpRun )
						{
							DEBUG("File do not exist\n");
							
							struct TagItem tags[] = {
								{	HTTP_HEADER_CONNECTION, (ULONG)StringDuplicate( "close" ) },
								{TAG_DONE, TAG_DONE}
							};	
		
							response = HttpNewSimple( HTTP_404_NOT_FOUND,  tags );
	
							result = 404;
						}
					}
					
					PathFree( base );
					PathFree( complete );
				}
			}
			PathFree( path );
		}

		// SPRING CLEANING!!! TIME TO CLEAN THE CASTLE!!! :) :) :)
		HttpFreeRequest( request );
		
		if( result != 101 )
		{
			sock->data = NULL;
		}
		return response;
	}
	// Winter cleaning
	HttpFreeRequest( request );
	return response;
}
Example #8
0
global func CyclopsExecuteMelee(fx, int timer)
{
	// Still carrying the melee weapon?
	if (fx.weapon->Contained() != fx.cyclops)
	{
		fx.weapon=nil;
		return false;
	}
	
	if (GetActiveSequence())
	{
		fx.spraying = 0;
		fx.spraying_charge = 0;
		return;
	}

	// Are we in range?
	var x=fx.cyclops->GetX();
	var y=fx.cyclops->GetY();
	var tx=fx.target->GetX();
	var ty=fx.target->GetY();
	var dx = tx-x, dy = ty-y;

	if (tx > x)
	{
		fx.cyclops->SetDir(DIR_Right);
	}
	else
	{
		fx.cyclops->SetDir(DIR_Left);
	}

	// the cyclops stands in its place and hits objects or breathes fire
	if (PathFree(x,y,tx,ty))
	{

		// hit objects
		if (Abs(dx) <= 35 && dy >= -15)
		{
			// OK, slash!
			if (fx.cyclops->IsAiming())
			{
				fx.weapon->ControlUseHolding(fx.cyclops, dx, dy);
				fx.weapon->ControlUseStop(fx.cyclops, dx, dy);
				return;
			}

			// target is under us - sword slash downwards!
			if (!CyclopsCheckHandsAction(fx)) return true;
			// Stop here
			fx.cyclops->SetCommand("None");
			fx.cyclops->SetComDir(COMD_None);
			// cooldown?
			if (!fx.weapon->CanStrikeWithWeapon(fx.cyclops))
			{
				return true;
			}

			// OK, slash!
			if (!fx.cyclops->IsAiming())
				fx.weapon->ControlUseStart(fx.cyclops, dx, dy);
			
			// fire breath cooldown
			fx.spraying = CYCLOPS_FireBreath_Duration;
			
			return;
		}
		
			DoFireBreath(fx, x ,y , tx, ty, timer);
	}
	
	return true;
}
Example #9
0
void UriFree( Uri* uri )
{
	if( !uri )
		return;

	if( uri->scheme )
	{
		free( uri->scheme );
		uri->scheme = NULL;
	}

	if( uri->authority )
	{
		if( uri->authority->user )
			free( uri->authority->user );
		uri->authority->user = NULL;

		if( uri->authority->host )
			free( uri->authority->host );
		uri->authority->host = NULL;

		free( uri->authority );
		uri->authority = NULL;
	}

	if( uri->path )
	{
		PathFree( uri->path );
		uri->path = NULL;
	}

	if( uri->query )
	{
		unsigned int iterator = 0;
		HashmapElement* e = NULL;
		while( ( e = HashmapIterate( uri->query, &iterator ) ) != NULL )
		{
			if( e->data != NULL )
				free( e->data );
			e->data = NULL;
			free( e->key );
			e->key = NULL;
		}
		HashmapFree( uri->query );
		uri->query = NULL;
	}

	if( uri->queryRaw )
	{
		free( uri->queryRaw );
		uri->queryRaw = NULL;
	}
	
	if( uri->fragment )
	{
		free( uri->fragment );
		uri->fragment = NULL;
	}

	free( uri );
}
Example #10
0
bool CPathFinderRay::Execute()
{
	CPathFinderRay *pRay;
	int iX,iY,iLastX,iLastY;
	switch (Status)
	{
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	case PF_Ray_Launch:
		// Path free
		if (PathFree(X2,Y2,TargetX,TargetY))
		{
			// Back shorten
			for (pRay=this; pRay->From; pRay=pRay->From)
				while (pRay->CheckBackRayShorten()) {}
			// Set all waypoints
			for (pRay=this; pRay->From; pRay=pRay->From)
				pPathFinder->SetWaypoint(pRay->From->X2,pRay->From->Y2,pPathFinder->WaypointParameter);
			// Success
			pPathFinder->Success=true;
			Status=PF_Ray_Still;
			return true;
		}
		// Path intersected
		else
		{
			// Start crawling
			Status=PF_Ray_Crawl;
			CrawlStartX=X2; CrawlStartY=Y2;
			CrawlAttach=FindCrawlAttach(X2,Y2);
			CrawlLength=0;
			if (!CrawlAttach) CrawlAttach=FindCrawlAttachDiagonal(X2,Y2,Direction);
			CrawlStartAttach=CrawlAttach;
			// Intersected but no attach found: unexpected failure
			if (!CrawlAttach)
				Status=PF_Ray_Failure;
			return true;
		}
		break;
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	case PF_Ray_Crawl:
		// Crawl
		iLastX=X2; iLastY=Y2;
		if (!Crawl())
			{ Status=PF_Ray_Failure; break; }
		// Back at crawl starting position: done and still
		if ((X2==CrawlStartX) && (Y2==CrawlStartY) && (CrawlAttach==CrawlStartAttach))
			{ Status=PF_Ray_Still; break; }
		// Crawl length
		CrawlLength++;
		if (CrawlLength>=pPathFinder->MaxCrawl)
			{ Status=PF_Ray_Still; break; }
		// Check back path intersection
		iX=X; iY=Y;
		if (!PathFree(iX,iY,X2,Y2))
			// Insert split ray
			if (!pPathFinder->SplitRay(this,iLastX,iLastY))
				{ Status=PF_Ray_Failure; break; }
		// Try new ray at target
		iX=X2; iY=Y2;
		// If has been crawling for a while
		if (CrawlLength>pPathFinder->Threshold)
			// If all free...
			if ( PathFree(iX,iY,TargetX,TargetY)
			     // ...or at least beyond threshold and not backwards toward crawl start
			     || ((Distance(iX,iY,X2,Y2)>pPathFinder->Threshold) && (Distance(iX,iY,CrawlStartX,CrawlStartY)>Distance(X2,Y2,CrawlStartX,CrawlStartY))) )
			{
				// Still
				Status=PF_Ray_Still;
				// Launch new rays
				if (!pPathFinder->AddRay(X2,Y2,TargetX,TargetY,Depth+1,PF_Direction_Left,this)
				    || !pPathFinder->AddRay(X2,Y2,TargetX,TargetY,Depth+1,PF_Direction_Right,this))
					Status=PF_Ray_Failure;
			}
		break;
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	case PF_Ray_Still: case PF_Ray_Failure: case PF_Ray_Deleted:
		return false;
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	}
	return true;
}
Example #11
0
static int fill_pathx(char* fname, 
                      char* ofname, 
                      char* surfaceFname, 
                      int seed) 
{
  int     err;
  int     num_paths;
  PATH **paths = NULL;
  LABEL *label;
  int  k, nlabel, nth;
  MRIS *mris;

  /* Read the paths file. */
  err = PathReadMany (fname, &num_paths, &paths);
  if (ERROR_NONE != err) {
    ErrorReturn (ERROR_BADFILE,
                 (ERROR_BADFILE, "Couldn't read %s", fname));
  }

  /* Warn if we have more than one path. */
  if (num_paths != 1) {
    printf ("WARNING: Found multiple paths in paths file. \n"
	    "Maybe you didn't mean to use the connect option?\n"
	    "Will only convert first path\n\n");
  }

  printf("Reading %s\n",surfaceFname);
  mris = MRISread(surfaceFname);
  if(mris == NULL) exit(1);

  // Make sure vals are 0
  for(k=0; k < mris->nvertices; k++) mris->vertices[k].val = 0;

  for(k=0; k < paths[0]->n_points; k++)
    mris->vertices[paths[0]->points[k].vno].val = 1;

  printf("Filling %d\n",seed);
  MRISfill(mris, seed);

  nlabel = 0;
  for(k=0; k < mris->nvertices; k++) 
    if(mris->vertices[k].val > 0.5) nlabel++;

  printf("nlabel %d\n",nlabel);
  label = LabelAlloc(nlabel, subject, "");
  label->n_points = nlabel;
  nth = 0;
  for(k=0; k < mris->nvertices; k++){
    if(mris->vertices[k].val < 0.5) continue;
    label->lv[nth].vno = k;
    label->lv[nth].x = mris->vertices[k].x;
    label->lv[nth].y = mris->vertices[k].y;
    label->lv[nth].z = mris->vertices[k].z;
    label->lv[nth].stat = 0;
    nth ++;
  }
  printf("Saving label file %s\n",ofname);
  LabelWrite(label, ofname);

  PathFree(&paths[0]);
  free (paths);
  MRISfree(&mris);
  LabelFree(&label);

  return(ERROR_NONE);
}
Example #12
0
static int con_and_fill_pathy(char* fname, 
                              char* ofname, 
                              MRIS* mris, 
                              int seed)
{
  int     err;
  int     num_paths;
  PATH **paths = NULL;
  LABEL *label;
  int *vtxnolist,*final_path, path_length, k, vtxno, nlabel, nth;

  /* Read the paths file. */
  err = PathReadMany (fname, &num_paths, &paths);
  if (ERROR_NONE != err) {
    ErrorReturn (ERROR_BADFILE,
                 (ERROR_BADFILE, "Couldn't read %s", fname));
  }

  /* Warn if we have more than one path. */
  if (num_paths != 1) {
    printf ("WARNING: Found multiple paths in paths file. \n"
	    "Maybe you didn't mean to use the connect option?\n"
	    "Will only convert first path\n\n");
  }

  final_path = (int*) calloc(mris->nvertices,sizeof(int));
  vtxnolist = (int*) calloc(paths[0]->n_points,sizeof(int));
  for(k=0; k < paths[0]->n_points; k++)
    vtxnolist[k] = paths[0]->points[k].vno;

  printf("Finding path...");

  MRISfindPath(vtxnolist, paths[0]->n_points, mris->nvertices, 
	       final_path, &path_length, mris );

  // Make sure they are 0
  for(k=0; k < mris->nvertices; k++) mris->vertices[k].val = 0;

  for(k=0; k < path_length; k++){
    vtxno = final_path[k];
    mris->vertices[vtxno].val = 1;
  }

  printf("Filling %d\n",seed);
  MRISfill(mris, seed);

  nlabel = 0;
  for(k=0; k < mris->nvertices; k++) 
    if(mris->vertices[k].val > 0.5) nlabel++;

  printf("nlabel %d\n",nlabel);
  label = LabelAlloc(nlabel, subject, "");
  label->n_points = nlabel;
  nth = 0;
  for(k=0; k < mris->nvertices; k++){
    if(mris->vertices[k].val < 0.5) continue;
    label->lv[nth].vno = k;
    label->lv[nth].x = mris->vertices[k].x;
    label->lv[nth].y = mris->vertices[k].y;
    label->lv[nth].z = mris->vertices[k].z;
    label->lv[nth].stat = 0;
    nth ++;
  }
  printf("Saving label file %s\n",ofname);
  LabelWrite(label, ofname);

  PathFree(&paths[0]);
  free (paths);
  MRISfree(&mris);
  free(final_path);
  free(vtxnolist);
  LabelFree(&label);

  return(ERROR_NONE);
}
Example #13
0
/*-------------------------------------------------------------------------*/
static int connect_path(char* fname, char* ofname, char *subject, char *hemi) 
{
  int     err;
  int     num_paths;
  PATH **paths = NULL, *newpath;

  int *vtxnolist,*final_path, path_length, k, vtxno;
  char tmpstr[2000];
  MRIS *mris;

  /* Read the paths file. */
  err = PathReadMany (fname, &num_paths, &paths);
  if (ERROR_NONE != err) {
    ErrorReturn (ERROR_BADFILE,
                 (ERROR_BADFILE, "Couldn't read %s", fname));
  }

  /* Warn if we have more than one path. */
  if (num_paths != 1) {
    printf ("WARNING: Found multiple paths in paths file. \n"
	    "Maybe you didn't mean to use the connect option?\n"
	    "Will only convert first path\n\n");
  }

  sprintf(tmpstr,"%s/%s/surf/%s.orig",getenv("SUBJECTS_DIR"),subject,hemi);
  printf("Reading %s\n",tmpstr);
  mris = MRISread(tmpstr);
  if(mris == NULL) exit(1);

  final_path = (int*) calloc(mris->nvertices,sizeof(int));
  vtxnolist = (int*) calloc(paths[0]->n_points,sizeof(int));
  for(k=0; k < paths[0]->n_points; k++)
    vtxnolist[k] = paths[0]->points[k].vno;

  MRISfindPath(vtxnolist, paths[0]->n_points, mris->nvertices, 
	       final_path, &path_length, mris );

  newpath = PathAlloc(path_length,"");
  newpath->n_points = path_length;
  newpath->points = (PATH_POINT *) calloc(path_length,sizeof(PATH_POINT));
  for(k=0; k < path_length; k++){
    vtxno = final_path[k];
    newpath->points[k].vno = vtxno;
    newpath->points[k].x = mris->vertices[vtxno].x;
    newpath->points[k].y = mris->vertices[vtxno].y;
    newpath->points[k].z = mris->vertices[vtxno].z;
  }

  /* Write the path file. */
  err = PathWriteMany (ofname, 1, &newpath);
  if (0 != err) {
    ErrorReturn (ERROR_BADFILE,
                 (ERROR_BADFILE, "Couldn't write to %s", ofname));
  }

  PathFree(&paths[0]);
  free (paths);
  PathFree(&newpath);
  MRISfree(&mris);
  free(final_path);
  free(vtxnolist);

  return(ERROR_NONE);
}
Example #14
0
/*--------------------------------------------------------------------*/
static int convert_path_to_label (char* fname, char* ofname) {
  int     err;
  int     num_paths;
  PATH**  paths       = NULL;
  int     path_index;
  int     label_size;
  int     pno;
  LABEL*  label       = NULL;
  int     label_vno;

  /* Read the paths file. */
  err = PathReadMany (fname, &num_paths, &paths);
  if (ERROR_NONE != err) {
    ErrorReturn (ERROR_BADFILE,
                 (ERROR_BADFILE, "Couldn't read %s", fname));
  }

  printf ("INFO: Got %d paths\n\n", num_paths);

  /* Go through the paths we can and build a sum of number of points
     we'll need to write to the label, including an extra one per path
     for the sentinel value. */
  label_vno = 0;
  label_size = 0;
  for (path_index = 0; path_index < num_paths; path_index++) {
    label_size += paths[path_index]->n_points + 1;
  }

  /* Allocate a label of that size. */
  label = LabelAlloc (label_size, NULL, NULL);
  if (NULL == label) {
    ErrorReturn (ERROR_NO_MEMORY,
                 (ERROR_NO_MEMORY, "Couldn't allocate label of %d points",
                  label_size));
  }
  label->n_points = label_size;

  /* For each path...*/
  for (path_index = 0; path_index < num_paths; path_index++) {
    /* Write all the path points to the label. */
    for (pno = 0; pno < paths[path_index]->n_points; pno++) {
      label->lv[label_vno].x = paths[path_index]->points[pno].x;
      label->lv[label_vno].y = paths[path_index]->points[pno].y;
      label->lv[label_vno].z = paths[path_index]->points[pno].z;
      label->lv[label_vno].vno = paths[path_index]->points[pno].vno;
      label_vno++;
    }

    /* Write the sentinel value. */
    label->lv[label_vno].x = -99999;
    label->lv[label_vno].y = -99999;
    label->lv[label_vno].z = -99999;
    label->lv[label_vno].vno = -99999;
    label_vno++;

    /* Go ahead and delte the path now. */
    PathFree (&paths[path_index]);
  }

  /* Free our paths variable. */
  free (paths);

  /* Write the label file. */
  LabelWrite (label, ofname);

  /* Free the label. */
  LabelFree (&label);

  return (ERROR_NONE);
}