int move_npc(Character *c) { int abil = character_getAbil(c); int cx, cy, pcx, pcy, mempcx, mempcy; character_getLocation( c, & cx, & cy); character_getLocation(pc, &pcx, &pcy); character_getMemPCLocation(c, &mempcx, &mempcy); // intelligence int smart = (1 & abil); // telepathy int tele = (2 & abil); // tunneling int tunnel = (4 & abil); // erratic int erratic = (8 & abil); // erractic desicion or no moving instruction if ((erratic && rand() % 2) || (!smart && !tele)) { move_random(c, tunnel); // 4 & c->c gives tunneling } else if (smart && tele) { move_dijkstra(c, tunnel); } else if (smart) { if (dungeon_lineOfSight(cx, cy, pcx, pcy)) { move_dijkstra(c, tunnel); character_setMemPCLocation(c, pcx, pcy); } else if (mempcx && mempcy) // has last seen PC { move_toward(c, mempcx, mempcy, tunnel); } else move_random(c, tunnel); } else if (tele) { move_toward(c, pcx, pcy, tunnel); } else { move_random(c, tunnel); } return 0; }
int move_toward(Character *c, int x, int y, int t) { int cx, cy; character_getLocation(c, &cx, &cy); int dx = x - cx; int dy = y - cy; int incx = 0; int incy = 0; if (dx > 0) incx = 1; if (dx < 0) incx = -1; if (dy > 0) incy = 1; if (dy < 0) incy = -1; int nextx = cx + incx; int nexty = cy + incy; if (t || !hardness[nexty][nextx]) update(c, nextx, nexty); else move_random(c, t); // get stuck return 0; }
/* escape algorithm for PC */ static void escape() { // look for optimal escape int x, y, escx = 0, escy = 0, min = 1<<8; int pcx, pcy; character_getLocation(pc, &pcx, &pcy); for (x = pcx-1; x <= pcx+1; x++) { for (y = pcy-1; y <= pcy+1; y++) { if (x == pcx && y == pcy) continue; // skip myself if (hardness[y][x]) continue; // PC cannot tunnel if (scanArea(x, y, 1)) continue; // absolutely not safe scanArea(x, y, 2); if (scc < min) { min = scc; escx = x; escy = y; } } } if (escx && escy) update(pc, escx, escy); else move_random(pc, 0); }
static int player_loop(t_lemipc *lemipc) { while (g_check) { printf("still Alive !\n"); lock_sem(lemipc->game->sem_id); if (check_death(lemipc) == 1) return (0); move_random(lemipc); usleep(600000); unlock_sem(lemipc->game->sem_id); } return (1); }
void piece_move (piece_info_t *obj) { int changed_loc; int speed, max_hits; int saved_moves; int need_input; long saved_loc; city_info_t *cityp; /* set func for piece if on city */ cityp = find_city (obj->loc); if (cityp != NULL) if (cityp->func[obj->type] != NOFUNC) obj->func = cityp->func[obj->type]; changed_loc = FALSE; /* not changed yet */ speed = piece_attr[obj->type].speed; max_hits = piece_attr[obj->type].max_hits; need_input = FALSE; /* don't require user input yet */ while (obj->moved < obj_moves (obj)) { saved_moves = obj->moved; /* save moves made */ saved_loc = obj->loc; /* remember starting location */ if (awake (obj) || need_input){ /* need user input? */ ask_user (obj); display_loc_u (obj->loc); /* let user see result */ need_input = FALSE; /* we got it */ } if (obj->moved == saved_moves) /* user set function? */ switch (obj->func) { /* handle preprogrammed function */ case NOFUNC: break; case RANDOM: move_random (obj); break; case SENTRY: obj->moved = speed; break; case FILL: move_fill (obj); break; case LAND: move_land (obj); break; case EXPLORE: move_explore (obj); break; case ARMYLOAD: move_armyload (obj); break; case ARMYATTACK:move_armyattack (obj); break; case TTLOAD: move_ttload (obj); break; case REPAIR: move_repair (obj); break; case WFTRANSPORT: move_transport (obj); break; case MOVE_N: case MOVE_NE: case MOVE_E: case MOVE_SE: case MOVE_S: case MOVE_SW: case MOVE_W: case MOVE_NW: move_dir (obj); break; default: move_path (obj); break; } if (obj->moved == saved_moves) need_input = TRUE; /* * handle fighters specially. If in a city or carrier, turn * is over and reset range to max. Otherwise, if * range = 0, fighter crashes and burns and turn is over. */ if (obj->type == FIGHTER && obj->hits > 0) { if ((user_map[obj->loc].contents == 'O' || user_map[obj->loc].contents == 'C') && obj->moved > 0) { obj->range = piece_attr[FIGHTER].range; obj->moved = speed; obj->func = NOFUNC; info("Landing confirmed."); } else if (obj->range == 0) { info("Fighter at %d crashed and burned.", obj->loc); kill_obj (obj, obj->loc); } } if (saved_loc != obj->loc) changed_loc = TRUE; } /* if a boat is in port, damaged, and never moved, fix some damage */ if (obj->hits > 0 /* still alive? */ && !changed_loc /* object never changed location? */ && obj->type != ARMY && obj->type != FIGHTER /* it is a boat? */ && obj->hits < max_hits /* it is damaged? */ && user_map[obj->loc].contents == 'O') /* it is in port? */ obj->hits++; /* fix some damage */ }