/* ==================== pod.cpp ==================== */ #include #include #include #include "pod.h" #include "../blotchmaker/mapsearch_pod.h" #include "../blotchmaker/stlastar.h" #include "../blotchmaker/map.h" const long TIMING_LOOP_DELAY =200000; // static variables: Map* Pod::map; // a pointer to the map. int Pod::pod_count = 0; // total pods on the map number - doubles as a pod ID counter vector Pod::pod_index; // a vector of pointers to pods. now we can look them up by ID number // list of fun names: string pod_names[] = { "Fluffy", "DiamondFire", "Ghost", "Bouncer", "Starbuck", "RogerWilco", "GooGoo", "HoJu", "Cupcake Whooper" }; // *STRUCTORS // ================================== Pod::Pod(int axis){ // FIXME: ERROR - map not defined for all pods yet path = NULL; name = pod_names[ rand() % 9 ]; // give the pod some generic dummy info for testing: align = axis; id = 1; att = 75; def = 60; opac = 40; acu = 65; size = 30; // aka "weight" spd = 50; x_pos = 0; y_pos = 0; avoid = 1; engage = 0; // put me somewhere on the map // set map coords // set internal coords // give me attributes based on shapes (?) // add to pod index IncPodCount(); } Pod::~Pod(){ // free up A* search if filled if (path != NULL) {path->FreeSolutionNodes();} DecPodCount(); // remove from vector? } // GET FUNCTIONS // ================================== string Pod::GetName(){ return name; } int Pod::GetAlign(){ return align; } int Pod::GetID(){ return id; } int Pod::GetAtt(){ return att; } int Pod::GetDef(){ return def; } int Pod::GetOpac(){ return opac; } int Pod::GetAcu(){ return acu; } int Pod::GetSize(){ return size; } int Pod::GetSpd(){ return spd; } int Pod::GetXPos(){ return x_pos; } int Pod::GetYPos(){ return y_pos; } // ================================== // GENERAL POD FUNCTIONS // ================================== // map pointer setter - used for all pods. void Pod::SetMapPointer(Map &incoming_map){ map = &incoming_map; } void Pod::IncPodCount(){ pod_count++; } void Pod::DecPodCount(){ pod_count--; } // ================================== // MOVEMENT FUNCTIONS // ================================== bool Pod::CreatePath(int x1, int y1, int x2, int y2) { // return 1 if path was made, 0 if path could not be found int timeout = ( ( map->GetCols() * map->GetRows() ) / 2 ); // set pod pointer for all mapsearch nodes // do this every time we search because many other pods are searching too and they all override eachother MapSearchNode_Pod::SetPodPointer(*this); // set the pointer to the map if it hasn't already been // FIXME - we dont need this here every time. just once at the beginning of program. remove and add error trap MapSearchNode_Pod::SetMapPointer(*map); // if we are starting a new search, we are obviously done with the old one, so free the nodes up if (path != NULL) {path->FreeSolutionNodes();} // Create an instance of the search class path = new AStarSearch; // Create and set start/goal states MapSearchNode_Pod nodeStart(x1,y1); MapSearchNode_Pod nodeEnd(x2,y2); path->SetStartAndGoalStates( nodeStart, nodeEnd ); int SearchState; int SearchSteps = 0; do { SearchState = path->SearchStep(); SearchSteps++; } while( SearchState == AStarSearch::SEARCH_STATE_SEARCHING && SearchSteps < timeout ); // if solution found: if( SearchState == AStarSearch::SEARCH_STATE_SUCCEEDED ) { return 1; } // otherwise, kill and return 0 else { delete path; path = NULL; return 0; } } bool Pod::Walk() { // moves the pod across the map along the specified path until it runs out of juice. // ERROR: got path? // the working node (our current position) MapSearchNode_Pod* is; MapSearchNode_Pod* was = path->GetSolutionStart(); int spd_allot = spd; // speed allotment - depletes with each step while(1) { // foreach tile Step //-------\/------move the pod to the next tile // get next tile in path is = path->GetSolutionNext(); if( !is ) { // no more tiles in path solution - already on goal path->FreeSolutionNodes(); path = NULL; return 1; } // can i afford to move there? if (map->GetRoadQ(is->x, is->y) <= spd_allot) { spd_allot -= map->GetRoadQ(is->x, is->y); // subtract road cost from allotment } else {break;} // can't afford to move anymore // retie all the occupation and positional markers map->SetOccu(is->x, is->y, *this ); // move the occupation to the new tile map->UnsetOccu(was->x, was->y); // NULL the old one, it's vacant x_pos = is->x; // set internal positional markers y_pos = is->y; //-------/\------move the pod to the next tile // trample road map->TrampleRoad(is->x, is->y); // TODO: maybe add trample amount depending on pod weight? // timing loop and shell display clock_t s = clock(); while ( (clock() - s) < TIMING_LOOP_DELAY) {continue;} // NOTE: unix dependent time ticks system("clear"); map->PrintToScreen(); // Engagement Prompt (will you marry me? :-) // check for enemy occupations in each adjascent tile Pod* enemy; int x_coords[] = {1,-1,0,0}; int y_coords[] = {0,0,1,-1}; for (int i=0; i < 4; i++) { if (map->IsValid(is->x + x_coords[i], is->y + y_coords[i]) == 0) {continue;} enemy = map->GetOccu(is->x + x_coords[i], is->y + y_coords[i]); if (enemy != NULL) { // if it's occupied if (enemy->GetAlign() != this->GetAlign()) { // if he's a bad guy PromptEngagement(); // let's fight! (or not) } } } // finish up moving was = is; // shift pointers down a notch is = NULL; // not needed, but just to be safe }; return 0; // did not hit goal after all moving finished this round } // ================================== // MISC FUNCTIONS // ================================== void Pod::PromptEngagement() { /* TODO: function not made yet */ cout << "Found Adjascent Enemy Pod! Shall i tear his arms off, sir?\n"; // decide to fight depending on what engagement set to. // send signal to the other pod so he can fight us if he wants } // ==================================