int dtf_setunits(t_dtf *map) { int i; int j; i = -1; if (get_crtreturn(NULL)) return (0); if (map == NULL || map->map == NULL || map->units != NULL) return (dbgerr("Error: dtf_setunits: NULL or invalid parameters")); if ((map->units = createlist(NULL, NULL)) == NULL) return (dbgcrt("Error: dtf_setunits: createlist function returned NULL")); while (++i < map->height && map->map[i] != NULL) { j = -1; while (++j < map->width) { if (ISERRV(map->map[i][j])) return (dbgcrt("Error: dtf_setunits: Invalid value in the map")); else if (ISREDV(map->map[i][j]) || ISBLUE(map->map[i][j])) { if (!dtf_addunit(map, j, i)) return (dbgerr("Error: dtf_setunits: Add unit failed")); } } } return (1); }
int dtf_createunits(t_dtf *map) { if (get_crtreturn(NULL)) return (0); if (map == NULL) return (dbgerr("Error: dtf_createunits: NULL parameter")); if (!dtf_setunits(map)) return (dbgerr("Error: dtf_createunits: Can't create the units list")); return (1); }
int dtf_createmap(t_dtf *map, char ***raw) { if (get_crtreturn(NULL)) return (0); if (map == NULL || raw == NULL) return (dbgerr("Error: dtf_createmap: NULL parameter")); if ((map->width = get_mapwidth(raw[0])) <= 0 || (map->height = get_mapheight(raw)) <= 0 || !check_maprect(map, raw) || !convert_rawtomap(map, raw) || !check_borders(map)) return (dbgerr("Error: dtf_createmap: invalid parameters")); return (1); }
int dtf_addunit(t_dtf *map, int x, int y) { t_uni *unit; if (get_crtreturn(NULL)) return (0); if (map == NULL || map->map == NULL || x < 0 || y < 0 || x >= map->width || y >= map->height) return (dbgerr("Error: dtf_addunit: NULL or invalid parameters")); if ((unit = build_uni()) == NULL) return (dbgcrt("Error: dtf_addunit: Impossible to create the unit")); unit->id = map->map[y][x]; unit->px = x; unit->py = y; unit->life = UNITLF; unit->color = ((ISBLUE(unit->id)) ? BLUEPL : REDPLY); unit->used = 0; if (ISARCH(unit->id)) unit->type = ((ISBLUE(unit->id)) ? BLUEAC : REDARC); else if (ISSLDR(unit->id)) unit->type = ((ISBLUE(unit->id)) ? BLUESD : REDSDR); else if (ISKNGT(unit->id)) unit->type = ((ISBLUE(unit->id)) ? BLUEKN : REDKNG); else unit->type = DEADTP; return (dtf_addunittolist(map, unit)); }
int exec_simpleins(t_dtf *map, char *ins) { if (get_crtreturn(NULL)) return (0); if (map == NULL || ins == NULL) return (dbgerr("Error: exec_simpleins: Invalid or NULL parameter")); if (!my_strcmp(ins, STRATG)) return (map->strategic(map)); else if (!my_strcmp(ins, PASSCD)) return (map->pass(map)); else if (!my_strcmp(ins, EXITCD)) return (dbgcrt("A player has rage quit")); else { my_putstr(ERRCMD); return (dbgerr("Error: exec_simpleins: Unknow command")); } return (1); }
void Buffer::alloc(size_t size) { free(); mMemory = ::malloc(size); if (mMemory == NULL) { dbgerr("failed to allocate %lu bytes\n", size); return; } mSize = size; }
int dtf_strategic(t_dtf *map) { if (get_crtreturn(NULL)) return (0); if (map == NULL) return (dbgerr("Error: dtf_strategic: NULL parameter")); my_printf("The %s is %d on %d.\n", map->name, (map->width - 1), (map->height - 1)); dtf_describeunits(map); return (1); }
int dtf_move(t_dtf *map, int id, char *position) { t_uni *tmp; if (get_crtreturn(NULL)) return (0); if (map == NULL || position == NULL || ISERRV(id) || (tmp = get_unitbyid(map, id)) == NULL) { my_putstr(DEFERR); return (dbgerr("Error: dtf_move: 1 or more invalid parameter")); } if (!my_strcmp(position, PUPCMD)) return (tmp->move(tmp, map, tmp->px, tmp->py - 1)); else if (!my_strcmp(position, DOWNCD)) return (tmp->move(tmp, map, tmp->px, tmp->py + 1)); else if (!my_strcmp(position, PRIGHT)) return (tmp->move(tmp, map, tmp->px + 1, tmp->py)); else if (!my_strcmp(position, LEFTCD)) return (tmp->move(tmp, map, tmp->px - 1, tmp->py)); my_putstr(DEFERR); return (dbgerr("Error: dtf_move: Unknown position")); }
int dtf_describeunits(t_dtf *map) { t_mls *cpy; if (get_crtreturn(NULL)) return (0); if (map == NULL || (cpy = map->units) == NULL) return (dbgerr("Error: dtf_describeunits: Invalid or NULL parameter")); while ((cpy = cpy->next) != NULL) { if (cpy->item != NULL) dtf_descunit(cpy->item); } return (1); }
int dtf_descunit(t_uni *unit) { if (get_crtreturn(NULL)) return (0); if (unit == NULL) return (dbgerr("Error: dtf_descunit: NULL or invalid parameter")); if (unit->color == DEADTP) return (0); my_printf("The %s unit %d is in %d/%d.\n", ((unit->color == BLUEPL) ? BLUEUN : REDUNI), unit->id, unit->px, unit->py); return (1); }
int exec_ins(t_dtf *map, char *str) { char **tab; if (get_crtreturn(NULL)) return (0); if (map == NULL || str == NULL || (tab = my_strtok(str, UNITOK)) == NULL || (tab[0] == NULL)) return (dbgerr("Error: exec_ins: NULL or invalid parameter")); if (tab[1] == NULL) return (exec_simpleins(map, tab[0])); else return (exec_complexins(map, tab)); return (1); }
int dtf_pass(t_dtf *map) { t_mls *cpy; t_uni *tmp; if (get_crtreturn(NULL)) return (0); if (map == NULL || (cpy = map->units) == NULL) return (dbgerr("Error: dtf_pass: NULL parameter")); map->actplayer = (map->actplayer == BLUEPL) ? REDPLY : BLUEPL; while ((cpy = cpy->next) != NULL && (tmp = cpy->item) != NULL) tmp->used = 0; dbgdok("Changed actual player"); return (1); }
int exec_complexins(t_dtf *map, char **tab) { if (get_crtreturn(NULL)) return (0); if (map == NULL || tab == NULL) return (0); if (count_els((void **)tab) == 4 && !my_strcmp(tab[1], MOVECD) && !my_strcmp(tab[2], TOWARD)) return (map->move(map, my_atoi(tab[0]), tab[3])); else { my_putstr(ERRCMD); return (dbgerr("Error: exec_simpleins: Unknow command")); } return (1); }
void Buffer::reAlloc(size_t size) { if (size == 0) { free(); return; } void* p = ::realloc(mMemory, size); if (p == NULL) { dbgerr("failed to allocate %lu bytes\n", size); return; } mMemory = p; mSize = size; }
int dtf_addunittolist(t_dtf *map, t_uni *unit) { t_mls *cpy; if (get_crtreturn(NULL)) return (0); if (map == NULL || unit == NULL || (cpy = map->units) == NULL) return (dbgerr("Error: dtf_addunittolist: Invalid or NULL parameter")); if (is_unitinlist(map, unit->id)) { gbgc_free(NULL, unit); return (dbgcrt("Error: dtf_addunittolist: Units multiple declaration")); } while (cpy->next != NULL) cpy = cpy->next; if (!list_addel(cpy, unit, NULL)) return (dbgcrt("Error: dtf_addunittolist: Can't add the unit to the list")); return (1); }
bool Buffer::readFile(const char* filename, bool nullterm) { // Read a file into memory buffer. If asked, a zero is appended at the end // so the buffer can be used as a null terminated string. FILE* f; struct stat st; bool ret = false; if (stat(filename, &st) == 0) { f = fopen(filename, "rb"); reAlloc(st.st_size + (nullterm ? 1 : 0)); char* buf = (char*)ptr(); if (buf != NULL) { if (fread(buf, 1, st.st_size, f) == (size_t)st.st_size) { if (nullterm) { buf[st.st_size] = '\0'; } ret = true; } } fclose(f); } if (!ret) { // Failed to read, free the buffer. free(); dbgerr("Failed to read file '%s' into buffer\n", filename); } return ret; }