void GIFFManager::load_chunk(IFFByteStream & istr, GP<GIFFChunk> chunk) { DEBUG_MSG("GIFFManager::load_chunk(): loading contents of chunk '" << chunk->get_name() << "'\n"); DEBUG_MAKE_INDENT(3); int chunk_size; GUTF8String chunk_id; while ((chunk_size=istr.get_chunk(chunk_id))) { if (istr.check_id(chunk_id)) { GP<GIFFChunk> ch=GIFFChunk::create(chunk_id); load_chunk(istr, ch); chunk->add_chunk(ch); } else { TArray<char> data(chunk_size-1); istr.get_bytestream()->readall( (char*)data, data.size()); GP<GIFFChunk> ch=GIFFChunk::create(chunk_id, data); chunk->add_chunk(ch); } istr.close_chunk(); } }
void descramble(unsigned char *fh, unsigned char *ptr, unsigned long filesz) { unsigned long chunksz; load_set(fh); my_srand(filesz); /* Descramble 2 meg blocks for as long as possible, then gradually reduce the window down to 32 bytes (1 slice) */ for(chunksz = MAXCHUNK; chunksz >= 32; chunksz >>= 1) { while(filesz >= chunksz) { load_chunk(ptr, chunksz); filesz -= chunksz; ptr += chunksz; } } /* Load final incomplete slice */ if(filesz) { load(ptr, filesz); } }
// Check to see if the resource has already been loaded // - Loaded: Return stored pointer // - Not Loaded: Load chunk from stored resource path static inline Mix_Chunk *get_sfx_resource( int resource_id ) { auto &resource = sfx_resources.resource[ resource_id ]; if( !resource.chunk ) { std::string path = ( current_soundpack_path + "/" + resource.path ); resource.chunk.reset( load_chunk( path ) ); } return resource.chunk.get(); }
int unfreeze(char *filename,char **msg) { HEADER_FREEZE hfreeze ; CHUNK_FREEZE cfreeze ; int nb ; int nb_chunks = 0 ; int nb_chunks_in_file = 0 ; int last_chunk = FALSE ; char realname8[10] ; char realname[18] ; memset(realname8,0,10) ; strncpy(realname8,filename,8) ; sprintf(realname,"%s.FRZ",realname8) ; // strupr(realname,realname) ; // fp=fopen("TRYING.FRZ","rb") ; fp = fopen(realname,"rb") ; if (fp==NULL) { *msg = msg_err_open ; return FALSE ; } *msg = msg_err_reading ; // default message now nb=fread(&hfreeze,1,sizeof(HEADER_FREEZE),fp) ; if (nb!=sizeof(HEADER_FREEZE)) return FALSE ; if (allocated_ram < (hfreeze.ramsize<<19)) { *msg = msg_err_ram ; return FALSE ; } while (!last_chunk) { nb = fread(&cfreeze,1,sizeof(CHUNK_FREEZE),fp) ; if (nb!=sizeof(CHUNK_FREEZE)) return FALSE ; if ((cfreeze.header1 == ID_END1)&&(cfreeze.header2 == ID_END2)) { last_chunk = TRUE ; fread(&nb_chunks_in_file,1,4,fp); } else if (load_chunk(&cfreeze)) nb_chunks++ ; } fclose(fp) ; sprintf(b,"%d chunks loaded in.",nb_chunks) ; *msg = b ; return TRUE ; }
void GIFFManager::load_file(GP<ByteStream> str) { DEBUG_MSG("GIFFManager::load_file(): Loading IFF file.\n"); DEBUG_MAKE_INDENT(3); GP<IFFByteStream> gistr=IFFByteStream::create(str); IFFByteStream &istr=*gistr; GUTF8String chunk_id; if (istr.get_chunk(chunk_id)) { if (chunk_id.substr(0,5) != "FORM:") G_THROW( ERR_MSG("GIFFManager.cant_find2") ); set_name(chunk_id); load_chunk(istr, top_level); istr.close_chunk(); } }
int luabash_builtin(WORD_LIST *list) { if (list && (list->word->word)) { char* command=list->word->word; if (strcmp(command, "init") == 0) { return init_luabash(); } else if (strcmp(command, "load") == 0) { list=list->next; if (list && (list->word->word)) return load_chunk(list->word->word); } else if (strcmp(command, "call") == 0) { list=list->next; if (list && (list->word->word)) return call_function(list->word->word, list->next); } } print_usage(); return EXECUTION_FAILURE; }
Region* load_region(const std::string& filename, int x, int y) { std::ifstream file; file.open(filename, std::ios::in | std::ios::binary); if(!file.is_open()) { return NULL; } Region* region = new Region(x, y); for(int chunkX = 0; chunkX < 32; ++chunkX) { for(int chunkY = 0; chunkY < 32; ++chunkY) { region->setChunk(chunkX, chunkY, load_chunk(file, chunkX, chunkY)); break; } break; } return region; }
static int load_file (FILE * fh, unsigned char *ptr, uint32_t filesz) { uint32_t chunksz; dc_srand (filesz); /* Descramble 2 meg blocks for as long as possible, then gradually reduce the window down to 32 bytes (1 slice) */ for (chunksz = MAXCHUNK; chunksz >= 32; chunksz >>= 1) while (filesz >= chunksz) { load_chunk (fh, ptr, chunksz); filesz -= chunksz; ptr += chunksz; } /* Load final incomplete slice */ if (filesz) if (fread (ptr, 1, filesz, fh) != filesz) return -1; return 0; }
/* TODO triple check this to make sure reference counting is correct */ PyObject* chunk_render(PyObject *self, PyObject *args) { RenderState state; PyObject *modeobj; PyObject *blockmap; int xoff, yoff; PyObject *imgsize, *imgsize0_py, *imgsize1_py; int imgsize0, imgsize1; PyObject *blocks_py; PyObject *left_blocks_py; PyObject *right_blocks_py; PyObject *up_left_blocks_py; PyObject *up_right_blocks_py; RenderMode *rendermode; int i, j; PyObject *t = NULL; if (!PyArg_ParseTuple(args, "OOiiiOiiOO", &state.world, &state.regionset, &state.chunkx, &state.chunky, &state.chunkz, &state.img, &xoff, &yoff, &modeobj, &state.textures)) return NULL; /* set up the render mode */ state.rendermode = rendermode = render_mode_create(modeobj, &state); if (rendermode == NULL) { return NULL; // note that render_mode_create will // set PyErr. No need to set it here } /* get the blockmap from the textures object */ blockmap = PyObject_GetAttrString(state.textures, "blockmap"); if (blockmap == NULL) { render_mode_destroy(rendermode); return NULL; } if (blockmap == Py_None) { render_mode_destroy(rendermode); PyErr_SetString(PyExc_RuntimeError, "you must call Textures.generate()"); return NULL; } /* get the image size */ imgsize = PyObject_GetAttrString(state.img, "size"); imgsize0_py = PySequence_GetItem(imgsize, 0); imgsize1_py = PySequence_GetItem(imgsize, 1); Py_DECREF(imgsize); imgsize0 = PyInt_AsLong(imgsize0_py); imgsize1 = PyInt_AsLong(imgsize1_py); Py_DECREF(imgsize0_py); Py_DECREF(imgsize1_py); /* set all block data to unloaded */ for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { state.chunks[i][j].loaded = 0; } } /* get the block data for the center column, erroring out if needed */ if (load_chunk(&state, 0, 0, 1)) { render_mode_destroy(rendermode); Py_DECREF(blockmap); return NULL; } if (state.chunks[1][1].sections[state.chunky].blocks == NULL) { /* this section doesn't exist, let's skeddadle */ render_mode_destroy(rendermode); Py_DECREF(blockmap); unload_all_chunks(&state); Py_RETURN_NONE; } /* set blocks_py, state.blocks, and state.blockdatas as convenience */ blocks_py = state.blocks = state.chunks[1][1].sections[state.chunky].blocks; state.blockdatas = state.chunks[1][1].sections[state.chunky].data; /* set up the random number generator again for each chunk so tallgrass is in the same place, no matter what mode is used */ srand(1); for (state.x = 15; state.x > -1; state.x--) { for (state.z = 0; state.z < 16; state.z++) { /* set up the render coordinates */ state.imgx = xoff + state.x*12 + state.z*12; /* 16*12 -- offset for y direction, 15*6 -- offset for x */ state.imgy = yoff - state.x*6 + state.z*6 + 16*12 + 15*6; for (state.y = 0; state.y < 16; state.y++) { unsigned char ancilData; state.imgy -= 12; /* get blockid */ state.block = getArrayShort3D(blocks_py, state.x, state.y, state.z); if (state.block == 0 || render_mode_hidden(rendermode, state.x, state.y, state.z)) { continue; } /* make sure we're rendering inside the image boundaries */ if ((state.imgx >= imgsize0 + 24) || (state.imgx <= -24)) { continue; } if ((state.imgy >= imgsize1 + 24) || (state.imgy <= -24)) { continue; } /* check for occlusion */ if (render_mode_occluded(rendermode, state.x, state.y, state.z)) { continue; } /* everything stored here will be a borrowed ref */ if (block_has_property(state.block, NODATA)) { /* block shouldn't have data associated with it, set it to 0 */ ancilData = 0; state.block_data = 0; state.block_pdata = 0; } else { /* block has associated data, use it */ ancilData = getArrayByte3D(state.blockdatas, state.x, state.y, state.z); state.block_data = ancilData; /* block that need pseudo ancildata: * grass, water, glass, chest, restone wire, * ice, fence, portal, iron bars, glass panes */ if ((state.block == 2) || (state.block == 9) || (state.block == 20) || (state.block == 54) || (state.block == 55) || (state.block == 64) || (state.block == 71) || (state.block == 79) || (state.block == 85) || (state.block == 90) || (state.block == 101) || (state.block == 102) || (state.block == 113) || (state.block == 139)) { ancilData = generate_pseudo_data(&state, ancilData); state.block_pdata = ancilData; } else { state.block_pdata = 0; } } /* make sure our block info is in-bounds */ if (state.block >= max_blockid || ancilData >= max_data) continue; /* get the texture */ t = PyList_GET_ITEM(blockmap, max_data * state.block + ancilData); /* if we don't get a texture, try it again with 0 data */ if ((t == NULL || t == Py_None) && ancilData != 0) t = PyList_GET_ITEM(blockmap, max_data * state.block); /* if we found a proper texture, render it! */ if (t != NULL && t != Py_None) { PyObject *src, *mask, *mask_light; int randx = 0, randy = 0; src = PyTuple_GetItem(t, 0); mask = PyTuple_GetItem(t, 0); mask_light = PyTuple_GetItem(t, 1); if (mask == Py_None) mask = src; if (state.block == 31) { /* add a random offset to the postion of the tall grass to make it more wild */ randx = rand() % 6 + 1 - 3; randy = rand() % 6 + 1 - 3; state.imgx += randx; state.imgy += randy; } render_mode_draw(rendermode, src, mask, mask_light); if (state.block == 31) { /* undo the random offsets */ state.imgx -= randx; state.imgy -= randy; } } } } } /* free up the rendermode info */ render_mode_destroy(rendermode); Py_DECREF(blockmap); unload_all_chunks(&state); Py_RETURN_NONE; }
void play_sound_internal(const std::string& files, channel_group group, unsigned int repeats, unsigned int distance, int id, int loop_ticks, int fadein_ticks) { if(files.empty() || distance >= DISTANCE_SILENT || !mix_ok) { return; } audio_lock lock; // find a free channel in the desired group int channel = Mix_GroupAvailable(group); if(channel == -1) { LOG_AUDIO << "All channels dedicated to sound group(" << group << ") are busy, skipping.\n"; return; } Mix_Chunk *chunk; std::string file = pick_one(files); try { chunk = load_chunk(file, group); assert(chunk); } catch(const chunk_load_exception&) { return; } /* * This check prevents SDL_Mixer from blowing up on Windows when UI sound is played * in response to toggling the checkbox which disables sound. */ if(group != SOUND_UI) { Mix_SetDistance(channel, distance); } int res; if(loop_ticks > 0) { if(fadein_ticks > 0) { res = Mix_FadeInChannelTimed(channel, chunk, -1, fadein_ticks, loop_ticks); } else { res = Mix_PlayChannel(channel, chunk, -1); } if(res >= 0) { Mix_ExpireChannel(channel, loop_ticks); } } else { if(fadein_ticks > 0) { res = Mix_FadeInChannel(channel, chunk, repeats, fadein_ticks); } else { res = Mix_PlayChannel(channel, chunk, repeats); } } if(res < 0) { ERR_AUDIO << "error playing sound effect: " << Mix_GetError() << std::endl; //still keep it in the sound cache, in case we want to try again later return; } channel_ids[channel] = id; //reserve the channel's chunk from being freed, since it is playing channel_chunks[res] = chunk; }
bool Script::run_chunk(std::string const& chunk) { if(! load_chunk(chunk.c_str()) ) return false; int result = lua_pcall(m_lua,0,LUA_MULTRET,0); return INTERNAL::protected_call_check_result(m_lua,result); }
void World::update_loaded_chunks(const glm::vec3& center) { int x = static_cast<int>((center.x < 0.0f) ? center.x - WorldConstants::CHUNK_SIZE - 1.0f : center.x); int y = static_cast<int>((center.y < 0.0f) ? center.y - WorldConstants::CHUNK_SIZE - 1.0f : center.y); int z = static_cast<int>((center.z < 0.0f) ? center.z - WorldConstants::CHUNK_SIZE - 1.0f : center.z); x /= WorldConstants::CHUNK_SIZE; y /= WorldConstants::CHUNK_SIZE; z /= WorldConstants::CHUNK_SIZE; int first_x = x - m_render_distance; int first_y = y - m_render_distance; int first_z = z - m_render_distance; int last_x = x + m_render_distance; int last_y = y + m_render_distance; int last_z = z + m_render_distance; for (x = first_x; x <= last_x; ++x) { for (y = first_y; y <= last_y; ++y) { for (z = first_z; z <= last_z; ++z) { if (!get_chunk(x, y, z)) load_chunk(x, y, z); } } } bool del_x = false; bool del_y = false; for (auto it_x = m_chunks.begin(); it_x != m_chunks.end();) { for (auto it_y = it_x->second.begin(); it_y != it_x->second.end();) { for (auto it_z = it_y->second.begin(); it_z != it_y->second.end();) { if ((it_z->second->get_x() < first_x) || (it_z->second->get_x() > last_x)) { del_x = true; break; } if ((it_z->second->get_y() < first_y) || (it_z->second->get_y() > last_y)) { del_y = true; break; } if ((it_z->second->get_z() < first_z) || (it_z->second->get_z() > last_z)) { it_z = it_y->second.erase(it_z); } else { ++it_z; } } if (del_x) { break; } else if (del_y) { it_y = it_x->second.erase(it_y); del_y = false; } else { ++it_y; } } if (del_x) { it_x = m_chunks.erase(it_x); del_x = false; } else { ++it_x; } } }