cp_messages.c

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2002-2010 UFO: Alien Invasion.
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016 
00017 See the GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00023 */
00024 #include "../cl_shared.h"
00025 #include "../sound/s_sample.h"
00026 #include "../sound/s_main.h"
00027 #include "../ui/ui_main.h"
00028 #include "../ui/ui_popup.h"
00029 #include "cp_campaign.h"
00030 #include "cp_popup.h"
00031 #include "cp_messages.h"
00032 #include "cp_time.h"
00033 #include "save/save_messages.h"
00034 
00035 char cp_messageBuffer[MAX_MESSAGE_TEXT];
00036 message_t *cp_messageStack;
00037 
00044 static void MS_TimestampedText (char *text, message_t *message, size_t textsize)
00045 {
00046     dateLong_t date;
00047     CL_DateConvertLong(&message->date, &date);
00048     Com_sprintf(text, textsize, _("%i %s %02i, %02i:%02i: "), date.year,
00049         Date_GetMonthName(date.month - 1), date.day, date.hour, date.min);
00050 }
00051 
00065 message_t *MS_AddNewMessage (const char *title, const char *text, qboolean popup, messageType_t type, technology_t *pedia)
00066 {
00067     return MS_AddNewMessageSound(title, text, popup, type, pedia, qtrue);
00068 }
00069 
00083 message_t *MS_AddNewMessageSound (const char *title, const char *text, qboolean popup, messageType_t type, technology_t *pedia, qboolean playSound)
00084 {
00085     message_t *mess;
00086     const char *sound = NULL;
00087 
00088     assert(type < MSG_MAX);
00089 
00090     /* allocate memory for new message - delete this with every new game */
00091     mess = (message_t *) Mem_PoolAlloc(sizeof(*mess), cp_campaignPool, 0);
00092 
00093     /* push the new message at the beginning of the stack */
00094     mess->next = cp_messageStack;
00095     cp_messageStack = mess;
00096 
00097     mess->type = type;
00098     mess->pedia = pedia;        /* pointer to UFOpaedia entry */
00099 
00100     mess->date = ccs.date;
00101 
00103     Q_strncpyz(mess->title, title, sizeof(mess->title));
00104     mess->text = Mem_PoolStrDup(text, cp_campaignPool, 0);
00105 
00106     /* get formatted date text */
00107     MS_TimestampedText(mess->timestamp, mess, sizeof(mess->timestamp));
00108 
00109     /* they need to be translated already */
00110     if (popup)
00111         CP_PopupList(mess->title, mess->text);
00112 
00113     switch (type) {
00114     case MSG_DEBUG:
00115         break;
00116     case MSG_STANDARD:
00117         sound = "geoscape/standard";
00118         break;
00119     case MSG_INFO:
00120     case MSG_TRANSFERFINISHED:
00121     case MSG_DEATH:
00122     case MSG_CONSTRUCTION:
00123     case MSG_PRODUCTION:
00124         sound = "geoscape/info";
00125         break;
00126     case MSG_RESEARCH_PROPOSAL:
00127     case MSG_RESEARCH_FINISHED:
00128         assert(pedia);
00129     case MSG_RESEARCH_HALTED:
00130     case MSG_EVENT:
00131     case MSG_NEWS:
00132         /* reread the new mails in UP_GetUnreadMails */
00133         ccs.numUnreadMails = -1;
00134         sound = "geoscape/mail";
00135         break;
00136     case MSG_UFOSPOTTED:
00137         sound = "geoscape/ufospotted";
00138         break;
00139     case MSG_BASEATTACK:
00140         sound = "geoscape/basealert";
00141         break;
00142     case MSG_TERRORSITE:
00143         sound = "geoscape/alien-activity";
00144         break;
00145     case MSG_CRASHSITE:
00146         sound = "geoscape/newmission";
00147         break;
00148     case MSG_PROMOTION:
00149         sound = "geoscape/promotion";
00150         break;
00151     case MSG_MAX:
00152         break;
00153     }
00154 
00155     if (playSound)
00156         S_StartLocalSample(sound, SND_VOLUME_DEFAULT);
00157 
00158     return mess;
00159 }
00160 
00167 static void MS_MessageSaveXML (mxml_node_t *p, message_t *message)
00168 {
00169     mxml_node_t *n;
00170 
00171     if (!message)
00172         return;
00173 
00174     /* bottom up */
00175     MS_MessageSaveXML(p, message->next);
00176 
00177     /* don't save these message types */
00178     if (message->type == MSG_INFO)
00179         return;
00180 
00181     Com_RegisterConstList(saveMessageConstants);
00182     n = mxml_AddNode(p, SAVE_MESSAGES_MESSAGE);
00183     mxml_AddString(n, SAVE_MESSAGES_TYPE, Com_GetConstVariable(SAVE_MESSAGETYPE_NAMESPACE, message->type));
00184     mxml_AddStringValue(n, SAVE_MESSAGES_TITLE, message->title);
00185     mxml_AddStringValue(n, SAVE_MESSAGES_TEXT, message->text);
00186     /* store script id of event mail */
00187     if (message->type == MSG_EVENT) {
00188         mxml_AddString(n, SAVE_MESSAGES_EVENTMAILID, message->eventMail->id);
00189         mxml_AddBoolValue(n, SAVE_MESSAGES_EVENTMAILREAD, message->eventMail->read);
00190     }
00191     if (message->pedia)
00192         mxml_AddString(n, SAVE_MESSAGES_PEDIAID, message->pedia->id);
00193     mxml_AddDate(n, SAVE_MESSAGES_DATE, message->date.day, message->date.sec);
00194     Com_UnregisterConstList(saveMessageConstants);
00195 }
00196 
00202 qboolean MS_SaveXML (mxml_node_t *p)
00203 {
00204     mxml_node_t *n = mxml_AddNode(p, SAVE_MESSAGES_MESSAGES);
00205 
00206     /* store message system items */
00207     MS_MessageSaveXML(n, cp_messageStack);
00208     return qtrue;
00209 }
00210 
00217 qboolean MS_LoadXML (mxml_node_t *p)
00218 {
00219     int i;
00220     mxml_node_t *n, *sn;
00221     n = mxml_GetNode(p, SAVE_MESSAGES_MESSAGES);
00222 
00223     if (!n)
00224         return qfalse;
00225 
00226     /* we have to set this a little bit higher here, otherwise the samples that are played when adding
00227      * a message to the stack would all played a few milliseconds after each other - that doesn't sound
00228      * nice */
00229     S_SetSampleRepeatRate(500);
00230 
00231     Com_RegisterConstList(saveMessageConstants);
00232     for (sn = mxml_GetNode(n, SAVE_MESSAGES_MESSAGE), i = 0; sn; sn = mxml_GetNextNode(sn, n, SAVE_MESSAGES_MESSAGE), i++) {
00233         eventMail_t *mail;
00234         const char *type = mxml_GetString(sn, SAVE_MESSAGES_TYPE);
00235         int mtype;
00236         char title[MAX_VAR];
00237         char text[MAX_MESSAGE_TEXT];
00238 
00239         if (!Com_GetConstIntFromNamespace(SAVE_MESSAGETYPE_NAMESPACE, type, (int*) &mtype)) {
00240             Com_Printf("Invaild message type '%s'\n", type);
00241             continue;
00242         }
00243 
00244         /* can contain high bits due to utf8 */
00245         Q_strncpyz(title, mxml_GetString(sn, SAVE_MESSAGES_TITLE), sizeof(title));
00246         Q_strncpyz(text,  mxml_GetString(sn, SAVE_MESSAGES_TEXT),  sizeof(text));
00247 
00248         if (mtype == MSG_EVENT) {
00249             mail = CL_GetEventMail(mxml_GetString(sn, SAVE_MESSAGES_EVENTMAILID), qfalse);
00250             if (mail)
00251                 mail->read = mxml_GetBool(sn, SAVE_MESSAGES_EVENTMAILREAD, qfalse);
00252         } else
00253             mail = NULL;
00254 
00255         /* event and not mail means, dynamic mail - we don't save or load them */
00257         if (!((mtype == MSG_EVENT && !mail) || (mtype == MSG_DEBUG && developer->integer != 1))) {
00258             char id[MAX_VAR];
00259             technology_t *tech = NULL;
00260             message_t *mess;
00261 
00262             Q_strncpyz(id, mxml_GetString(sn, SAVE_MESSAGES_PEDIAID), sizeof(id));
00263             if (id[0] != '\0')
00264                 tech = RS_GetTechByID(id);
00265             if (!tech && (mtype == MSG_RESEARCH_PROPOSAL || mtype == MSG_RESEARCH_FINISHED)) {
00267                 continue;
00268             }
00269             mess = MS_AddNewMessageSound(title, text, qfalse, mtype, tech, qfalse);
00270             mess->eventMail = mail;
00271             mxml_GetDate(sn, SAVE_MESSAGES_DATE, &mess->date.day, &mess->date.sec);
00272             /* redo timestamp text after setting date */
00273             MS_TimestampedText(mess->timestamp, mess, sizeof(mess->timestamp));
00274         }
00275     }
00276     Com_UnregisterConstList(saveMessageConstants);
00277 
00278     /* reset the sample repeat rate */
00279     S_SetSampleRepeatRate(0);
00280 
00281     return qtrue;
00282 }
00283 
00284 void MS_MessageInit (void)
00285 {
00286     MSO_Init();
00287 }

Generated by  doxygen 1.6.2