void SaveEachTimer(timer_node *t) { int save_time; object_node *o; char *s; o = GetObjectByID(t->object_id); if (o == NULL) { eprintf("SaveEachTimer can't get object %i\n",t->object_id); return; } s = GetNameByID(t->message_id); if (!s) { eprintf("SaveEachTimer is not saving invalid timer %i\n",t->object_id); return; } SaveGameWriteByte(SAVE_GAME_TIMER); SaveGameWriteInt(t->timer_id); SaveGameWriteInt(t->object_id); SaveGameWriteString(GetNameByID(t->message_id)); save_time = (int)(t->time - GetMilliCount()); if (save_time < 0) save_time = 0; SaveGameWriteInt(save_time); }
void MoveObject(int dest_id,int source_id) { object_node *source,*dest; source = GetObjectByID(source_id); if (source == NULL) { eprintf("MoveObject can't find source %i, total death end game\n", source_id); FlushDefaultChannels(); return; } dest = GetObjectByIDEvenDeleted(dest_id); if (dest == NULL) { eprintf("MoveListNode can't find dest %i, total death end game\n", dest_id); FlushDefaultChannels(); return; } /* don't change the dest id here--it is set to array index, correctly */ dest->class_id = source->class_id; dest->deleted = source->deleted; dest->garbage_ref = source->garbage_ref; dest->num_props = source->num_props; dest->p = source->p; if (source->class_id == SYSTEM_CLASS) SetSystemObjectID(dest_id); }
__inline void StoreValue(int object_id,local_var_type *local_vars,int data_type,int data, val_type new_data) { class_node *class_data; object_node *o; if (kod_stat.debugging) { if (new_data.v.tag == TAG_INVALID) eprintf("[%s] StoreValue trying to assign with uninitialized data (INVALID %i)\n", BlakodDebugInfo(),new_data.v.data); } switch (data_type) { case LOCAL_VAR : if (data < 0 || data >= local_vars->num_locals) { eprintf("[%s] StoreValue can't write to illegal local var %i\n", BlakodDebugInfo(),data); return; } local_vars->locals[data].int_val = new_data.int_val; break; case PROPERTY : o = GetObjectByID(object_id); if (o == NULL) { eprintf("[%s] StoreValue can't find object %i\n", BlakodDebugInfo(),object_id); return; } class_data = GetClassByID(o->class_id); if (class_data == NULL) { eprintf("[%s] StoreValue can't find class id %i\n", BlakodDebugInfo(),o->class_id); return; } /* equal to num_properties is ok, because self = prop 0 */ if (data < 0 || data > class_data->num_properties) { eprintf("[%s] StoreValue can't write to illegal property %i (max %i)\n", BlakodDebugInfo(),data,class_data->num_properties); return; } o->p[data].val.int_val = new_data.int_val; break; default : eprintf("[%s] StoreValue can't identify type %i\n", BlakodDebugInfo(),data_type); break; } }
Bool SetObjectPropertyByName(int object_id,char *prop_name,val_type val) { object_node *o; class_node *c; int property_id; o = GetObjectByID(object_id); if (o == NULL) { eprintf("SetObjectPropertyByName can't find object %i\n",object_id); return False; } c = GetClassByID(o->class_id); if (c == NULL) { eprintf("SetObjectPropertyByName can't find class %i\n", o->class_id); return False; } property_id = GetPropertyIDByName(c,prop_name); if (property_id == INVALID_PROPERTY) { eprintf("SetObjectPropertyByName can't find property %s in class %s (%i)\n", prop_name, c->class_name, c->class_id); return False; } if (o->num_props <= property_id) { eprintf("SetObjectPropertyByName property index/id %i not in object %i class %s (%i)\n", property_id,object_id,c->class_name,c->class_id); return False; } if (o->p[property_id].id != property_id) { eprintf("SetObjectPropertyByName property index/id mismatch %i %i\n", property_id,o->p[property_id].id); return False; } o->p[property_id].val = val; return True; }
Bool LoadTimer(int timer_id,int object_id,char *message_name,int milliseconds) { object_node *o; timer_node *t; message_node *m; o = GetObjectByID(object_id); if (o == NULL) { eprintf("LoadTimer can't find object %i\n",object_id); return False; } m = GetMessageByName(o->class_id,message_name,NULL); if (m == NULL) { eprintf("LoadTimer can't find message name %s\n",message_name); return False; } if (numActiveTimers == num_timer_nodes) ReallocTimerNodes(); t = timer_heap[numActiveTimers]; t->timer_id = timer_id; t->object_id = object_id; t->message_id = m->message_id; t->time = GetMilliCount() + milliseconds; TimerAddNode(t); /* the timers weren't saved in numerical order, but they were * compacted to first x non-negative integers */ if (timer_id >= next_timer_num) next_timer_num = timer_id + 1; return True; }
Bool LoadTimer(int timer_id,int object_id,char *message_name,int milliseconds) { object_node *o; timer_node *t; message_node *m; o = GetObjectByID(object_id); if (o == NULL) { eprintf("LoadTimer can't find object %i\n",object_id); return False; } m = GetMessageByName(o->class_id,message_name,NULL); if (m == NULL) { eprintf("LoadTimer can't find message name %s\n",message_name); return False; } t = (timer_node *)AllocateMemory(MALLOC_ID_TIMER,sizeof(timer_node)); t->timer_id = timer_id; t->object_id = object_id; t->message_id = m->message_id; t->time = GetMilliCount() + milliseconds; AddTimerNode(t); numActiveTimers++; /* the timers weren't saved in numerical order, but they were * compacted to first x non-negative integers */ if (timer_id >= next_timer_num) next_timer_num = timer_id + 1; return True; }
void DeleteBlakodObject(int object_id) { class_node *c; object_node *o; o = GetObjectByID(object_id); if (o == NULL) { eprintf("DeleteBlakodObject can't get an object by ID %i!\n",object_id); return; } c = GetClassByID(o->class_id); if (c == NULL) { eprintf("DeleteBlakodObject can't find class %i\n",o->class_id); return; } /* now remove object */ FreeMemory(MALLOC_ID_OBJECT_PROPERTIES,o->p,sizeof(prop_type)*(1+c->num_properties)); o->deleted = True; }
/* returns the return value of the blakod */ int SendBlakodMessage(int object_id,int message_id,int num_parms,parm_node parms[]) { object_node *o; class_node *c,*propagate_class; message_node *m; val_type message_ret; int prev_interpreting_class; char *prev_bkod; int propagate_depth = 0; prev_bkod = bkod; prev_interpreting_class = kod_stat.interpreting_class; o = GetObjectByID(object_id); if (o == NULL) { bprintf("SendBlakodMessage can't find OBJECT %i\n",object_id); return NIL; } c = GetClassByID(o->class_id); if (c == NULL) { eprintf("SendBlakodMessage OBJECT %i can't find CLASS %i\n", object_id,o->class_id); return NIL; } m = GetMessageByID(c->class_id,message_id,&c); if (m == NULL) { bprintf("SendBlakodMessage CLASS %s (%i) OBJECT %i can't find a handler for MESSAGE %s (%i)\n", c->class_name,c->class_id,object_id,GetNameByID(message_id),message_id); return NIL; } m->called_count++; kod_stat.num_messages++; stack[message_depth].class_id = c->class_id; stack[message_depth].message_id = m->message_id; stack[message_depth].propagate_depth = 0; stack[message_depth].num_parms = num_parms; memcpy(stack[message_depth].parms,parms,num_parms*sizeof(parm_node)); stack[message_depth].bkod_ptr = bkod; if (message_depth > 0) stack[message_depth-1].bkod_ptr = prev_bkod; message_depth++; if (message_depth > kod_stat.message_depth_highest) kod_stat.message_depth_highest = message_depth; if (message_depth >= MAX_DEPTH) { bprintf("SendBlakodMessage sending to CLASS %s (%i), depth is %i, aborted!\n", c->class_name,c->class_id,message_depth); kod_stat.interpreting_class = prev_interpreting_class; message_depth--; bkod = prev_bkod; return NIL; } if (m->trace_session_id != INVALID_ID) { trace_session_id = m->trace_session_id; m->trace_session_id = INVALID_ID; } if (trace_session_id != INVALID_ID) TraceInfo(trace_session_id,c->class_name,m->message_id,num_parms,parms); kod_stat.interpreting_class = c->class_id; bkod = m->handler; propagate_depth = 1; while (InterpretAtMessage(object_id,c,m,num_parms,parms,&message_ret) == RETURN_PROPAGATE) { propagate_class = m->propagate_class; m = m->propagate_message; if (m == NULL) { bprintf("SendBlakodMessage can't propagate MESSAGE %s (%i) in CLASS %s (%i)\n", GetNameByID(message_id),message_id,c->class_name,c->class_id); message_depth -= propagate_depth; kod_stat.interpreting_class = prev_interpreting_class; bkod = prev_bkod; return NIL; } if (propagate_class == NULL) { bprintf("SendBlakodMessage can't find class to propagate to, from " "MESSAGE %s (%i) in CLASS %s (%i)\n",GetNameByID(message_id),message_id,c->class_name,c->class_id); message_depth -= propagate_depth; kod_stat.interpreting_class = prev_interpreting_class; bkod = prev_bkod; return NIL; } c = propagate_class; m->called_count++; if (m->trace_session_id != INVALID_ID) { trace_session_id = m->trace_session_id; m->trace_session_id = INVALID_ID; } if (trace_session_id != INVALID_ID) TraceInfo(trace_session_id,"(propagate)",m->message_id,num_parms,parms); kod_stat.interpreting_class = c->class_id; stack[message_depth-1].bkod_ptr = bkod; stack[message_depth].class_id = c->class_id; stack[message_depth].message_id = m->message_id; stack[message_depth].propagate_depth = propagate_depth; stack[message_depth].num_parms = num_parms; memcpy(stack[message_depth].parms,parms,num_parms*sizeof(parm_node)); stack[message_depth].bkod_ptr = m->handler; message_depth++; propagate_depth++; bkod = m->handler; } message_depth -= propagate_depth; kod_stat.interpreting_class = prev_interpreting_class; bkod = prev_bkod; return message_ret.int_val; }
/* returns the return value of the blakod */ int SendTopLevelBlakodMessage(int object_id,int message_id,int num_parms,parm_node parms[]) { int ret_val = 0; UINT64 start_time = 0; int interp_time = 0; int posts = 0; int accumulated_num_interpreted = 0; if (message_depth != 0) { eprintf("SendTopLevelBlakodMessage called with message_depth %i\n",message_depth); } kod_stat.debugging = ConfigBool(DEBUG_UNINITIALIZED); start_time = GetMilliCount(); kod_stat.num_top_level_messages++; trace_session_id = INVALID_ID; num_interpreted = 0; ret_val = SendBlakodMessage(object_id,message_id,num_parms,parms); while (post_q.next != post_q.last) { posts++; accumulated_num_interpreted += num_interpreted; num_interpreted = 0; if (accumulated_num_interpreted > 10*ConfigInt(BLAKOD_MAX_STATEMENTS)) { bprintf("SendTopLevelBlakodMessage too many instructions in posted followups\n"); dprintf("SendTopLevelBlakodMessage too many instructions in posted followups\n"); dprintf(" OBJECT %i CLASS %s MESSAGE %s (%i) some followups are being aborted\n", object_id, GetClassByID(GetObjectByID(object_id)->class_id)->class_name, GetNameByID(message_id), message_id); break; } /* posted messages' return value is ignored */ SendBlakodMessage(post_q.data[post_q.last].object_id,post_q.data[post_q.last].message_id, post_q.data[post_q.last].num_parms,post_q.data[post_q.last].parms); post_q.last = (post_q.last + 1) % MAX_POST_QUEUE; } interp_time = (int)(GetMilliCount() - start_time); kod_stat.interpreting_time += interp_time; if (interp_time > kod_stat.interpreting_time_highest) { kod_stat.interpreting_time_highest = interp_time; kod_stat.interpreting_time_message_id = message_id; kod_stat.interpreting_time_object_id = object_id; kod_stat.interpreting_time_posts = posts; } if (interp_time > 1000) { kod_stat.interpreting_time_over_second++; kod_stat.interpreting_time_message_id = message_id; kod_stat.interpreting_time_object_id = object_id; kod_stat.interpreting_time_posts = posts; } if (num_interpreted > kod_stat.num_interpreted_highest) kod_stat.num_interpreted_highest = num_interpreted; kod_stat.num_interpreted += num_interpreted; if (kod_stat.num_interpreted > 1000000000L) { kod_stat.num_interpreted -= 1000000000L; kod_stat.billions_interpreted++; } if (message_depth != 0) { eprintf("SendTopLevelBlakodMessage returning with message_depth %i\n",message_depth); } return ret_val; }