SokobanState::SokobanState(const SokobanLevel &level) : level(level), num_boxes(level.NumBoxes()), free_goals((1<<num_boxes)-1), player(level.GetPlayer()), boxes(new Position[num_boxes]), parent(NULL), last_move(0), last_box(-1), last_move_distance(0), scribble(level.GetScribble(free_goals)) { for (int i = 0; i < num_boxes; ++i) boxes[i] = level.GetBox(i); CalcCost(); }
void BuildQueue(TRACKPTR * queueheadnodePtr, FILE * ifp) { /* declare variables to be used repeatedly within function */ CAR newcar; NODEPTR temp; /* allocate memory for the incoming track queue */ (*queueheadnodePtr) = (TRACKPTR)malloc(sizeof(TRACK)); if (*queueheadnodePtr == NULL) { fprintf(stderr, "Not enough memory free.\n"); exit(-3); } /* initialize the head node for the incoming track queue */ InitiateTrack(queueheadnodePtr); /* initilize the car struct to be used within the while loop for building the queue */ InitializeCar(&newcar); /* read through the data file and build the queue */ while(fscanf(ifp, "%d %s %s %s %d %d", &newcar.carNum, newcar.cargo, newcar.origin, newcar.destination, &newcar.weight, &newcar.miles) != -1) { /* calculates the cost of moving the specific car */ CalcCost(&newcar); /* finds memory for the new node and stores in temp */ temp = CreateNode(); /* sets data from newcar struct into the new node */ SetData(&temp, newcar); /* get the track id for the specific node */ temp -> trackId = GetTrackId(temp -> data.destination); /* insert the node into the queue */ InsertNode(&temp, queueheadnodePtr); } }
/* * Compile the terminal capabilities for a display. * Input: tgetent(, D_termname) extra_incap, extra_outcap. * Effect: display initialisation. */ int InitTermcap(int width, int height) { char *s; int i; char tbuf[TERMCAP_BUFSIZE], *tp; int t, xue, xse, xme; memset(tbuf, 0, sizeof(tbuf)); if (*D_termname == 0 || e_tgetent(tbuf, D_termname) != 1) { Msg(0, "Cannot find terminfo entry for '%s'.", D_termname); return -1; } if ((D_tentry = malloc(TERMCAP_BUFSIZE + (extra_incap ? strlen(extra_incap) + 1 : 0))) == 0) { Msg(0, "%s", strnomem); return -1; } /* * loop through all needed capabilities, record their values in the display */ tp = D_tentry; for (i = 0; i < T_N; i++) { switch (term[i].type) { case T_FLG: D_tcs[i].flg = e_tgetflag(term[i].tcname); break; case T_NUM: D_tcs[i].num = e_tgetnum(term[i].tcname); break; case T_STR: D_tcs[i].str = e_tgetstr(term[i].tcname, &tp); /* no empty strings, please */ if (D_tcs[i].str && *D_tcs[i].str == 0) D_tcs[i].str = 0; break; default: Panic(0, "Illegal tc type in entry #%d", i); /*NOTREACHED*/} } /* * Now a good deal of sanity checks on the retrieved capabilities. */ if (D_HC) { Msg(0, "You can't run screen on a hardcopy terminal."); return -1; } if (D_OS) { Msg(0, "You can't run screen on a terminal that overstrikes."); return -1; } if (!D_CL) { Msg(0, "Clear screen capability required."); return -1; } if (!D_CM) { Msg(0, "Addressable cursor capability required."); return -1; } if ((s = getenv("COLUMNS")) && (i = atoi(s)) > 0) D_CO = i; if ((s = getenv("LINES")) && (i = atoi(s)) > 0) D_LI = i; if (width) D_CO = width; if (height) D_LI = height; if (D_CO <= 0) D_CO = 80; if (D_LI <= 0) D_LI = 24; if (D_CTF) { /* standard fixes for xterms etc */ /* assume color for everything that looks ansi-compatible */ if (!D_CAF && D_ME && (InStr(D_ME, "\033[m") || InStr(D_ME, "\033[0m"))) { D_CAF = "\033[3%p1%dm"; D_CAB = "\033[4%p1%dm"; } if (D_OP && InStr(D_OP, "\033[39;49m")) D_CAX = 1; if (D_OP && (InStr(D_OP, "\033[m") || InStr(D_OP, "\033[0m"))) D_OP = 0; /* ISO2022 */ if ((D_EA && InStr(D_EA, "\033(B")) || (D_AS && InStr(D_AS, "\033(0"))) D_CG0 = 1; if (InStr(D_termname, "xterm") || InStr(D_termname, "rxvt") || (D_CKM && InStr(D_CKM, "\033[M"))) D_CXT = 1; /* "be" seems to be standard for xterms... */ if (D_CXT) D_BE = 1; } if (nwin_options.flowflag == nwin_undef.flowflag) nwin_default.flowflag = D_CNF ? FLOW_OFF : D_NX ? FLOW_ON : FLOW_AUTOFLAG; D_CLP |= (!D_AM || D_XV || D_XN); if (!D_BL) D_BL = "\007"; if (!D_BC) { if (D_BS) D_BC = "\b"; else D_BC = D_LE; } if (!D_CR) D_CR = "\r"; if (!D_NL) D_NL = "\n"; /* * Set up attribute handling. * This is rather complicated because termcap has different * attribute groups. */ if (D_UG > 0) D_US = D_UE = 0; if (D_SG > 0) D_SO = D_SE = 0; /* Unfortunately there is no 'mg' capability. * For now we think that mg > 0 if sg and ug > 0. */ if (D_UG > 0 && D_SG > 0) D_MH = D_MD = D_MR = D_MB = D_ME = 0; xue = ATYP_U; xse = ATYP_S; xme = ATYP_M; if (D_SO && D_SE == 0) { Msg(0, "Warning: 'so' but no 'se' capability."); if (D_ME) xse = xme; else D_SO = 0; } if (D_US && D_UE == 0) { Msg(0, "Warning: 'us' but no 'ue' capability."); if (D_ME) xue = xme; else D_US = 0; } if ((D_MH || D_MD || D_MR || D_MB) && D_ME == 0) { Msg(0, "Warning: 'm?' but no 'me' capability."); D_MH = D_MD = D_MR = D_MB = 0; } /* * Does ME also reverse the effect of SO and/or US? This is not * clearly specified by the termcap manual. Anyway, we should at * least look whether ME and SE/UE are equal: */ if (D_UE && D_SE && strcmp(D_SE, D_UE) == 0) xse = xue; if (D_SE && D_ME && strcmp(D_ME, D_SE) == 0) xse = xme; if (D_UE && D_ME && strcmp(D_ME, D_UE) == 0) xue = xme; for (i = 0; i < NATTR; i++) { D_attrtab[i] = D_tcs[T_ATTR + i].str; D_attrtyp[i] = i == ATTR_SO ? xse : (i == ATTR_US ? xue : xme); } /* Set up missing entries (attributes are priority ordered) */ s = 0; t = 0; for (i = 0; i < NATTR; i++) if ((s = D_attrtab[i])) { t = D_attrtyp[i]; break; } for (i = 0; i < NATTR; i++) { if (D_attrtab[i] == 0) { D_attrtab[i] = s; D_attrtyp[i] = t; } else { s = D_attrtab[i]; t = D_attrtyp[i]; } } if (D_CAF || D_CAB || D_CSF || D_CSB) D_hascolor = 1; if (D_UT) D_BE = 1; /* screen erased with background color */ if (!D_DO) D_DO = D_NL; if (!D_SF) D_SF = D_NL; if (D_IN) D_IC = D_IM = 0; if (D_EI == 0) D_IM = 0; /* some strange termcap entries have IC == IM */ if (D_IC && D_IM && strcmp(D_IC, D_IM) == 0) D_IC = 0; if (D_KE == 0) D_KS = 0; if (D_CVN == 0) D_CVR = 0; if (D_VE == 0) D_VI = D_VS = 0; if (D_CCE == 0) D_CCS = 0; if (D_CG0) { if (D_CS0 == 0) D_CS0 = "\033(%p1%c"; if (D_CE0 == 0) D_CE0 = "\033(B"; D_AC = 0; D_EA = 0; } else if (D_AC || (D_AS && D_AE)) { /* some kind of graphics */ D_CS0 = (D_AS && D_AE) ? D_AS : ""; D_CE0 = (D_AS && D_AE) ? D_AE : ""; D_CC0 = D_AC; } else { D_CS0 = D_CE0 = ""; D_CC0 = 0; D_AC = ""; /* enable default string */ } for (i = 0; i < 256; i++) D_c0_tab[i] = i; if (D_AC) { /* init with default string first */ s = "l+m+k+j+u+t+v+w+q-x|n+o~s_p\"r#`+a:f'g#~o.v-^+<,>h#I#0#y<z>"; for (i = (strlen(s) - 2) & ~1; i >= 0; i -= 2) D_c0_tab[(int)(unsigned char)s[i]] = s[i + 1]; } if (D_CC0) for (i = (strlen(D_CC0) - 2) & ~1; i >= 0; i -= 2) D_c0_tab[(int)(unsigned char)D_CC0[i]] = D_CC0[i + 1]; if (D_PF == 0) D_PO = 0; if (D_CXC) if (CreateTransTable(D_CXC)) return -1; /* Termcap fields Z0 & Z1 contain width-changing sequences. */ if (D_CZ1 == 0) D_CZ0 = 0; CheckScreenSize(0); if (D_TS == 0 || D_FS == 0 || D_DS == 0) D_HS = 0; if (D_HS) { if (D_WS < 0) D_WS = 0; } D_has_hstatus = hardstatusemu & ~HSTATUS_ALWAYS; if (D_HS && !(hardstatusemu & HSTATUS_ALWAYS)) D_has_hstatus = HSTATUS_HS; if (D_CKJ) { int enc = FindEncoding(D_CKJ); if (enc != -1) D_encoding = enc; } if (!D_tcs[T_NAVIGATE].str && D_tcs[T_NAVIGATE + 1].str) D_tcs[T_NAVIGATE].str = D_tcs[T_NAVIGATE + 1].str; /* kh = @1 */ if (!D_tcs[T_NAVIGATE + 2].str && D_tcs[T_NAVIGATE + 3].str) D_tcs[T_NAVIGATE + 2].str = D_tcs[T_NAVIGATE + 3].str; /* kH = @7 */ D_UPcost = CalcCost(D_UP); D_DOcost = CalcCost(D_DO); D_NLcost = CalcCost(D_NL); D_LEcost = CalcCost(D_BC); D_NDcost = CalcCost(D_ND); D_CRcost = CalcCost(D_CR); D_IMcost = CalcCost(D_IM); D_EIcost = CalcCost(D_EI); if (D_CAN) { D_auto_nuke = true; } if (D_COL > 0) { D_obufmax = D_COL; D_obuflenmax = D_obuflen - D_obufmax; } /* Some xterm entries set F0 and F10 to the same string. Nuke F0. */ if (D_tcs[T_CAPS].str && D_tcs[T_CAPS + 10].str && !strcmp(D_tcs[T_CAPS].str, D_tcs[T_CAPS + 10].str)) D_tcs[T_CAPS].str = 0; /* Some xterm entries set kD to ^?. Nuke it. */ if (D_tcs[T_NAVIGATE_DELETE].str && !strcmp(D_tcs[T_NAVIGATE_DELETE].str, "\0177")) D_tcs[T_NAVIGATE_DELETE].str = 0; /* wyse52 entries have kcub1 == kb == ^H. Nuke... */ if (D_tcs[T_CURSOR + 3].str && !strcmp(D_tcs[T_CURSOR + 3].str, "\008")) D_tcs[T_CURSOR + 3].str = 0; D_nseqs = 0; for (i = 0; i < T_OCAPS - T_CAPS; i++) remap(i, 1); for (i = 0; i < kmap_extn; i++) remap(i + (KMAP_KEYS + KMAP_AKEYS), 1); D_seqp = D_kmaps + 3; D_seql = 0; D_seqh = 0; D_tcinited = 1; MakeTermcap(0); CheckEscape(); return 0; }
/**\brief chooses who the AI should target given the list of the AI's enemies * */ int AI::ChooseTarget( lua_State *L ){ //printf("choosing target\n"); SpriteManager *sprites = Simulation_Lua::GetSimulation(L)->GetSpriteManager(); list<Sprite*> *nearbySprites = sprites->GetSpritesNear(this->GetWorldPosition(), COMBAT_RANGE, DRAW_ORDER_SHIP); nearbySprites->sort(CompareAI); list<Sprite*>::iterator it; list<enemy>::iterator enemyIt=enemies.begin(); //printf("printing list of enemies\n"); //printf("the size of enemies = %d\n", enemies.size() ); for(enemyIt=enemies.begin(); enemyIt!=enemies.end();){ if(sprites->GetSpriteByID( enemyIt->id)==NULL){ enemyIt=enemies.erase(enemyIt); } else { enemyIt++; } } //printf("printing list of nearby it->GetTarget()\n"); //int nearbySpritesSize=(*nearbySprites).size(); //printf("the size of nearbySprites = %d\n",nearbySpritesSize); /*for(it=nearbySprites->begin(); it!=nearbySprites->end(); it++){ if( (*it)->GetDrawOrder() ==DRAW_ORDER_SHIP ) printf("it->GetTarget() = %d\n",((AI*)(*it))->GetTarget() ); printf("it->GetID() = %d\n",(*it)->GetID() ); }*/ it=nearbySprites->begin(); enemyIt=enemies.begin(); int max=0,currTarget=-1; int threat=0; //printf("starting sprite iteration\n"); for(it= nearbySprites->begin(); it!=nearbySprites->end() && enemyIt!=enemies.end() ; it++){ if( (*it)->GetID()== this->GetID() ) continue; if( (*it)->GetDrawOrder() ==DRAW_ORDER_SHIP){ if( enemyIt->id < ((AI*) (*it))->GetTarget() ){ //printf("starting enemyIt iteration\n"); while ( enemyIt!=enemies.end() && enemyIt->id < ( (AI*) (*it) )->GetTarget() ) { // printf("enemyIt = %d\n",enemyIt->id); // printf("it->GetTarget() = %d\n", ((AI*) (*it))->GetTarget() ); if ( !InRange( sprites->GetSpriteByID(enemyIt->id)->GetWorldPosition() , this->GetWorldPosition() ) ) { enemyIt=enemies.erase(enemyIt); threat=0; continue; } int cost= CalcCost( threat + ( (Ship*)sprites->GetSpriteByID(enemyIt->id))->GetTotalCost() , enemyIt->damage); //damage might need to be scaled so that the damage can be adquately compared to the treat level if(currTarget==-1 || max<cost){ currTarget=enemyIt->id; max=cost; } threat=0; enemyIt++; } //printf("successfully completed enemyIt iteration\n"); it--; continue; } if( enemyIt->id == ((AI*) (*it))->GetTarget() ) threat-= ( (Ship*)(*it) )->GetTotalCost(); } else{ LogMsg( ERR, "Error Sprite %d is not an AI\n", (*it)->GetID() ); } } // printf("finished sprite iteration\n"); // cout<<"enemies.size()="<<enemies.size()<<'\n'; while (enemyIt!=enemies.end()) { //printf("starting enemyIt iteration mark2\n"); if ( !InRange( sprites->GetSpriteByID(enemyIt->id)->GetWorldPosition() , this->GetWorldPosition() ) ) { enemyIt=enemies.erase(enemyIt); threat=0; continue; } //printf("finished In range check\n"); //printf("calculate cost\n"); int cost= CalcCost( threat + ( (Ship*)sprites->GetSpriteByID(enemyIt->id))->GetTotalCost() , enemyIt->damage); //damage might need to be scaled so that the damage can be adquately compared to the treat level threat=0; //printf("finished calculating cost\n"); if( currTarget==-1 || max < cost){ max=cost; currTarget=enemyIt->id; } //printf("successfully completed enemyIt iteration mark 2\n"); enemyIt++; } //printf("finished choosing target. %d is the target\n",currTarget); return currTarget; }
// create new state with one new move applied // note: (assumed to be valid, check before) SokobanState::SokobanState(const SokobanState &state, int box, char move, int distance, int free_goals): level(state.level), num_boxes(state.num_boxes), free_goals(free_goals), player(state.player), boxes(new Position[num_boxes]), parent(&state), last_move(move), last_box(box), last_move_distance(distance), scribble(level.GetScribble(free_goals)) { for (int i = 0; i < num_boxes; ++i) boxes[i] = state.boxes[i]; // The player is where the box was before the move // (gets modified for longer pushes) player = boxes[box]; // Make the push and sort and make sure the boxes are sorted in the array switch (move) { case MOVE_UP: boxes[box].y -= distance; player.y -= distance-1; for (int idx = box - 1; idx >= 0; --idx) { if (boxes[idx] < boxes[box]) break; // done, all earlier in order Position p = boxes[idx]; boxes[idx] = boxes[box]; boxes[box] = p; box --; } break; case MOVE_DOWN: boxes[box].y += distance; player.y += distance-1; for (int idx = box + 1; idx < num_boxes; ++idx) { if (boxes[box] < boxes[idx]) break; // done, all earlier in order Position p = boxes[idx]; boxes[idx] = boxes[box]; boxes[box] = p; box ++; } break; case MOVE_LEFT: // will be sorted, or collide, but move is assumed to be valid boxes[box].x -= distance; player.x -= distance-1; break; case MOVE_RIGHT: // will be sorted, or collide, but move is assumed to be valid boxes[box].x += distance; player.x += distance-1; break; default: assert(0); } last_box = box; CalcCost(); }