void PlayerInst::drop_item(GameState* gs, const GameAction& action) { ItemSlot& itemslot = inventory().get(action.use_id); int dropx = round_to_multiple(x, TILE_SIZE, true), dropy = round_to_multiple(y, TILE_SIZE, true); int amnt = itemslot.amount(); bool already_item_here = gs->object_radius_test(dropx, dropy, ItemInst::RADIUS, item_colfilter); if (!already_item_here) { gs->add_instance(new ItemInst(itemslot.item, Pos(dropx, dropy), id)); itemslot.clear(); } if (this->local) { gs->game_hud().reset_slot_selected(); } }
void test_int_helpers () { std::cout << "\ntest_int_helpers\n"; // ispow2 for (int i = 1; i < (1<<30); i *= 2) { OIIO_CHECK_ASSERT (ispow2(i)); if (i > 1) OIIO_CHECK_ASSERT (! ispow2(i+1)); } OIIO_CHECK_ASSERT (ispow2(int(0))); OIIO_CHECK_ASSERT (! ispow2(-1)); OIIO_CHECK_ASSERT (! ispow2(-2)); // ispow2, try size_t, which is unsigned for (size_t i = 1; i < (1<<30); i *= 2) { OIIO_CHECK_ASSERT (ispow2(i)); if (i > 1) OIIO_CHECK_ASSERT (! ispow2(i+1)); } OIIO_CHECK_ASSERT (ispow2((unsigned int)0)); // pow2roundup OIIO_CHECK_EQUAL (pow2roundup(4), 4); OIIO_CHECK_EQUAL (pow2roundup(5), 8); OIIO_CHECK_EQUAL (pow2roundup(6), 8); OIIO_CHECK_EQUAL (pow2roundup(7), 8); OIIO_CHECK_EQUAL (pow2roundup(8), 8); // pow2rounddown OIIO_CHECK_EQUAL (pow2rounddown(4), 4); OIIO_CHECK_EQUAL (pow2rounddown(5), 4); OIIO_CHECK_EQUAL (pow2rounddown(6), 4); OIIO_CHECK_EQUAL (pow2rounddown(7), 4); OIIO_CHECK_EQUAL (pow2rounddown(8), 8); // round_to_multiple OIIO_CHECK_EQUAL (round_to_multiple(1, 5), 5); OIIO_CHECK_EQUAL (round_to_multiple(2, 5), 5); OIIO_CHECK_EQUAL (round_to_multiple(3, 5), 5); OIIO_CHECK_EQUAL (round_to_multiple(4, 5), 5); OIIO_CHECK_EQUAL (round_to_multiple(5, 5), 5); OIIO_CHECK_EQUAL (round_to_multiple(6, 5), 10); // round_to_multiple_of_pow2 OIIO_CHECK_EQUAL (round_to_multiple_of_pow2(int(1), 4), 4); OIIO_CHECK_EQUAL (round_to_multiple_of_pow2(int(2), 4), 4); OIIO_CHECK_EQUAL (round_to_multiple_of_pow2(int(3), 4), 4); OIIO_CHECK_EQUAL (round_to_multiple_of_pow2(int(4), 4), 4); OIIO_CHECK_EQUAL (round_to_multiple_of_pow2(int(5), 4), 8); // round_to_multiple_of_pow2 OIIO_CHECK_EQUAL (round_to_multiple_of_pow2(size_t(1), size_t(4)), 4); OIIO_CHECK_EQUAL (round_to_multiple_of_pow2(size_t(2), size_t(4)), 4); OIIO_CHECK_EQUAL (round_to_multiple_of_pow2(size_t(3), size_t(4)), 4); OIIO_CHECK_EQUAL (round_to_multiple_of_pow2(size_t(4), size_t(4)), 4); OIIO_CHECK_EQUAL (round_to_multiple_of_pow2(size_t(5), size_t(4)), 8); }
Pos round_to_multiple(const Pos& pos, int mult, bool centered) { return Pos(round_to_multiple(pos.x, mult, centered), round_to_multiple(pos.y, mult, centered)); }
bool OpenEXRInput::read_native_deep_tiles (int xbegin, int xend, int ybegin, int yend, int zbegin, int zend, int chbegin, int chend, DeepData &deepdata) { if (m_deep_tiled_input_part == NULL) { error ("called OpenEXRInput::read_native_deep_tiles without an open file"); return false; } #ifdef USE_OPENEXR_VERSION2 try { const PartInfo &part (m_parts[m_subimage]); size_t width = (xend - xbegin); size_t npixels = width * (yend - ybegin) * (zend - zbegin); chend = clamp (chend, chbegin+1, m_spec.nchannels); int nchans = chend - chbegin; // Set up the count and pointers arrays and the Imf framebuffer std::vector<TypeDesc> channeltypes; m_spec.get_channelformats (channeltypes); deepdata.init (npixels, nchans, &channeltypes[chbegin], &channeltypes[chend]); Imf::DeepFrameBuffer frameBuffer; Imf::Slice countslice (Imf::UINT, (char *)(&deepdata.nsamples[0] - xbegin - ybegin*width), sizeof(unsigned int), sizeof(unsigned int) * width); frameBuffer.insertSampleCountSlice (countslice); for (int c = chbegin; c < chend; ++c) { Imf::DeepSlice slice (part.pixeltype[c], (char *)(&deepdata.pointers[c-chbegin] - xbegin*nchans - ybegin*width*nchans), sizeof(void*) * nchans, // xstride of pointer array sizeof(void*) * nchans*width, // ystride of pointer array part.chanbytes[c]); // stride of data sample frameBuffer.insert (m_spec.channelnames[c].c_str(), slice); } m_deep_tiled_input_part->setFrameBuffer (frameBuffer); int xtiles = round_to_multiple (xend-xbegin, m_spec.tile_width) / m_spec.tile_width; int ytiles = round_to_multiple (yend-ybegin, m_spec.tile_height) / m_spec.tile_height; // Get the sample counts for each pixel and compute the total // number of samples and resize the data area appropriately. m_deep_tiled_input_part->readPixelSampleCounts (0, xtiles-1, 0, ytiles-1); deepdata.alloc (); // Read the pixels m_deep_tiled_input_part->readTiles (0, xtiles-1, 0, ytiles-1, m_miplevel, m_miplevel); } catch (const std::exception &e) { error ("Failed OpenEXR read: %s", e.what()); return false; } return true; #else return false; #endif }
bool OpenEXRInput::read_native_deep_tiles (int xbegin, int xend, int ybegin, int yend, int zbegin, int zend, int chbegin, int chend, DeepData &deepdata) { if (m_deep_tiled_input_part == NULL) { error ("called OpenEXRInput::read_native_deep_tiles without an open file"); return false; } try { const PartInfo &part (m_parts[m_subimage]); size_t width = (xend - xbegin); size_t npixels = width * (yend - ybegin) * (zend - zbegin); chend = clamp (chend, chbegin+1, m_spec.nchannels); int nchans = chend - chbegin; // Set up the count and pointers arrays and the Imf framebuffer std::vector<TypeDesc> channeltypes; m_spec.get_channelformats (channeltypes); deepdata.init (npixels, nchans, array_view<const TypeDesc>(&channeltypes[chbegin], chend-chbegin), spec().channelnames); std::vector<unsigned int> all_samples (npixels); std::vector<void*> pointerbuf (npixels * nchans); Imf::DeepFrameBuffer frameBuffer; Imf::Slice countslice (Imf::UINT, (char *)(&all_samples[0] - xbegin - ybegin*width), sizeof(unsigned int), sizeof(unsigned int) * width); frameBuffer.insertSampleCountSlice (countslice); for (int c = chbegin; c < chend; ++c) { Imf::DeepSlice slice (part.pixeltype[c], (char *)(&pointerbuf[0]+(c-chbegin) - xbegin*nchans - ybegin*width*nchans), sizeof(void*) * nchans, // xstride of pointer array sizeof(void*) * nchans*width, // ystride of pointer array deepdata.samplesize()); // stride of data sample frameBuffer.insert (m_spec.channelnames[c].c_str(), slice); } m_deep_tiled_input_part->setFrameBuffer (frameBuffer); int xtiles = round_to_multiple (xend-xbegin, m_spec.tile_width) / m_spec.tile_width; int ytiles = round_to_multiple (yend-ybegin, m_spec.tile_height) / m_spec.tile_height; int firstxtile = (xbegin - m_spec.x) / m_spec.tile_width; int firstytile = (ybegin - m_spec.y) / m_spec.tile_height; // Get the sample counts for each pixel and compute the total // number of samples and resize the data area appropriately. m_deep_tiled_input_part->readPixelSampleCounts ( firstxtile, firstxtile+xtiles-1, firstytile, firstytile+ytiles-1); deepdata.set_all_samples (all_samples); deepdata.get_pointers (pointerbuf); // Read the pixels m_deep_tiled_input_part->readTiles ( firstxtile, firstxtile+xtiles-1, firstytile, firstytile+ytiles-1, m_miplevel, m_miplevel); } catch (const std::exception &e) { error ("Failed OpenEXR read: %s", e.what()); return false; } catch (...) { // catch-all for edge cases or compiler bugs error ("Failed OpenEXR read: unknown exception"); return false; } return true; }
bool feature_exists_near(GameState* gs, const Pos& p) { Pos midx = round_to_multiple(p, TILE_SIZE, true); return gs->object_radius_test(midx.x, midx.y, 4); }