Crossfire Server  1.75.0
Faces.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2020 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #include "Faces.h"
14 #include "string.h"
15 
16 template<>
17 Face *asset_create(const std::string& name) {
18  Face *face = (Face *)calloc(1, sizeof(Face));
19  face->name = add_string(name.c_str());
20  return face;
21 }
22 
23 template<>
24 void asset_destroy(Face *item) {
25  free_string(item->name);
26  free(item);
27 }
28 
29 Faces::Faces() : m_checksum(0) {
33 }
34 
35 void Faces::replace(Face *existing, Face *update) {
36  existing->visibility = update->visibility;
37  existing->magicmap = update->magicmap;
38  if (update->smoothface) {
39  existing->smoothface = update->smoothface;
40  }
41  asset_destroy(update);
42 }
43 
44 const Face *Faces::findById(uint16_t id) {
45  auto face = std::find_if(m_assets.begin(), m_assets.end(), [&id] (auto& face) {
46  return face.second->number == id;
47  });
48  return face == m_assets.end() ? NULL : face->second;
49 }
50 
51 void Faces::added(Face *face) {
52  face->number = m_assets.size() - 1;
53 
55  m_checksum += face->number & 0xff;
56  m_checksum &= 0xffffffff;
57 
59  m_checksum += (face->number >> 8) & 0xff;
60  m_checksum &= 0xffffffff;
61  for (size_t l = 0; l < strlen(face->name); l++) {
63  m_checksum += face->name[l];
64  m_checksum &= 0xffffffff;
65  }
66 }
virtual void added(Face *face) override
An asset was either referenced (but undefined) or defined.
Definition: Faces.cpp:51
Face * smoothface
Smoothed face for this, NULL for none.
Definition: face.h:18
#define ROTATE_RIGHT(c)
Definition: global.h:162
New face structure - this enforces the notion that data is face by face only - you can not change the...
Definition: face.h:14
sstring add_string(const char *str)
Share a string.
Definition: shstr.cpp:137
const Face * findById(uint16_t id)
Definition: Faces.cpp:44
std::unordered_map< std::string, Face *> m_assets
Known assets.
const Face * blank_face
Following can just as easily be pointers, but it is easier to keep them like this.
Definition: image.cpp:36
Faces()
Definition: Faces.cpp:29
void free_string(sstring str)
This will reduce the refcount, and if it has reached 0, str will be freed.
Definition: shstr.cpp:294
virtual void replace(Face *existing, Face *update) override
Replace an asset by an updated version.
Definition: Faces.cpp:35
void asset_destroy(Face *item)
Definition: Faces.cpp:24
#define BLANK_FACE_NAME
Definition: define.h:576
Face * asset_create(const std::string &name)
Definition: Faces.cpp:17
uint16_t number
This is the image unique identifier.
Definition: face.h:15
const Face * empty_face
Definition: image.cpp:36
uint8_t visibility
How visible is the face compared to other faces, highest wins.
Definition: face.h:16
#define SMOOTH_FACE_NAME
Definition: define.h:578
const Face * smooth_face
Definition: image.cpp:36
int m_checksum
Definition: Faces.h:28
#define EMPTY_FACE_NAME
Definition: define.h:577
uint8_t magicmap
Color to show this in magic map.
Definition: face.h:17
sstring name
Face name, as used by archetypes and such.
Definition: face.h:19