From 8e8ce754c0611e3d8b6b73430541f07d299ba10c Mon Sep 17 00:00:00 2001 From: dwachs Date: Fri, 26 May 2017 09:29:02 +0200 Subject: [PATCH 1/3] ADD: animated pedestrians --- simutrans/trunk/descriptor/pedestrian_desc.h | 32 +++++++++---- .../trunk/descriptor/reader/pedestrian_reader.cc | 6 +++ .../trunk/descriptor/writer/pedestrian_writer.cc | 53 ++++++++++++++++++---- simutrans/trunk/simutrans/history.txt | 5 +- simutrans/trunk/vehicle/simpeople.cc | 24 +++++++++- simutrans/trunk/vehicle/simpeople.h | 4 ++ 6 files changed, 106 insertions(+), 18 deletions(-) diff --git a/simutrans/trunk/descriptor/pedestrian_desc.h b/simutrans/trunk/descriptor/pedestrian_desc.h index 8e3fe69..07c5f99 100644 --- a/simutrans/trunk/descriptor/pedestrian_desc.h +++ b/simutrans/trunk/descriptor/pedestrian_desc.h @@ -8,6 +8,7 @@ #define __PEDESTRIAN_DESC_H #include "obj_base_desc.h" +#include "image_array.h" #include "image_list.h" #include "../dataobj/ribi.h" #include "../network/checksum.h" @@ -22,26 +23,41 @@ * Child nodes: * 0 Name * 1 Copyright - * 2 Image-list + * 2 Image-list or 2d */ class pedestrian_desc_t : public obj_named_desc_t { - friend class pedestrian_reader_t; + friend class pedestrian_reader_t; - uint16 distribution_weight; + uint16 distribution_weight; + uint16 steps_per_frame; public: - image_id get_image_id(ribi_t::dir dir) const - { - image_t const* const image = get_child(2)->get_image(dir); + image_id get_image_id(ribi_t::dir dir, uint16 phase=0) const + { + image_t const* image = NULL; + if (steps_per_frame > 0) { + image = get_child(2)->get_image(dir, phase); + } + else { + image = get_child(2)->get_image(dir); + } return image != NULL ? image->get_id() : IMG_EMPTY; - } + } - uint16 get_distribution_weight() const { return distribution_weight; } + uint16 get_distribution_weight() const { return distribution_weight; } + + uint16 get_steps_per_frame() const { return steps_per_frame; } + + uint16 get_animation_count(ribi_t::dir dir) const + { + return steps_per_frame>0 ? get_child(2)->get_list(dir)->get_count() : 1; + } void calc_checksum(checksum_t *chk) const { chk->input(distribution_weight); } + }; #endif diff --git a/simutrans/trunk/descriptor/reader/pedestrian_reader.cc b/simutrans/trunk/descriptor/reader/pedestrian_reader.cc index 85cb27d..0e1ad48 100644 --- a/simutrans/trunk/descriptor/reader/pedestrian_reader.cc +++ b/simutrans/trunk/descriptor/reader/pedestrian_reader.cc @@ -58,10 +58,16 @@ obj_desc_t * pedestrian_reader_t::read_node(FILE *fp, obj_node_info_t &node) const uint16 v = decode_uint16(p); const int version = v & 0x8000 ? v & 0x7FFF : 0; + desc->steps_per_frame = 0; + if(version == 0) { // old, nonversion node desc->distribution_weight = v; } + else if (version == 1) { + desc->distribution_weight = decode_uint16(p); + desc->steps_per_frame = decode_uint16(p); + } DBG_DEBUG("pedestrian_reader_t::read_node()", "version=%i, chance=%i", version, desc->distribution_weight); return desc; } diff --git a/simutrans/trunk/descriptor/writer/pedestrian_writer.cc b/simutrans/trunk/descriptor/writer/pedestrian_writer.cc index dd7eb24..dd732e8 100644 --- a/simutrans/trunk/descriptor/writer/pedestrian_writer.cc +++ b/simutrans/trunk/descriptor/writer/pedestrian_writer.cc @@ -3,6 +3,7 @@ #include "obj_node.h" #include "text_writer.h" #include "imagelist_writer.h" +#include "imagelist2d_writer.h" #include "pedestrian_writer.h" @@ -10,7 +11,7 @@ void pedestrian_writer_t::write_obj(FILE* fp, obj_node_t& parent, tabfileobj_t& { int i; - obj_node_t node(this, 4, &parent); + obj_node_t node(this, 6, &parent); write_head(fp, node, obj); @@ -20,19 +21,55 @@ void pedestrian_writer_t::write_obj(FILE* fp, obj_node_t& parent, tabfileobj_t& "s", "w", "sw", "se", "n", "e", "ne", "nw" }; slist_tpl keys; + slist_tpl > keys_animated; std::string str; - for (i = 0; i < 8; i++) { - char buf[40]; - sprintf(buf, "image[%s]", dir_codes[i]); + char buf[40]; + // test for animation images + uint16 is_animated = 0; + for (i = 0; i < 8; i++) { + sprintf(buf, "image[%s][0]", dir_codes[i]); str = obj.get(buf); - keys.append(str); + is_animated += !str.empty(); + } + + for (i = 0; i < 8; i++) { + if (is_animated) { + for (uint16 j = 0; j<500; j++) { + keys_animated.append(); + sprintf(buf, "image[%s][%d]", dir_codes[i], j); + str = obj.get(buf); + printf("%s : %s\n", buf, str.c_str()); + if (str.empty()) { + break; + } + keys_animated.at(i).append(str); + } + } + else { + sprintf(buf, "image[%s]", dir_codes[i]); + str = obj.get(buf); + keys.append(str); + } } - imagelist_writer_t::instance()->write_obj(fp, node, keys); - node.write_uint16(fp, distribution_weight, 0); - node.write_uint16(fp, 0, 2); //dummy, unused (and uninitialized in past versions) + uint16 steps_per_frame = is_animated ? max(obj.get_int("steps_per_frame", 1), 1) : 0; + + if (is_animated) { + imagelist2d_writer_t::instance()->write_obj(fp, node, keys_animated); + } + else { + imagelist_writer_t::instance()->write_obj(fp, node, keys); + } + + + // Hajo: Version needs high bit set as trigger -> this is required + // as marker because formerly nodes were unversionend + uint16 version = 0x8001; + node.write_uint16(fp, version, 0); + node.write_uint16(fp, distribution_weight, 2); + node.write_uint16(fp, steps_per_frame, 4); node.write(fp); } diff --git a/simutrans/trunk/simutrans/history.txt b/simutrans/trunk/simutrans/history.txt index 1e8631f..fdeb58d 100644 --- a/simutrans/trunk/simutrans/history.txt +++ b/simutrans/trunk/simutrans/history.txt @@ -3,6 +3,9 @@ ADD: (Ichou) force replacement of wayobj by holding ctrl key while building ADD: (THLeader) long convoi patch FIX: Removed line management window memory leaks + ADD: animated pedestrians, parameters: + steps_per_frame (internal steps per frame, straight movement across tile is 256 steps) + image[dir][a] (dir - direction, a - animation counter, a=0,1,..) Release of 120.2.2: (r8163 on 31-3-2017): ADD: Name filter in depot window @@ -54,7 +57,7 @@ Release of 120.2: (r8077 on 12-Feb-2016): CHG: various improvements to JIT2 to improve factory and electricity network stability CHG: factories which produce power log the amount of power production consumed instead of the amount of power produced CHG: factories which consume power log the amount consumed as negative - FIX: F12 no longer brings up the ingame help window in the Steam version of Simutrans + FIX: F12 no longer brings up the ingame help window in the Steam version of Simutrans Release of 120.1.3: (r7753 on 2-Feb-2016): FIX: road vehicles search for new route when arriving at intersection and old route is invalid diff --git a/simutrans/trunk/vehicle/simpeople.cc b/simutrans/trunk/vehicle/simpeople.cc index 6ab27aa..15168ff 100644 --- a/simutrans/trunk/vehicle/simpeople.cc +++ b/simutrans/trunk/vehicle/simpeople.cc @@ -64,6 +64,7 @@ bool pedestrian_t::successfully_loaded() pedestrian_t::pedestrian_t(loadsave_t *file) : road_user_t() { + animation_steps = 0; rdwr(file); if(desc) { welt->sync.add(this); @@ -75,6 +76,7 @@ pedestrian_t::pedestrian_t(grund_t *gr) : road_user_t(gr, simrand(65535)), desc(pick_any_weighted(list)) { + animation_steps = 0; time_to_life = pick_any(strecke); calc_image(); } @@ -94,6 +96,17 @@ void pedestrian_t::calc_image() } +image_id pedestrian_t::get_image() const +{ + if (desc->get_steps_per_frame() > 0) { + uint16 frame = ((animation_steps + steps) / desc->get_steps_per_frame()) % desc->get_animation_count(ribi_t::get_dir(direction)); + return desc->get_image_id(ribi_t::get_dir(get_direction()), frame); + } + else { + return image; + } +} + void pedestrian_t::rdwr(loadsave_t *file) { @@ -198,7 +211,6 @@ void pedestrian_t::hop(grund_t *gr) { leave_tile(); set_pos(gr->get_pos()); - calc_image(); // no need to call enter_tile(); gr->obj_add(this); @@ -234,4 +246,14 @@ void pedestrian_t::hop(grund_t *gr) // .. but this looks ugly, so disappear time_to_life = 0; } + // carry over remainder to next tile for continuous animation during straight movement + uint16 steps_per_animation = desc->get_steps_per_frame() * desc->get_animation_count(ribi_t::get_dir(direction)); + if (steps_per_animation > 0) { + animation_steps = (animation_steps + steps_next + 1) % steps_per_animation; + } + else { + animation_steps = 0; + } + + calc_image(); } diff --git a/simutrans/trunk/vehicle/simpeople.h b/simutrans/trunk/vehicle/simpeople.h index a8877f5..e6e3ca1 100644 --- a/simutrans/trunk/vehicle/simpeople.h +++ b/simutrans/trunk/vehicle/simpeople.h @@ -18,6 +18,7 @@ private: private: const pedestrian_desc_t *desc; + uint16 animation_steps; protected: void rdwr(loadsave_t *file); @@ -42,6 +43,9 @@ public: sync_result sync_step(uint32 delta_t); + // overloaded to enable animations + virtual image_id get_image() const; + virtual grund_t* hop_check(); virtual void hop(grund_t* gr); -- 1.8.4.5