static u16 global_add(struct global_state *gstate, struct string *name, value val) { struct symbol *pos; ivalue old_size, aindex; GCCHECK(val); GCPRO2(gstate, name); old_size = vector_len(gstate->environment->values); aindex = env_add_entry(gstate->environment, val); if (vector_len(gstate->environment->values) != old_size) /* Increase mvars too */ { struct vector *new_mvars = alloc_vector(vector_len(gstate->environment->values)); memcpy(new_mvars->data, gstate->mvars->data, gstate->mvars->o.size - sizeof(struct obj)); gstate->mvars = new_mvars; } GCPOP(2); gstate->mvars->data[aindex] = makeint(var_normal); pos = table_add_fast(gstate->global, name, makeint(aindex)); SET_READONLY(pos); /* index of global vars never changes */ return aindex; }
//two args: vec1 & vec2 cellpoint vector_eq(void) { int len1 = vector_len(args_ref(1)); int len2 = vector_len(args_ref(2)); int i; if (args_ref(1) == args_ref(2)){ args_pop(2); return a_true; } if (len1 != len2){ args_pop(2); return a_false; } for (i=0; i < len1; ++i){ reg = vector_ref(args_ref(1), i); args_push(vector_ref(args_ref(2), i)); args_push(reg); reg = equal(); if (is_false(reg)){ args_pop(2); return a_false; } } args_pop(2); return a_true; }
ray_of_death_t *ray_of_death_ctor( vector_t *destination ) { ray_of_death_t *rod = malloc(sizeof(*rod)); hboolean collides; body_create((body_t*)rod, &Ray_of_death); rod->intensity = MAX_ROD_INTENSITY; vector_copy(rod->body.pos, hedgehog[local_hedgehog]->body.pos); vector_inc(rod->body.pos, &arm1_relative_pos); vector_sub(destination, rod->body.pos, &rod->direction); vector_mul(&rod->direction, 500/vector_len(&rod->direction), &rod->direction); collides = NULL != polygon_check_collision(map, rod->body.pos, &rod->direction, destination); vector_sub(destination, rod->body.pos, &rod->direction); if (!collides) { vector_mul(&rod->direction, 500/vector_len(&rod->direction), &rod->direction); } { int i; for(i = 0; i < 100; i++) { particle[i].pos.x = rod->body.pos->x + i * rod->direction.x / 100.0; particle[i].pos.y = rod->body.pos->y + i * rod->direction.y / 100.0; if (i % 2) { particle[i].color.red = 1; particle[i].color.green = 1; particle[i].color.blue = 1; } else { particle[i].color.red = 0; particle[i].color.green = 0; particle[i].color.blue = 1; } } } object_pool_push(&game_bodies, rod); return rod; }
void prompt_to_load_file() { char msg[] = "Please select the map file you wish to load"; char path[FILENAME_MAX * 6] = { "\0" }; int result; mouse_callback = NULL; result = file_select_ex(msg, path, "txt", sizeof(path), 0, 0); if (!result) { if (nodelist != NULL && vector_len((vector_t*)nodelist) > 0) return; allegro_message("You did not select a file to load: there's nothing for the program to do. Exiting."); exit(0); } if (debugging) fprintf(stderr, "%s\n", path); nodelist = load_file(path); render_image(screen); render_image(backbuffer); render_info(screen); render_info(backbuffer); strncpy(last_file, path, sizeof(last_file)); mouse_callback = mouse_cb; }
static void is_gstate(struct global_state *gstate) { /* Could use lots more checks on gstate... */ TYPEIS(gstate, type_vector); if (vector_len(gstate) != 8) RUNTIME_ERROR(error_bad_type); }
/* return the vector_len(v), but handle that the size may be an index into seen values */ static long safe_vlen(struct vector *v, struct seen_values *seen) { if (v->o.flags & OBJ_FLAG_0) { long idx = v->o.size; return (seen->seen[idx].lhs_size - sizeof v->o) / sizeof v->data[0]; } return vector_len(v); }
static void reconstruct_coordinates(vector_t *nodes) { int qnt = rect_len*3; /* quantum (minimum space between nodes) */ int i, j, len = vector_len(nodes); int xpad = 0, ypad = 0; mapnode_t *nd, *adj_nd; for (i = 0; i < len; i++) { nd = vector_aref(nodes, i); if (!nd || nd->touch) continue; nd->touch = 1; for (j = 0; nd->adj[j].nodeptr != NULL; j++) { adj_nd = nd->adj[j].nodeptr; if (adj_nd->touch) // continue; // adj_nd->touch = 1; switch (*nd->adj[j].movestr) { case 'n': adj_nd->y = nd->y+qnt; if (nd->adj[j].movestr[1] == 'e') adj_nd->x = nd->x+qnt; else if (nd->adj[j].movestr[1] == 'w') adj_nd->x = nd->x-qnt; break; case 'e': adj_nd->x = nd->x+qnt; break; case 's': adj_nd->y = nd->y-qnt; if (nd->adj[j].movestr[1] == 'e') adj_nd->x = nd->x+qnt; else if (nd->adj[j].movestr[1] == 'w') adj_nd->x = nd->x-qnt; break; case 'w': adj_nd->x = nd->x-qnt; break; default: adj_nd->x = nd->x+qnt; adj_nd->y = nd->y+qnt; break; } } } }
int hashmap_count(hashmap* hm) { int len = 0; for (int i = 0; i < hm->used_buckets->len; ++i) { int pos = *(int*) vector_get(hm->used_buckets, i); len += vector_len(hm->pairs + pos); } return len; }
//one arg: vec cellpoint vector_2_list(void) { int i, len = vector_len(args_ref(1)); reg = NIL; for (i=len-1; i >= 0; --i){ reg = cons(vector_ref(args_ref(1), i), reg); } args_pop(1); return reg; }
//two args: vec & fill void vector_fill(void) { int i, len; reg = args_ref(1); len = vector_len(reg); for (i=0; i < len; ++i){ vector_set(reg, i, args_ref(2)); } args_pop(1); }
struct vector *copy_vector(struct vector *v) { struct vector *newp; uvalue size = vector_len(v); GCPRO1(v); newp = alloc_vector(size); memcpy(newp->data, v->data, size * sizeof(*v->data)); GCPOP(1); return newp; }
void ray_of_death_process( ray_of_death_t *self, int ticks ) { int i; coord_t delta, delta2; for(i = 0; i < 100; i++) { delta = sin(i + self->intensity*40); delta2 = cos(i + self->intensity*40); particle[i].speed.x = (delta * (-self->direction.y) + delta2 * self->direction.x) / vector_len(&self->direction); particle[i].speed.y = (delta * self->direction.x + delta2 * self->direction.y) / vector_len(&self->direction); particle_process(&particle[i], ticks ); } self->intensity -= 0.01; if (self->intensity < -10) ray_of_death_dtor(self); }
static cv_t default_handler(obj_t cont, obj_t values) { /* * For all non-message-irritants-who conditions, * print the condition's class. * For all message conditions, print the condition's message. * If who or irritants given, print (cons who irritants). */ assert(is_cont(cont)); EVAL_LOG("cont=%O values=%O", cont, values); obj_t ex = CAR(values); obj_t parts = record_get_field(ex, 0); const char *psn = program_short_name(); size_t psnl = strlen(psn); size_t i, size = vector_len(parts); for (i = 0; i < size; i++) { obj_t rtd = record_rtd(vector_ref(parts, i)); if (rtd != message && rtd != irritants && rtd != who) { ofprintf(stderr, "%*s: %O\n", (int)psnl, psn, rtd_name(rtd)); psn = ""; } } obj_t who_p = FALSE_OBJ; obj_t irr_p = make_uninitialized(); for (i = 0; i < size; i++) { obj_t p = vector_ref(parts, i); obj_t rtd = record_rtd(p); if (rtd == message) { obj_t msg = record_get_field(p, 0); const wchar_t *chars = string_value(msg); fprintf(stderr, "%*s %ls\n", (int)psnl, psn, chars); psn = ""; } else if (rtd == who) who_p = record_get_field(p, 0); else if (rtd == irritants) irr_p = record_get_field(p, 0); } if (who_p != FALSE_OBJ && !is_uninitialized(irr_p)) { ofprintf(stderr, "%*s %O\n", (int)psnl, psn, CONS(who_p, irr_p)); psn = ""; } else if (who_p != FALSE_OBJ) { ofprintf(stderr, "%*s %O\n", (int)psnl, psn, who_p); psn = ""; } else if (!is_uninitialized(irr_p)) { ofprintf(stderr, "%*s %O\n", (int)psnl, psn, irr_p); psn = ""; } if (*psn) fprintf(stderr, "%s: unknown exception\n", psn); ofprintf(stderr, "\n"); return cv(EMPTY_LIST, CONS(make_uninitialized(), EMPTY_LIST)); }
t_vect3d normalizator(t_vect3d vector1) { t_vect3d ret; double len; len = vector_len(vector1); // if (len == 0) // { // write(1, "pasniark\n", 9); // // exit(0); // } ret.x = vector1.x / len; ret.y = vector1.y / len; ret.z = vector1.z / len; return (ret); }
mapnode_t * node_at_coords(int parmx, int parmy) { int x, y, ndx, ndy, i; int sz = rect_len+1;// len = vector_len(nodelist); mapnode_t *nd; /* handle multiple nodes at the same position (return next in list) */ i = vector_find((vector_t *)nodelist, selected_node) + 1; for (; i < vector_len((vector_t *)nodelist); i++) { nd = vector_aref((vector_t *)nodelist, i); if (!nd) continue; ndx = nd->x; ndy = nd->y; for (x = -sz; x <= sz; x++) { for (y = -sz; y <= sz; y++) { if ((ndx + x + xoffset == parmx) && (ndy + y + yoffset == parmy)) { if ((selected_node != NULL) && (selected_node == nd)) goto skip; else return nd; } } } skip: continue; } if ((selected_node != NULL)) { selected_node = NULL; return node_at_coords(parmx, parmy); } return NULL; }
vector_t * load_file(char *fname) { int i, sel_id, lineno = 0, ln = 1, tot = 0, prcnt = 0; int xpad = 0, ypad = 0; FILE *file; char buf[65536]; mapnode_t *nd, *cur_node; /* setup variables */ file = fopen(fname, "r"); if (!file) ERROR_EXIT(); if (selected_node) sel_id = selected_node->id; else sel_id = 0; if (!nodelist) nodelist = new_mapnode_list((vector_t *)nodelist); /* Get the total number of bytes (for percentage loaded display) */ while (fgets(buf, sizeof(buf), file)) ln += strlen(buf); tot = ln; ln = 1; rewind(file); /* read lines until EOF, setting values as specified */ while (fgets(buf, sizeof(buf), file)) { lineno++; if (*buf == '[') { if (!isdigit(buf[1])) { allegro_message("parse error:%s:%d:\n%s", fname, lineno, buf); if (fclose(file) == EOF) allegro_message("error closing file: %s", strerror(errno)); return (vector_t *)nodelist; } cur_node = lookup_node_by_id(atoi(&buf[1])); if (cur_node) continue; cur_node = mapnode_new(); cur_node->id = atoi(&buf[1]); vector_push((vector_t *)nodelist, (void*)cur_node); } /* parse this line and assign a value, if recognized */ string_to_value(buf, cur_node); /* Display the current completion percentage */ ln += strlen(buf); prcnt = (int)(100 / (tot / (float)ln)); textprintf_ex(screen, font, 2, 2, 255, 0, "Loading: %d%%", prcnt); } /* ensure min_id and max_id are updated/accurate */ for (i = 0; i < vector_len((vector_t *)nodelist); i++) { pad_coords(vector_aref((vector_t *)nodelist, i), &xpad, &ypad); cur_node = vector_aref((vector_t *)nodelist, i); if (cur_node->id > max_id) max_id = cur_node->id; if (cur_node->id < min_id) min_id = cur_node->id; } /* if there was previously a highlighted node, highlight any node w/ the same ID value as the old one (if the new list has such an ID) */ if (sel_id) selected_node = lookup_node_by_id(sel_id); if (fclose(file) == EOF) allegro_message("error closing file: %s", strerror(errno)); if (reconstruct_node_coords) { reconstruct_coordinates((vector_t *)nodelist); } strncpy(last_file, fname, sizeof(last_file)); return (vector_t *)nodelist; }
int render_image(BITMAP *canvas) { int color = 0, linecolor = 55; int i, j, len, sz = rect_len; int minx, miny, maxx, maxy; mapnode_t *nd; minx = 0 - xoffset; miny = 0 - yoffset; maxx = canvas->w - xoffset; maxy = canvas->h - yoffset; scare_mouse(); if (!canvas) canvas = backbuffer; if (!canvas) ERROR_EXIT(); clear_to_color(canvas, bgc); len = vector_len((vector_t *)nodelist); for (i = 0; i < len; i++) { nd = vector_aref((vector_t *)nodelist, i); if (!nd) continue; /* ensure the node is in current screen bounds */ if ((nd->x < minx) || (nd->y < miny)) continue; else if ((nd->x > maxx) || (nd->y > maxy)) continue; /* draw the connections to adjacent rooms */ for (j = 0; nd->adj[j].nodeptr != NULL; j++) { /* restrict lines drawn to those ending in on-screen nodes only */ if (nd->adj[j].nodeptr->x < minx) continue; else if (nd->adj[j].nodeptr->y < miny) continue; else if (nd->adj[j].nodeptr->x > maxx) continue; else if (nd->adj[j].nodeptr->y > maxy) continue; line(canvas, nd->x+xoffset, nd->y+yoffset, nd->adj[j].nodeptr->x+xoffset, nd->adj[j].nodeptr->y+yoffset, linecolor); } } /* draw the rectangles representing each on-screen node */ for (i = 0; i < len; i++) { nd = vector_aref((vector_t *)nodelist, i); if (!nd) continue; else if ((nd->x < minx) || (nd->y < miny)) continue; else if ((nd->x > maxx) || (nd->y > maxy)) continue; rectfill(canvas, nd->x-sz+xoffset, nd->y-sz+yoffset, nd->x+sz+xoffset, nd->y+sz+yoffset, color); } /* make sure the selected node always has all adjacent rooms drawn as connections */ if (selected_node) { nd = selected_node; for (j = 0; nd->adj[j].nodeptr != NULL; j++) { line(canvas, nd->x+xoffset, nd->y+yoffset, nd->adj[j].nodeptr->x+xoffset, nd->adj[j].nodeptr->y+yoffset, highlight_color); } } render_info(canvas); return 0; }
void render_info(BITMAP *bmp) { char buf[256]; scare_mouse(); rectfill(bmp, 0, bmp->h - 16, bmp->w/2, bmp->h, bgc); rectfill(bmp, bmp->w/3, 0, (int)bmp->w*.66, 10, bgc); if (selected_node) { snprintf(buf, sizeof(buf), "%s: %s (ID: %d coord: %d,%d)", (selected_node->area?selected_node->area:"(unknown area)"), (selected_node->title?selected_node->title:"(unknown title)"), selected_node->id, selected_node->x, selected_node->y); textprintf_centre_ex(bmp, font, bmp->w/2, 2, 0, -1, "%s", buf); hline(bmp, bmp->w/2 - text_length(font, buf)/2, 10, bmp->w/2 + text_length(font, buf)/2, 0); highlight_node(selected_node, highlight_color); } textprintf_ex(bmp, font, 2, (bmp->h - 8) - 2 - text_height(font), 0, -1, "Center: %d,%d", (int)(bmp->w/2.0+xoffset), (int)(bmp->h/2.0+yoffset)); textprintf_ex(bmp, font, 2, bmp->h - 8, 0, -1, "Mouse: %d,%d", mouse_x+xoffset, mouse_y+yoffset); if (last_file) { snprintf(buf, sizeof(buf), "Most recent file: \"%s\" (%d nodes total)", last_file, vector_len((vector_t *)nodelist)); rectfill(bmp, text_length(font, buf), bmp->h - text_height(font), bmp->w, bmp->h, bgc); textprintf_right_ex(bmp, font, bmp->w, bmp->h-10, 0, -1, "%s", buf); } unscare_mouse(); }
int source_cloc(const source_t* source) { return (int)vector_len(source->lines); }
bool vector_is_empty(const vector_t *v) { assert(v); return vector_len(v) == 0; }