From a4034c1272ce5f905accc8142eee26963612f9b7 Mon Sep 17 00:00:00 2001 From: Fris0uman <41293484+Fris0uman@users.noreply.github.com> Date: Tue, 28 Apr 2020 17:48:40 +0200 Subject: [PATCH] Move ascii arts to their own file to avoid bloat (#39847) --- data/json/ascii_arts.json | 223 ++++++++++++++++++++++++++++++++ data/json/items/battery.json | 8 ++ data/json/items/containers.json | 2 + data/json/items/generic.json | 1 + doc/JSON_INFO.md | 60 +++++---- src/ascii_art.cpp | 43 ++++++ src/ascii_art.h | 20 +++ src/init.cpp | 2 + src/item.cpp | 8 +- src/item_factory.cpp | 11 +- src/itype.h | 2 +- src/type_id.h | 3 + 12 files changed, 343 insertions(+), 40 deletions(-) create mode 100644 data/json/ascii_arts.json create mode 100644 src/ascii_art.cpp create mode 100644 src/ascii_art.h diff --git a/data/json/ascii_arts.json b/data/json/ascii_arts.json new file mode 100644 index 0000000000000..67d8c18292ba8 --- /dev/null +++ b/data/json/ascii_arts.json @@ -0,0 +1,223 @@ +[ + { + "type": "ascii_art", + "id": "glass_bottle", + "picture": [ + "", + "", + "", + " ║ ║", + " ║ ║", + " ║ ║", + " ╔═╝ ╚═╗", + " ╔╝ #╚╗", + " ╔╝ ### ╚╗", + " ║ #### ║", + " ║ #### ║", + " ║ #### ║", + " ║ #### ║", + " ║#### ║", + " ║### ║", + " # ║", + " ║ ║", + " ║ ║", + " ║ ║", + " ║ ║", + " ╚═══════════╝" + ] + }, + { + "type": "ascii_art", + "id": "plastic_bottle", + "picture": [ + "", + "", + "", + " ║ ║", + " ║ ║", + " ╔╝ ╚╗", + " ╔╝ ╚╗", + " ╔╝ ╚╗", + " ║ ║", + " ╔ ════ ╗", + " ║ ║", + " ╔ ════ ╗", + " ║ ║", + " ╔ ════ ╗", + " ║ ║", + " ╔ ════ ╗", + " ║ ║", + " ╔ ════ ╗", + " ║ ║", + " ╔ ════ ╗", + " ║ ║", + " ╚════════╝" + ] + }, + { + "type": "ascii_art", + "id": "cashcard", + "picture": [ + "", + "", + "", + " ╔═══════════════════╗", + " ║ ║", + " ╔═ ╔═╔═╗╔═║ ║ ║", + " ║═ ┼ ║ ║═║╚╗║═║ ║", + " ╚═ ╚═║ ║═╝║ ║ ║", + " ║ ║", + " ║ RIVTECH TRUST ║", + " ║ ║", + " ║ ║", + " ║ 555 993 55221 066 ║", + " ╚═══════════════════╝" + ] + }, + { + "type": "ascii_art", + "id": "ultra_light_battery", + "picture": [ + "", + "", + " █████", + " ██████████", + " █████████████", + " ███████─███████", + " ███████████████", + " █████████████████", + " ███████████████████", + " ████ULTRA∙LIGHT████", + " ███████████████████", + " ███████████████████", + " █████████████████", + " █████████████████", + " ███████┼███████", + " █████████████", + " █████████", + " █████" + ] + }, + { + "type": "ascii_art", + "id": "ultra_light_battery_plut", + "picture": [ + "", + "", + " █████", + " ██████████", + " █████████████", + " ███████─███████", + " ███████████████", + " █████████████████", + " ███████████████████", + " ████ULTRA∙LIGHT████", + " ███████████████████", + " ███████████████████", + " █████████████████", + " █████████████████", + " ███████┼███████", + " █████████████", + " █████████", + " █████" + ] + }, + { + "type": "ascii_art", + "id": "light_battery", + "picture": [ + "", + "", + " ┌ ^ ┐", + " │ + │", + " │ │", + " │ │", + " │ │", + " │ │", + " │ - │", + " └ - ┘" + ] + }, + { + "type": "ascii_art", + "id": "light_battery_plut", + "picture": [ + "", + "", + " ┌ ^ ┐", + " │ + │", + " │ │", + " │ │", + " │ │", + " │ │", + " │ - │", + " └ - ┘" + ] + }, + { + "type": "ascii_art", + "id": "medium_battery", + "picture": [ + "", + "", + " - ──── ^ ┐", + " │ │", + " │ │", + " │ ─ ┼ │", + " │ │", + " │ MEDIUM │", + " └──────────┘" + ] + }, + { + "type": "ascii_art", + "id": "medium_battery_plut", + "picture": [ + "", + "", + " - ──── ^ ┐", + " │ │", + " │ │", + " │ ─ ┼ │", + " │ │", + " │ MEDIUM │", + " └──────────┘" + ] + }, + { + "type": "ascii_art", + "id": "heavy_battery", + "picture": [ + "", + "", + " -┐ ┌^┐", + " ╔═ ═══════ ═╗", + " ║ ║", + " ║ ║", + " ║ ═ ╫ ║", + " ║ ║", + " ║ ║", + " ║ ║", + " ║ HEAVY ║", + " ╚═══════════════╝" + ] + }, + { + "type": "ascii_art", + "id": "heavy_battery_plut", + "picture": [ + "", + "", + " -┐ ┌^┐", + " ╔═ ═══════ ═╗", + " ║ ║", + " ║ ║", + " ║ ═ ╫ ║", + " ║ ║", + " ║ ║", + " ║ ║", + " ║ HEAVY ║", + " ╚═══════════════╝" + ] + } +] diff --git a/data/json/items/battery.json b/data/json/items/battery.json index be4dd6251d8c9..08ccc263f159f 100644 --- a/data/json/items/battery.json +++ b/data/json/items/battery.json @@ -22,6 +22,7 @@ "category": "spare_parts", "name": { "str": "ultra-light battery", "str_pl": "ultra-light batteries" }, "description": "This is a light battery cell designed for small size over everything else. It retains its universal compatibility, though.", + "ascii_picture": "ultra_light_battery", "weight": "5 g", "volume": "1 ml", "price": 1500, @@ -40,6 +41,7 @@ "category": "spare_parts", "name": { "str": "ultra-light plutonium fuel battery", "str_pl": "ultra-light plutonium fuel batteries" }, "description": "This battery uses a thin plutonium-244 rod to stablize an exotic nanocompound. It is universally compatible with small devices. Although it stores a huge amount of power, it cannot be recharged.", + "ascii_picture": "ultra_light_battery_plut", "weight": "80 g", "volume": "1 ml", "price": 15000, @@ -78,6 +80,7 @@ "category": "spare_parts", "name": { "str": "light battery", "str_pl": "light batteries" }, "description": "This is a light battery cell, universally compatible with all kinds of small devices.", + "ascii_picture": "light_battery", "weight": "30 g", "volume": "25ml", "price": 3500, @@ -114,6 +117,7 @@ "category": "spare_parts", "name": { "str": "light plutonium fuel battery", "str_pl": "light plutonium fuel batteries" }, "description": "This battery uses a thin plutonium-244 rod to stabilize an exotic nanocompound. It is universally compatible with all kinds of personal electronic devices. Although it stores a huge amount of power, it cannot be recharged.", + "ascii_picture": "light_battery_plut", "weight": "160 g", "volume": "35ml", "price": 50000, @@ -152,6 +156,7 @@ "category": "spare_parts", "name": { "str": "medium battery", "str_pl": "medium batteries" }, "description": "This is a medium battery cell, universally compatible with all kinds of appliances and power tools.", + "ascii_picture": "medium_battery", "weight": "600 g", "volume": "450ml", "price": 10000, @@ -188,6 +193,7 @@ "category": "spare_parts", "name": { "str": "medium plutonium fuel battery", "str_pl": "medium plutonium fuel batteries" }, "description": "This battery uses a thin plutonium-244 rod to stablize an exotic nanocompound. It is universally compatible with all kinds of appliances and power tools. Although it stores a huge amount of power, it cannot be recharged.", + "ascii_picture": "medium_battery_plut", "weight": "1000 g", "volume": "525ml", "price": 100000, @@ -226,6 +232,7 @@ "category": "spare_parts", "name": { "str": "heavy battery", "str_pl": "heavy batteries" }, "description": "This is a heavy battery cell, universally compatible with all kinds of industrial-grade equipment and large tools.", + "ascii_picture": "heavy_battery", "weight": "1000 g", "volume": "1225ml", "price": 10000, @@ -262,6 +269,7 @@ "category": "spare_parts", "name": { "str": "heavy plutonium fuel battery", "str_pl": "heavy plutonium fuel batteries" }, "description": "This battery uses a thin plutonium-244 rod to stablize an exotic nanocompound. It is universally compatible with all kinds of industrial-grade equipment and large tools. Although it stores a huge amount of power, it cannot be recharged.", + "ascii_picture": "heavy_battery_plut", "weight": "1600 g", "volume": "1500ml", "price": 100000, diff --git a/data/json/items/containers.json b/data/json/items/containers.json index ee88fa151b2c9..aaa2ff3be0949 100644 --- a/data/json/items/containers.json +++ b/data/json/items/containers.json @@ -206,6 +206,7 @@ "to_hit": 1, "bashing": 2, "material": "glass", + "ascii_picture": "glass_bottle", "symbol": ")", "color": "cyan", "contains": "750 ml", @@ -225,6 +226,7 @@ "price_postapoc": 10, "to_hit": 1, "material": "plastic", + "ascii_picture": "plastic_bottle", "symbol": ")", "color": "light_cyan", "contains": "500 ml", diff --git a/data/json/items/generic.json b/data/json/items/generic.json index 60c9e9cbab32e..ca7a4a79b6d8f 100644 --- a/data/json/items/generic.json +++ b/data/json/items/generic.json @@ -2456,6 +2456,7 @@ "symbol": ",", "color": "yellow", "name": { "str": "cash card" }, + "ascii_picture": "cashcard", "description": "A yellow plastic card used to store money. These became popular once the government officially moved to all electronic money. It holds up to 2 million dollars.", "price": 1000, "price_postapoc": 0, diff --git a/doc/JSON_INFO.md b/doc/JSON_INFO.md index 1bba8ae5175d4..534b0831d106d 100644 --- a/doc/JSON_INFO.md +++ b/doc/JSON_INFO.md @@ -17,6 +17,7 @@ Use the `Home` key to return to the top. + [Other formatting](#other-formatting) - [Description and content of each JSON file](#description-and-content-of-each-json-file) * [`data/json/` JSONs](#datajson-jsons) + + [Ascii_arts](#ascii_arts) + [Body_parts](#body_parts) + [Bionics](#bionics) + [Dreams](#dreams) @@ -185,6 +186,7 @@ Here's a quick summary of what each of the JSON files contain, broken down by fo |--- |--- | achievements.json | achievements | anatomy.json | a listing of player body parts - do not edit +| ascii_arts.json | ascii arts for item descriptions | bionics.json | bionics, does NOT include bionic effects | body_parts.json | an expansion of anatomy.json - do not edit | clothing_mods.json | definition of clothing mods @@ -381,6 +383,36 @@ This section describes each json file and their contents. Each json has their ow ## `data/json/` JSONs +### Ascii_arts + +| Identifier | Description +|--- |--- +| id | Unique ID. Must be one continuous word, use underscores if necessary. +| picture | Array of string, each entry is a line of an ascii picture and must be at most 42 columns long. + +```C++ + { + "type": "ascii_art", + "id": "cashcard", + "picture": [ + "", + "", + "", + " ╔═══════════════════╗", + " ║ ║", + " ╔═ ╔═╔═╗╔═║ ║ ║", + " ║═ ┼ ║ ║═║╚╗║═║ ║", + " ╚═ ╚═║ ║═╝║ ║ ║", + " ║ ║", + " ║ RIVTECH TRUST ║", + " ║ ║", + " ║ ║", + " ║ 555 993 55221 066 ║", + " ╚═══════════════════╝" + ] + } +``` + ### Body_parts | Identifier | Description @@ -1544,33 +1576,7 @@ See also VEHICLE_JSON.md "symbol": "[", // The item symbol as it appears on the map. Must be a Unicode string exactly 1 console cell width. "looks_like": "rag", // hint to tilesets if this item has no tile, use the looks_like tile "description": "Socks. Put 'em on your feet.", // Description of the item -"ascii_picture": [ - " ,,,,,,,,,,,,,", - " .;;;;;;;;;;;;;;;;;;;,.", - " .;;;;;;;;;;;;;;;;;;;;;;;;,", - ".;;;;;;;;;;;;;;;;;;;;;;;;;;;;.", - ";;;;;@;;;;;;;;;;;;;;;;;;;;;;;;' .............", - ";;;;@@;;;;;;;;;;;;;;;;;;;;;;;;'.................", - ";;;;@@;;;;;;;;;;;;;;;;;;;;;;;;'...................`", - ";;;;@;;;;;;;;;;;;;;;@;;;;;;;'.....................", - " `;;;;;;;;;;;;;;;;;;;@@;;;;;'..................;....", // Ascii art that will be displayed at the bottom of the item description. Array of string with each element being a line of the picture. Lines longer than 42 characters won't display properly. - " `;;;;;;;;;;;;;;;;@@;;;;'....................;;...", - " `;;;;;;;;;;;;;@;;;;'...;.................;;....", - " `;;;;;;;;;;;;' ...;;...............;.....", - " `;;;;;;' ...;;..................", - " ;; ..;...............", - " ` ............", - " ` ......", - " ` ..", - " ` '", - " ` '", - " ` '", - " ` `", - " ` `,", - " `", - " `", - " `." - ], +"ascii_picture": "ascii_socks", // Id of the asci_art used for this item "phase": "solid", // (Optional, default = "solid") What phase it is "weight": "350 g", // Weight, weight in grams, mg and kg can be used - "50 mg", "5 g" or "5 kg". For stackable items (ammo, comestibles) this is the weight per charge. "volume": "250 ml", // Volume, volume in ml and L can be used - "50 ml" or "2 L". For stackable items (ammo, comestibles) this is the volume of stack_size charges. diff --git a/src/ascii_art.cpp b/src/ascii_art.cpp new file mode 100644 index 0000000000000..a2fb9a66736b8 --- /dev/null +++ b/src/ascii_art.cpp @@ -0,0 +1,43 @@ +#include "ascii_art.h" + +#include "assign.h" +#include "generic_factory.h" + +static const int ascii_art_width = 42; + +namespace +{ +generic_factory ascii_art_factory( "ascii_art" ); +} // namespace + +template<> +const ascii_art &string_id::obj() const +{ + return ascii_art_factory.obj( *this ); +} + +template<> +bool string_id::is_valid() const +{ + return ascii_art_factory.is_valid( *this ); +} + +void ascii_art::load_ascii_art( const JsonObject &jo, const std::string &src ) +{ + ascii_art_factory.load( jo, src ); +} + +void ascii_art::load( const JsonObject &jo, const std::string & ) +{ + assign( jo, "id", id ); + + assign( jo, "picture", picture ); + for( std::string &line : picture ) { + if( utf8_width( remove_color_tags( line ) ) > ascii_art_width ) { + line = trim_by_length( line, ascii_art_width ); + debugmsg( "picture in %s contains a line too long to be displayed (>%i char).", id.c_str(), + ascii_art_width ); + } + } +} + diff --git a/src/ascii_art.h b/src/ascii_art.h new file mode 100644 index 0000000000000..b5f248c829d97 --- /dev/null +++ b/src/ascii_art.h @@ -0,0 +1,20 @@ +#pragma once +#ifndef ASCII_ART_H +#define ASCII_ART_H + +#include "type_id.h" +#include "json.h" + + +class ascii_art +{ + public: + static void load_ascii_art( const JsonObject &jo, const std::string &src ); + void load( const JsonObject &jo, const std::string & ); + bool was_loaded; + + ascii_art_id id; + std::vector picture; +}; + +#endif diff --git a/src/init.cpp b/src/init.cpp index aa97399b02dba..fc8728aca79bc 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -16,6 +16,7 @@ #include "ammo.h" #include "ammo_effect.h" #include "anatomy.h" +#include "ascii_art.h" #include "behavior.h" #include "bionics.h" #include "bodypart.h" @@ -228,6 +229,7 @@ void DynamicDataLoader::initialize() add( "hit_range", &Creature::load_hit_range ); add( "scent_type", &scent_type::load_scent_type ); add( "disease_type", &disease_type::load_disease_type ); + add( "ascii_art", &ascii_art::load_ascii_art ); // json/colors.json would be listed here, but it's loaded before the others (see init_colors()) // Non Static Function Access diff --git a/src/item.cpp b/src/item.cpp index 22ac19245bdd6..96ff419d90221 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -17,6 +17,7 @@ #include "advanced_inv.h" #include "ammo.h" +#include "ascii_art.h" #include "avatar.h" #include "bionics.h" #include "bodypart.h" @@ -3681,8 +3682,11 @@ void item::final_info( std::vector &info, const iteminfo_query *parts, } } if( get_option( "ENABLE_ASCII_ART_ITEM" ) ) { - for( const std::string &line : type->ascii_picture ) { - info.push_back( iteminfo( "DESCRIPTION", line ) ); + const ascii_art_id art = type->picture_id; + if( art.is_valid() ) { + for( const std::string &line : art->picture ) { + info.push_back( iteminfo( "DESCRIPTION", line ) ); + } } } } diff --git a/src/item_factory.cpp b/src/item_factory.cpp index b1ab7c5844dee..e3d99768022c9 100644 --- a/src/item_factory.cpp +++ b/src/item_factory.cpp @@ -66,8 +66,6 @@ static void set_allergy_flags( itype &item_template ); static void hflesh_to_flesh( itype &item_template ); static void npc_implied_flags( itype &item_template ); -static const int ascii_art_width = 42; - bool item_is_blacklisted( const std::string &id ) { return item_blacklist.count( id ); @@ -508,13 +506,6 @@ void Item_factory::finalize_post( itype &obj ) } } } - for( std::string &line : obj.ascii_picture ) { - if( utf8_width( remove_color_tags( line ) ) > ascii_art_width ) { - line = trim_by_length( line, ascii_art_width ); - debugmsg( "ascii_picture in %s contains a line too long to be displayed (>%i char).", obj.id, - ascii_art_width ); - } - } } void Item_factory::finalize() @@ -2211,7 +2202,7 @@ void Item_factory::load_basic_info( const JsonObject &jo, itype &def, const std: assign( jo, "explode_in_fire", def.explode_in_fire ); assign( jo, "insulation", def.insulation_factor ); assign( jo, "solar_efficiency", def.solar_efficiency ); - assign( jo, "ascii_picture", def.ascii_picture ); + assign( jo, "ascii_picture", def.picture_id ); if( jo.has_member( "thrown_damage" ) ) { def.thrown_damage = load_damage_instance( jo.get_array( "thrown_damage" ) ); diff --git a/src/itype.h b/src/itype.h index e1d08f16f040c..e6fc7c9fff798 100644 --- a/src/itype.h +++ b/src/itype.h @@ -869,7 +869,7 @@ struct itype { std::string snippet_category; translation description; // Flavor text - std::vector ascii_picture; + ascii_art_id picture_id; // The container it comes in cata::optional default_container; diff --git a/src/type_id.h b/src/type_id.h index 1a6431c82cd17..3e9b86584e029 100644 --- a/src/type_id.h +++ b/src/type_id.h @@ -34,6 +34,9 @@ using efftype_id = string_id; class scent_type; using scenttype_id = string_id; +class ascii_art; +using ascii_art_id = string_id; + class disease_type; using diseasetype_id = string_id;