// 设备初始化,fb为外部帧缓存,非 NULL 将引用外部帧缓存(每行 4字节对齐) void device_init(device_t *device, int width, int height, void *fb) { int need = sizeof(void*)* (height * 2 + 1024) + width * height * 8; char *ptr = (char*)malloc(need + 64); char *framebuf, *zbuf; int j; assert(ptr); device->framebuffer = (IUINT32**)ptr; device->zbuffer = (float**)(ptr + sizeof(void*)* height); ptr += sizeof(void*)* height * 2; device->texture = (IUINT32**)ptr; ptr += sizeof(void*)* 1024; framebuf = (char*)ptr; zbuf = (char*)ptr + width * height * 4; ptr += width * height * 8; if (fb != NULL) framebuf = (char*)fb; for (j = 0; j < height; j++) { device->framebuffer[j] = (IUINT32*)(framebuf + width * 4 * j); device->zbuffer[j] = (float*)(zbuf + width * 4 * j); } device->texture[0] = (IUINT32*)ptr; device->texture[1] = (IUINT32*)(ptr + 16); memset(device->texture[0], 0, 64); device->tex_width = 2; device->tex_height = 2; device->max_u = 1.0f; device->max_v = 1.0f; device->width = width; device->height = height; device->background = 0xc0c0c0; device->foreground = 0; transform_init(&device->transform, width, height); device->render_state = RENDER_STATE_WIREFRAME; }
void kindOf_building_init(kindOf_building_t* b) { b->name = NULL; b->width = 0; b->height = 0; b->sprite = 0; b->n_sprites = 0; b->door_dx = 0; b->door_dy = 0; b->button_sprite = -1; b->button_index = -1; transform_init(&b->build); transform_init(&b->make); b->n_items = 0; b->items = NULL; }
int kindOf_building_newItem(kindOf_building_t* b) { b->items = CREALLOC(b->items, transform_t, b->n_items+1); transform_init(&b->items[b->n_items]); return b->n_items++; }
void ai_init(ai_t* ai) { ai->name = NULL; transform_init(&ai->inventory); ai->building = -1; }
char ai_get(character_t* c, char is_item, int id, float amount, char keep) { universe_t* u = c->w->universe; amount -= inventory_get(&c->inventory, is_item, id); if (amount <= 0) return 0; // gather if possible kindOf_mine_t* m = universe_mineFor(u, is_item, id); if (m != NULL) { character_goMine(c, m); return 1; } // if the current building cannot obtain the component, build one which can building_t* b = building_get(&c->w->objects, c->hasBuilding); transform_t* tr = b == NULL ? NULL : kindOf_building_canMake(b->t, is_item, id); if (tr == NULL) { // do not replace the building if (keep) { if (rand() % 60 != 0) return 1; float price = is_item ? u->items[id].price : u->materials[id].price; price *= amount; if (c->inventory.money < price) { if (c->inBuilding != c->hasBuilding) character_goto(c, c->hasBuilding); return 1; } // try and buy it building_t* b = world_findSale(c->w, c->o.x, c->o.y, is_item, id); if (b == NULL) return 1; if (c->inBuilding != b->o.uuid) { character_goto(c, b->o.uuid); return 1; } building_take(b, is_item, id, amount, &c->inventory, 0); return 1; } kindOf_building_t* b = universe_buildFor(u, is_item, id); if (b == NULL) { const char* name = is_item ? u->items[id].name : u->materials[id].name; fprintf(stderr, "%s: I do not know how to make %s\n", c->ai->name, name); return 1; } // materials to be gather before building transform_t total; transform_init(&total); // first, gather the non-base materials for the component tr = kindOf_building_canMake(b, is_item, id); for (int i = 0; i < tr->n_req; i++) { component_t* p = &tr->req[i]; if (universe_mineFor(u, p->is_item, p->id) == NULL) transform_req(&total, p->is_item, p->id, p->amount*amount); } // then, gather the materials for the building transform_add(&total, &b->build, 1); // do gather char isreq = ai_getreq(c, &total, 1, keep); transform_exit(&total); if (isreq) return 1; // build character_buildAuto(c, b); return 1; } if (ai_getreq(c, tr, amount, keep)) return 1; // go in the building if (c->inBuilding != c->hasBuilding) { character_goto(c, b->o.uuid); return 1; } // enqueue item if (is_item && b->work_n == 0) { int nth = tr - b->t->items; building_work_enqueue(b, nth); } // work return 1; }
/** * After driver.c loads a new version of a program as a shared library, it * accesses the symbol "kitsune_init_inplace" from the library and calls it, * passing in: * * 1. env - the jmp_buf to use with longjmp when updating * 2. prev_handle - the handle to the previous version shared library * 4. argc/argv - the arguments to pass on to main */ int kitsune_init_inplace(jmp_buf *env, void *prev_handle, void *cur_handle, char **next_code, const char *bench_filename, int argc, char **argv) { /* We've beguin executing code in this version. */ is_loading = 0; jmp_env = env; prev_ver_handle = prev_handle; cur_ver_handle = cur_handle; next_version_code = next_code; bench_init(bench_filename); #ifdef ENABLE_THREADING ktthread_init(); #endif /* * If the handle to the previous version was NULL, we infer that we are the * first version starting up. */ if (kitsune_is_updating()) { kitsune_has_updated_p = 1; #ifdef ENABLE_THREADING /* * Wait for all child threads to reach update points (or terminate) */ ktthread_main_wait(); bench_quiesce_finish(); #endif /* * Setup handle to the old version's stack variables that were captured from * the stackvars API/compiler. */ stackvars_flip(); addresscheck_init(); transform_init(); /* * Get the pointer to the saved static variables. */ registervars_migrate(); } /* * Initialize the log. */ if(!kitsune_logging_init(argv[0])) { printf("Couldn't initialize logging!\n"); abort(); } /* initialize the memory allocation tracker tree*/ alloctrack_init(); /* * We may wish to perform some initialization (e.g., altering the set of * threads or the argc/argv arguments to the program) before entering main(). * Here, we call such a transformer if it exists. */ if (kitsune_is_updating()) { state_xform_fn_t ps_fn = kitsune_get_cur_val("_kitsune_prestart_xform"); if (ps_fn) { kitsune_log("Calling prestart transformation function."); ps_fn(); } } /* * After saving the information passed from the driver, we invoke the main * function of current version shared library. */ bench_restart_start(); kitsune_log("Entering target program: %s\n", argv[0]); return main(argc, argv); }