| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include "aiambient.h" |
| 21 | #include "string.h" |
| 22 | #include "objinfo.h" |
| 23 | #include "game.h" |
| 24 | #include "psrand.h" |
| 25 | |
| 26 | ambient_life a_life; |
| 27 | |
| 28 | #define AL_VERSION 1 |
| 29 | |
| 30 | void ambient_life::DoFrame(void) {} |
| 31 | |
| 32 | void ambient_life::InitForLevel(void) { |
| 33 | char i; |
| 34 | |
| 35 | for (i = 0; i < MAX_AL_TYPES; i++) { |
| 36 | ComputeNextSize(i); |
| 37 | m_cur_num[i] = 0; |
| 38 | m_next_do_time[i] = Gametime; |
| 39 | } |
| 40 | |
| 41 | DoFrame(); |
| 42 | } |
| 43 | |
| 44 | void ambient_life::GetALValue(char i, char field, void *ptr) { |
| 45 | switch (field) { |
| 46 | case ALI_TYPE: |
| 47 | *((int *)ptr) = m_type[i]; |
| 48 | break; |
| 49 | case ALI_TOTAL: |
| 50 | *((uint8_t *)ptr) = m_total[i]; |
| 51 | break; |
| 52 | case ALI_MAX: |
| 53 | *((uint8_t *)ptr) = m_max[i]; |
| 54 | break; |
| 55 | case ALI_MIN: |
| 56 | *((uint8_t *)ptr) = m_min[i]; |
| 57 | break; |
| 58 | case ALI_FLAGS: |
| 59 | *((uint8_t *)ptr) = m_flags[i]; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void ambient_life::ComputeNextSize(char i) { |
| 65 | char diff = m_max[i] - m_min[i]; |
| 66 | if (diff > 0) { |
| 67 | char offset = ps_rand() % diff; |
| 68 | m_next_size[i] = m_min[i] + offset; |
| 69 | } else { |
| 70 | m_next_size[i] = m_max[i]; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void ambient_life::SetALValue(char i, char field, void *ptr) { |
| 75 | switch (field) { |
| 76 | case ALI_TYPE: |
| 77 | m_type[i] = *((int *)ptr); |
| 78 | break; |
| 79 | case ALI_TOTAL: |
| 80 | m_total[i] = *((uint8_t *)ptr); |
| 81 | break; |
| 82 | case ALI_MAX: |
| 83 | m_max[i] = *((uint8_t *)ptr); |
| 84 | break; |
| 85 | case ALI_MIN: |
| 86 | m_min[i] = *((uint8_t *)ptr); |
| 87 | break; |
| 88 | case ALI_FLAGS: |
| 89 | m_flags[i] = *((uint8_t *)ptr); |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | // Jeff(Linux): Commented out because m_total[] is unsigned, so never negative |
| 94 | // if(m_total[i] < 0) |
| 95 | // m_total[i] = 0; |
| 96 | if (m_total[i] > MAX_ALS_PER_TYPE) |
| 97 | m_total[i] = MAX_ALS_PER_TYPE; |
| 98 | |
| 99 | // Jeff(Linux): Commented out because m_max[] is unsigned, so never negative |
| 100 | // if(m_max[i] < 0) |
| 101 | // m_max[i] = 0; |
| 102 | if (m_max[i] > m_total[i]) |
| 103 | m_max[i] = m_total[i]; |
| 104 | |
| 105 | // Jeff(Linux): Commented out because m_min[] is unsigned, so never negative |
| 106 | // if(m_min[i] < 0) |
| 107 | // m_min[i] = 0; |
| 108 | if (m_min[i] > m_max[i]) |
| 109 | m_min[i] = m_max[i]; |
| 110 | |
| 111 | ComputeNextSize(i); |
| 112 | } |
| 113 | |
| 114 | void ambient_life::ALReset() { |
| 115 | int i; |
| 116 | int j; |
| 117 | |
| 118 | for (i = 0; i < MAX_AL_TYPES; i++) { |
| 119 | m_type[i] = -1; |
| 120 | |
| 121 | for (j = 0; j < MAX_ALS_PER_TYPE; j++) { |
| 122 | m_handle[i][j] = 0; |
| 123 | } |
| 124 | |
| 125 | m_total[i] = 0; |
| 126 | m_cur_num[i] = 0; |
| 127 | m_flags[i] = 0; |
| 128 | |
| 129 | m_min[i] = 0; |
| 130 | m_max[i] = 0; |
| 131 | |
| 132 | m_next_do_time[i] = 0; |
| 133 | m_next_size[i] = 0; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void ambient_life::SaveData(CFILE *fp) { |
| 138 | int i, j; |
| 139 | int16_t len; |
| 140 | |
| 141 | cf_WriteInt(fp, AL_VERSION); |
| 142 | |
| 143 | for (i = 0; i < MAX_AL_TYPES; i++) { |
| 144 | int type = m_type[i]; |
| 145 | |
| 146 | if (type >= 0) { |
| 147 | len = strlen(Object_info[type].name) + 1; // Accounts for NULL charactor |
| 148 | cf_WriteShort(fp, len); |
| 149 | for (j = 0; j < len; j++) { |
| 150 | cf_WriteByte(fp, Object_info[type].name[j]); |
| 151 | } |
| 152 | } else { |
| 153 | cf_WriteShort(fp, 1); |
| 154 | cf_WriteByte(fp, '\0'); |
| 155 | } |
| 156 | |
| 157 | cf_WriteByte(fp, m_total[i]); |
| 158 | cf_WriteByte(fp, m_flags[i]); |
| 159 | cf_WriteByte(fp, m_min[i]); |
| 160 | cf_WriteByte(fp, m_max[i]); |
| 161 | cf_WriteByte(fp, m_next_size[i]); |
| 162 | cf_WriteFloat(fp, m_next_do_time[i]); |
| 163 | } |
| 164 | |
| 165 | for (i = 0; i < MAX_AL_TYPES; i++) { |
| 166 | cf_WriteByte(fp, m_cur_num[i]); |
| 167 | |
| 168 | for (j = 0; j < m_cur_num[i]; j++) { |
| 169 | cf_WriteInt(fp, m_handle[i][j]); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void ambient_life::LoadData(CFILE *fp) { |
| 175 | int version = cf_ReadInt(fp); |
| 176 | int i, j; |
| 177 | int len; |
| 178 | char temp_name[256]; |
| 179 | |
| 180 | if (version < 1) |
| 181 | return; |
| 182 | |
| 183 | for (i = 0; i < MAX_AL_TYPES; i++) { |
| 184 | len = cf_ReadShort(fp); |
| 185 | for (j = 0; j < len; j++) { |
| 186 | { |
| 187 | temp_name[j] = cf_ReadByte(fp); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | m_type[i] = FindObjectIDName(temp_name); |
| 192 | |
| 193 | m_total[i] = cf_ReadByte(fp); |
| 194 | m_flags[i] = cf_ReadByte(fp); |
| 195 | m_min[i] = cf_ReadByte(fp); |
| 196 | m_max[i] = cf_ReadByte(fp); |
| 197 | m_next_size[i] = cf_ReadByte(fp); |
| 198 | m_next_do_time[i] = cf_ReadFloat(fp); |
| 199 | } |
| 200 | |
| 201 | for (i = 0; i < MAX_AL_TYPES; i++) { |
| 202 | m_cur_num[i] = cf_ReadByte(fp); |
| 203 | |
| 204 | for (j = 0; j < m_cur_num[i]; j++) { |
| 205 | m_handle[i][j] = cf_ReadInt(fp); |
| 206 | } |
| 207 | } |
| 208 | } |