/* * Unserializes the data from buffer to a specialized object. * Should return 1 on error, 0 on success. * Serial reder position should be set to correct position before calling this. */ int object_unserialize(object *obj, serial *ser, game_state *gs) { obj->pos.x = serial_read_float(ser); obj->pos.y = serial_read_float(ser); obj->vel.x = serial_read_float(ser); obj->vel.y = serial_read_float(ser); float gravity = serial_read_float(ser); obj->direction = serial_read_int8(ser); obj->group = serial_read_int8(ser); obj->layers = serial_read_int8(ser); uint8_t stride = serial_read_int8(ser); uint8_t repeat = serial_read_int8(ser); obj->sprite_override = serial_read_int8(ser); obj->age = serial_read_int32(ser); random_seed(&obj->rand_state, serial_read_int32(ser)); uint8_t animation_id = serial_read_int8(ser); uint8_t pal_offset = serial_read_int8(ser); int8_t hit_frames = serial_read_int8(ser); int8_t can_hit = serial_read_int8(ser); // Other stuff not included in serialization obj->y_percent = 1.0; obj->cur_animation_own = OWNER_EXTERNAL; obj->cur_animation = NULL; obj->cur_sprite = NULL; obj->sound_translation_table = NULL; obj->cur_surface = NULL; obj->cur_remap = 0; obj->halt = 0; obj->halt_ticks = 0; obj->cast_shadow = 0; player_create(obj); // Read animation state uint16_t anim_str_len = serial_read_int16(ser); char anim_str[anim_str_len+1]; if(anim_str_len > 0) { serial_read(ser, anim_str, anim_str_len); } obj->animation_state.current_tick = (uint16_t)serial_read_int16(ser); obj->animation_state.previous_tick = (uint16_t)serial_read_int16(ser); uint8_t reverse = serial_read_int8(ser); // Read the specialization ID from ther serial "stream". // This should be an int. int specialization_id = serial_read_int8(ser); // This should automatically bootstrap the object so that it has at least // unserialize function callback and local memory allocated object_auto_specialize(obj, specialization_id); // Now, if the object has unserialize function, call it with // serialization data. serial object should be pointing to the // start of that data. if(obj->unserialize != NULL) { obj->unserialize(obj, ser, animation_id, gs); } else { DEBUG("object has no special unserializer"); } // Init animation with correct string and tick if(anim_str_len > 0) { // server is using a custom string DEBUG("serialized object has custom animation string %s", anim_str); player_reload_with_str(obj, anim_str); } if(reverse) { object_set_playback_direction(obj, PLAY_BACKWARDS); } // deserializing hars can reset these, so we have to set this late obj->stride = stride; object_set_gravity(obj, gravity); object_set_repeat(obj, repeat); object_set_pal_offset(obj, pal_offset); obj->hit_frames = hit_frames; obj->can_hit = can_hit; /*DEBUG("Animation state: [%d] %s, ticks = %d stride = %d direction = %d pos = %f,%f vel = %f,%f gravity = %f", strlen(player_get_str(obj))+1, player_get_str(obj), obj->animation_state.ticks, obj->stride, obj->animation_state.reverse, obj->pos.x, obj->pos.y, obj->vel.x, obj->vel.y, obj->gravity);*/ // Return success return 0; }
/** Sets a new animation string to currently playing animation * \param obj Object handle * \param str New animation string */ void object_set_custom_string(object *obj, const char *str) { obj->custom_str = strcpy(malloc(strlen(str)+1), str); player_reload_with_str(obj, str); DEBUG("Set animation string to %s", str); }
void player_reload(object *obj) { player_reload_with_str(obj, str_c(&obj->cur_animation->animation_string)); }