void read_inventory_list(lua_State *L, int tableindex, Inventory *inv, const char *name, Server* srv, int forcesize) { if(tableindex < 0) tableindex = lua_gettop(L) + 1 + tableindex; // If nil, delete list if(lua_isnil(L, tableindex)){ inv->deleteList(name); return; } // Otherwise set list std::vector<ItemStack> items = read_items(L, tableindex,srv); int listsize = (forcesize != -1) ? forcesize : items.size(); InventoryList *invlist = inv->addList(name, listsize); int index = 0; for(std::vector<ItemStack>::const_iterator i = items.begin(); i != items.end(); i++){ if(forcesize != -1 && index == forcesize) break; invlist->changeItem(index, *i); index++; } while(forcesize != -1 && index < forcesize){ invlist->deleteItem(index); index++; } }
static int a_readmuseumgiveback(attrib * a, void *owner, struct gamedata *data) { museumgiveback *gb = (museumgiveback *)a->data.v; READ_INT(data->store, &gb->cookie); read_items(data->store, &gb->items); return AT_READ_OK; }
/*! * \brief Parse _DiaObject from the given node * Fill a GList* with objects which is to be put in a * diagram or a group by the caller. * Can be called recursively to allow groups in groups. * This is only using the render branch of the file, if the * object type is not registered with Dia. Otherwise the objects * are just created from their type and properties. * \ingroup DiaRenderScriptImport */ static GList* read_items (xmlNodePtr startnode, DiaContext *ctx) { xmlNodePtr node; GList *items = NULL; for (node = startnode; node != NULL; node = node->next) { if (xmlIsBlankNode(node)) continue; if (node->type != XML_ELEMENT_NODE) continue; if (!xmlStrcmp(node->name, (const xmlChar *)"object")) { xmlChar *sType = xmlGetProp(node, (const xmlChar *)"type"); const DiaObjectType *ot = object_get_type ((gchar *)sType); xmlNodePtr props = NULL, render = NULL; props = find_child_named (node, "properties"); render = find_child_named (node, "render"); if (ot && !ot->ops) { GList *moreitems; /* FIXME: 'render' is also the grouping element */ moreitems = read_items (render->children, ctx); if (moreitems) { DiaObject *group = group_create (moreitems); /* apply group props, e.g. transform */ object_load_props (group, props, ctx); /* group eats list */ items = g_list_append (items, group); } } else if (ot) { Point startpoint = {0.0,0.0}; Handle *handle1,*handle2; DiaObject *o; o = ot->ops->create(&startpoint, ot->default_user_data, &handle1,&handle2); if (o) { object_load_props (o, props, ctx); items = g_list_append (items, o); } } else if (render) { DiaObject *o = _render_object (render, ctx); if (o) items = g_list_append (items, o); } else { g_debug ("DRS-Import: %s?", node->name); } } else { g_debug ("DRS-Import: %s?", node->name); } } return items; }
int main(void) { int test0[] = {81, 88, 75, 42, 87, 84, 86, 65}; int test1[] = {157, 150, 164, 119, 79, 159, 161, 139, 158}; int r0 = special_sum(8, test0); int r1 = special_sum(9, test1); if(r0 == -1) { printf("test0 is not special\n"); } else { printf("test0 is special, with S(_) == %d\n", r0); } if(r1 == -1) { printf("test1 is not special\n"); } else { printf("test1 is special, with S(_) == %d\n", r1); } FILE *fp = fopen("sets.txt", "r"); char buf[1024]; int sum = 0; int special_count = 0; int count = 0; while(NULL != fgets(buf, sizeof(buf), fp)) { count++; int *items = malloc(12*sizeof(int)); int nitems = 0; read_items(buf, &nitems, items); printf("read set: {%d", items[0]); int z; for(z = 1; z < nitems; z++) { printf(", %d", items[z]); } printf("}"); int result = special_sum(nitems, items); if(result) { printf(" (special, sum is %d)\n", result); special_count++; sum += result; } else { printf(" (not special)\n"); } } printf("%d / %d are special\n", special_count, count); printf("sum of special sets: %d\n", sum); }
void read_db() { #ifdef DEBUG_VALUE //char cur_dir[256]; //GetCurrentDirectory(256, cur_dir); //printf("in read_db(): %s\n", cur_dir); #endif GLint charsRead = read_chars(); GLint mapsRead = read_maps(); GLint itemsRead = read_items(); if (charsRead || mapsRead || itemsRead) { exit_glut("Error reading database!"); } }
/* imports the given SVG file, returns TRUE if successful */ gboolean import_drs (const gchar *filename, DiagramData *dia, void* user_data) { GList *item, *items; xmlDocPtr doc = xmlParseFile(filename); items = read_items (doc->xmlChildrenNode); for (item = items; item != NULL; item = g_list_next (item)) { DiaObject *obj = (DiaObject *)item->data; layer_add_object(dia->active_layer, obj); } g_list_free (items); xmlFreeDoc(doc); return TRUE; }
// get_craft_result(input) int ModApiCraft::l_get_craft_result(lua_State *L) { NO_MAP_LOCK_REQUIRED; int input_i = 1; std::string method_s = getstringfield_default(L, input_i, "method", "normal"); enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method", es_CraftMethod, CRAFT_METHOD_NORMAL); int width = 1; lua_getfield(L, input_i, "width"); if(lua_isnumber(L, -1)) width = luaL_checkinteger(L, -1); lua_pop(L, 1); lua_getfield(L, input_i, "items"); std::vector<ItemStack> items = read_items(L, -1,getServer(L)); lua_pop(L, 1); // items IGameDef *gdef = getServer(L); ICraftDefManager *cdef = gdef->cdef(); CraftInput input(method, width, items); CraftOutput output; std::vector<ItemStack> output_replacements; bool got = cdef->getCraftResult(input, output, output_replacements, true, gdef); lua_newtable(L); // output table if (got) { ItemStack item; item.deSerialize(output.item, gdef->idef()); LuaItemStack::create(L, item); lua_setfield(L, -2, "item"); setintfield(L, -1, "time", output.time); push_items(L, output_replacements); lua_setfield(L, -2, "replacements"); } else { LuaItemStack::create(L, ItemStack()); lua_setfield(L, -2, "item"); setintfield(L, -1, "time", 0); lua_newtable(L); lua_setfield(L, -2, "replacements"); } lua_newtable(L); // decremented input table lua_pushstring(L, method_s.c_str()); lua_setfield(L, -2, "method"); lua_pushinteger(L, width); lua_setfield(L, -2, "width"); push_items(L, input.items); lua_setfield(L, -2, "items"); return 2; }
/* imports the given DRS file, returns TRUE if successful */ gboolean import_drs (const gchar *filename, DiagramData *dia, DiaContext *ctx, void* user_data) { GList *item, *items; xmlDocPtr doc = xmlParseFile(filename); xmlNodePtr root = NULL, node; Layer *active_layer = NULL; for (node = doc->children; node; node = node->next) if (xmlStrcmp (node->name, (const xmlChar *)"drs") == 0) root = node; if (!root || !(root = find_child_named (root, "diagram"))) { dia_context_add_message (ctx, _("Broken file?")); return FALSE; } for (node = root->children; node != NULL; node = node->next) { if (xmlStrcmp (node->name, (const xmlChar *)"layer") == 0) { xmlChar *str; xmlChar *name = xmlGetProp (node, (const xmlChar *)"name"); Layer *layer = new_layer (g_strdup (name ? (gchar *)name : _("Layer")), dia); if (name) xmlFree (name); str = xmlGetProp (node, (const xmlChar *)"active"); if (xmlStrcmp (str, (const xmlChar *)"true")) { active_layer = layer; xmlFree (str); } items = read_items (node->children, ctx); for (item = items; item != NULL; item = g_list_next (item)) { DiaObject *obj = (DiaObject *)item->data; layer_add_object(layer, obj); } g_list_free (items); data_add_layer (dia, layer); } } if (active_layer) data_set_active_layer (dia, active_layer); xmlFreeDoc(doc); return TRUE; }
int main() { FILE *f; GPtrArray *examinees; OscatsSpace *contSpace, *binSpace; OscatsItemBank *bank; OscatsExaminee *e; OscatsDim dim; OscatsPoint *initTheta, *initAlpha; const guint num_tests = 4; const gchar *test_names[] = { "random", "FI", "KL.3PL", "KL.DINA" }; OscatsTest *test[num_tests]; OscatsAlgExposureCounter *exposure[num_tests]; OscatsAlgClassRates *rates[num_tests]; guint i, j, k; g_type_init(); g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL); // For debugging /* In the previous examples, we had only one latent space; but, here we * want to use two different models from *different* latent spaces. The * first latent space is a unidimensional continuous space (for the 3PL * model); the second is an N_ATTR-dimensional binary space (for the DINA * model). If appropriate to the simulation (e.g. to compare 2PL vs. 3PL) * we could have created two different models from the same space. * * Note: Even though OscatsSpace accommodates mixed-type (continuous and * binary) latent spaces, the models we use here are either only * continuous or only binary, so they come from different latent spaces. * A mixed-type model would be something like the Fusion model, which has * both binary and continuous components. */ contSpace = g_object_new(OSCATS_TYPE_SPACE, "numCont", 1, NULL); binSpace = g_object_new(OSCATS_TYPE_SPACE, "numBin", N_ATTR, NULL); initTheta = oscats_point_new_from_space(contSpace); initAlpha = oscats_point_new_from_space(binSpace); printf("Reading examinees.\n"); f = fopen("ex03-person.dat", "r"); examinees = read_examinees(f, contSpace, binSpace); fclose(f); printf("Reading items.\n"); f = fopen("ex03-items.dat", "r"); bank = read_items(f, contSpace, binSpace); fclose(f); printf("Creating tests.\n"); for (j=0; j < num_tests; j++) { // Create a new test with the given properties // Be sure to end calls to g_object_new() with NULL! test[j] = g_object_new(OSCATS_TYPE_TEST, "id", test_names[j], "itembank", bank, "length_hint", LEN, NULL); // Register the common CAT algorithms for this test. // Here, we always simulate from the DINA model. oscats_algorithm_register( g_object_new(OSCATS_TYPE_ALG_SIMULATE, "modelKey", "DINA", "thetaKey", "trueAlpha", NULL), test[j]); // Usually we would only have one estimate algorithm, but here, we need // to estimate both the continuous ability and the latent class, each // based on a separate model. oscats_algorithm_register( g_object_new(OSCATS_TYPE_ALG_ESTIMATE, "modelKey", "3PL", "thetaKey", "estTheta", NULL), test[j]); oscats_algorithm_register( g_object_new(OSCATS_TYPE_ALG_ESTIMATE, "modelKey", "DINA", "thetaKey", "estAlpha", NULL), test[j]); exposure[j] = oscats_algorithm_register(g_object_new(OSCATS_TYPE_ALG_EXPOSURE_COUNTER, NULL), test[j]); g_object_ref(exposure[j]); rates[j] = oscats_algorithm_register( g_object_new(OSCATS_TYPE_ALG_CLASS_RATES, "simKey", "trueAlpha", "estKey", "estAlpha", NULL), test[j]); g_object_ref(rates[j]); oscats_algorithm_register(g_object_new(OSCATS_TYPE_ALG_FIXED_LENGTH, "len", LEN, NULL), test[j]); } // Register the item selection criteria for the different tests: // Since we aren't just using the default model for everything, we have to // tell each algorithm which model/theta to use for selection. oscats_algorithm_register(g_object_new(OSCATS_TYPE_ALG_PICK_RAND, NULL), test[0]); oscats_algorithm_register( g_object_new(OSCATS_TYPE_ALG_MAX_FISHER, "num", 5, "modelKey", "3PL", "thetaKey", "estTheta", NULL), test[1]); oscats_algorithm_register( g_object_new(OSCATS_TYPE_ALG_MAX_KL, "num", 5, "modelKey", "3PL", "thetaKey", "estTheta", NULL), test[2]); oscats_algorithm_register( g_object_new(OSCATS_TYPE_ALG_MAX_KL, "num", 5, "modelKey", "DINA", "thetaKey", "estAlpha", NULL), test[3]); printf("Administering.\n"); f = fopen("ex03-person-results.dat", "w"); fprintf(f, "ID\ttrue.theta\ttrue.alpha\tinit.theta\tinit.alpha"); for (j=0; j < num_tests; j++) fprintf(f, "\t%s.theta\t%s.alpha", test_names[j], test_names[j]); fprintf(f, "\n"); for (i=0; i < N_EXAMINEES; i++) { OscatsPoint *tmp; e = g_ptr_array_index(examinees, i); tmp = oscats_examinee_get_theta_by_name(e, "trueTheta"); dim = OSCATS_DIM_CONT+0; // first continuous dimension fprintf(f, "%s\t%g\t", e->id, oscats_point_get_cont(tmp, dim)); tmp = oscats_examinee_get_theta_by_name(e, "trueAlpha"); dim = OSCATS_DIM_BIN+0; // first binary dimension for (k=0; k < N_ATTR; k++, dim++) fprintf(f, oscats_point_get_bin(tmp, dim) ? "1" : "0"); // Choose initial theta and alpha for this examinee // We'll use the same initial abilities for all four tests dim = OSCATS_DIM_CONT+0; // first continuous dimension oscats_point_set_cont(initTheta, dim, oscats_rnd_normal(sqrt(0.05))-0.37); dim = OSCATS_DIM_BIN+0; // first binary dimension for (k=0; k < N_ATTR; k++, dim++) oscats_point_set_bin(initAlpha, dim, oscats_rnd_uniform() < 0.5); // Initialize ability estimates for this examinee // (Examinee takes references for the new OscatsPoint objects) oscats_examinee_set_theta_by_name(e, "estTheta", oscats_point_new_from_space(contSpace)); oscats_examinee_set_theta_by_name(e, "estAlpha", oscats_point_new_from_space(binSpace)); for (j=0; j < num_tests; j++) { // Reset initial latent ability for this test oscats_point_copy( oscats_examinee_get_theta_by_name(e, "estTheta"), initTheta); oscats_point_copy( oscats_examinee_get_theta_by_name(e, "estAlpha"), initAlpha); // Do the administration! oscats_test_administer(test[j], e); // Output the resulting theta.hat and alpha.hat tmp = oscats_examinee_get_theta_by_name(e, "estTheta"); dim = OSCATS_DIM_CONT+0; // first continuous dimension fprintf(f, "\t%g\t", oscats_point_get_cont(tmp, dim)); tmp = oscats_examinee_get_theta_by_name(e, "estAlpha"); dim = OSCATS_DIM_BIN+0; // first binary dimension for (k=0; k < N_ATTR; k++, dim++) fprintf(f, oscats_point_get_bin(tmp, dim) ? "1" : "0"); } fprintf(f, "\n"); } fclose(f); f = fopen("ex03-exposure.dat", "w"); fprintf(f, "ID"); for (j=0; j < num_tests; j++) fprintf(f, "\t%s", test_names[j]); fprintf(f, "\n"); for (i=0; i < N_ITEMS; i++) { OscatsItem *item = OSCATS_ITEM(oscats_item_bank_get_item(bank, i)); fprintf(f, "%d", i+1); // Get the exposure rate for this item in each test for (j=0; j < num_tests; j++) fprintf(f, "\t%g", oscats_alg_exposure_counter_get_rate(exposure[j], item)); fprintf(f, "\n"); } fclose(f); f = fopen("ex03-rates.dat", "w"); fprintf(f, "Rate"); for (j=0; j < num_tests; j++) fprintf(f, "\t%s", test_names[j]); fprintf(f, "\nPattern"); for (j=0; j < num_tests; j++) fprintf(f, "\t%g", oscats_alg_class_rates_get_pattern_rate(rates[j])); for (k=0; k < N_ATTR; k++) { fprintf(f, "\nattr.%d", k+1); for (j=0; j < num_tests; j++) fprintf(f, "\t%g", oscats_alg_class_rates_get_attribute_rate(rates[j], k)); } for (k=0; k <= N_ATTR; k++) { fprintf(f, "\nfreq.%d", k); for (j=0; j < num_tests; j++) fprintf(f, "\t%g", oscats_alg_class_rates_get_misclassify_freq(rates[j], k)); } fprintf(f, "\n"); fclose(f); // Clean up printf("Done.\n"); for (j=0; j < num_tests; j++) { g_object_unref(exposure[j]); g_object_unref(rates[j]); g_object_unref(test[j]); } g_object_unref(bank); g_ptr_array_free(examinees, TRUE); g_object_unref(initTheta); g_object_unref(initAlpha); g_object_unref(contSpace); g_object_unref(binSpace); return 0; }