Talk:Scripted AI Objects: Difference between revisions

Jump to navigation Jump to search
m
beginning to clean up/structure the discussion - removing the code, because it is being edited as part of the main article already (see my contributions there)
(The reason for not using the SI system is that US aeronautical engineering textbooks more often uses the Foot–pound–second system)
m (beginning to clean up/structure the discussion - removing the code, because it is being edited as part of the main article already (see my contributions there))
Line 57: Line 57:
:: correct, but this is not specific to FDMs - all the autopilot/routing logic that people tend to reinvent in Nasal is significantly overlapping with existing, generic, C++ subsystems that are currently not yet exposed to Nasal. Thus, the main thing is using properties analogous to the actual C++ subsystems for interacting with the FDM/AP and RM components, which will ensure that a reusable and generic design is established, while preparing it for future updates. Otherwise, there will be more and more Nasal code doing what the C++ code is known to be very good at already.--[[User:Hooray|Hooray]] ([[User talk:Hooray|talk]]) 18:00, 29 October 2014 (UTC)
:: correct, but this is not specific to FDMs - all the autopilot/routing logic that people tend to reinvent in Nasal is significantly overlapping with existing, generic, C++ subsystems that are currently not yet exposed to Nasal. Thus, the main thing is using properties analogous to the actual C++ subsystems for interacting with the FDM/AP and RM components, which will ensure that a reusable and generic design is established, while preparing it for future updates. Otherwise, there will be more and more Nasal code doing what the C++ code is known to be very good at already.--[[User:Hooray|Hooray]] ([[User talk:Hooray|talk]]) 18:00, 29 October 2014 (UTC)


== Extending geo.Coord using Sub-classing ==
A suggestion that I have is to expand geo.Coord to hold orientation values.  That would mean we could handle target and have their lat/lon/alt/pitch/roll/heading at our fingertips.
A suggestion that I have is to expand geo.Coord to hold orientation values.  That would mean we could handle target and have their lat/lon/alt/pitch/roll/heading at our fingertips.
[[User:Red_Leader|Red Leader]] ([[User_talk:Red_Leader|Talk]] | [[Special:Contributions/Red_Leader|contribs]]) 17:03, 29 October 2014 (UTC)
[[User:Red_Leader|Red Leader]] ([[User_talk:Red_Leader|Talk]] | [[Special:Contributions/Red_Leader|contribs]]) 17:03, 29 October 2014 (UTC)


: like I said, this is trivial to do using sub-classing, so need to touch geo.nas at all - just create your own geo.Coord sub-class and add helpers for getting the corresponding properties via props.nas --[[User:Hooray|Hooray]] ([[User talk:Hooray|talk]]) 17:55, 29 October 2014 (UTC)
: like I said, this is trivial to do using sub-classing, so need to touch geo.nas at all - just create your own geo.Coord sub-class and add helpers for getting the corresponding properties via props.nas --[[User:Hooray|Hooray]] ([[User talk:Hooray|talk]]) 17:55, 29 October 2014 (UTC)
Progress for tonight as follows.  I've created missile.nas and ai.nas, both in $FGDATA/Nasal, with the code as follows.
ai.nas:
<syntaxhighlight lang="nasal">
# This module enables generic AI objects to be created and updated
var Obj = {
new: func(type = nil, model_path = "Models/Geometry/null.ac"){
if(type == nil){
die("ai.nas: Error! 'type' is not defined");
}
var m = { parents: [Obj] };
var n = props.globals.getNode("models", 1);
for(var i = 0; 1; i += 1){
if(n.getChild("model", i, 0) == nil){
break;
}
}
m.model_node = n.getChild("model", i, 1);
var n = props.globals.getNode("ai/models", 1);
for(var i = 0; 1; i += 1){
if(n.getChild(type, i, 0) == nil){
break;
}
}
m.ai_node = n.getChild(type, i, 1);
m.ai_node.getNode("valid", 1).setBoolValue(1);
m.latN    = m.ai_node.getNode("position/latitude-deg", 1);
m.lonN    = m.ai_node.getNode("position/longitude-deg", 1);
m.altN    = m.ai_node.getNode("position/altitude-ft", 1);
m.hdgN    = m.ai_node.getNode("orientation/true-heading-deg", 1);
m.pitchN  = m.ai_node.getNode("orientation/pitch-deg", 1);
m.rollN  = m.ai_node.getNode("orientation/roll-deg", 1);
m.latN.setDoubleValue(0);
m.lonN.setDoubleValue(0);
m.altN.setDoubleValue(0);
m.hdgN.setDoubleValue(0);
m.pitchN.setDoubleValue(0);
m.rollN.setDoubleValue(0);
m.model_node.getNode("path", 1).setValue(model_path);
m.model_node.getNode("latitude-deg-prop", 1).setValue(m.latN.getPath());
m.model_node.getNode("longitude-deg-prop", 1).setValue(m.lonN.getPath());
m.model_node.getNode("elevation-ft-prop", 1).setValue(m.altN.getPath());
m.model_node.getNode("heading-deg-prop", 1).setValue(m.hdgN.getPath());
m.model_node.getNode("pitch-deg-prop", 1).setValue(m.pitchN.getPath());
m.model_node.getNode("roll-deg-prop", 1).setValue(m.rollN.getPath());
m.model_node.getNode("load", 1).remove();
return m;
},
del: func(){
me.model_node.remove();
me.ai_node.remove();
},
reload_model: func(model_path = "Models/Geometry/null.ac"){
me.model_node.remove();
var n = props.globals.getNode("models", 1);
for(var i = 0; 1; i += 1){
if(n.getChild("model", i, 0) == nil){
break;
}
}
me.model_node = n.getChild("model", i, 1);
me.model_node.getNode("path", 1).setValue(model_path);
me.model_node.getNode("latitude-deg-prop", 1).setValue(me.latN.getPath());
me.model_node.getNode("longitude-deg-prop", 1).setValue(me.lonN.getPath());
me.model_node.getNode("elevation-ft-prop", 1).setValue(me.altN.getPath());
me.model_node.getNode("heading-deg-prop", 1).setValue(me.hdgN.getPath());
me.model_node.getNode("pitch-deg-prop", 1).setValue(me.pitchN.getPath());
me.model_node.getNode("roll-deg-prop", 1).setValue(me.rollN.getPath());
},
set_pos: func(lat, lon, alt = 0, roll = 0, ptch = 0, hdg = 0){
me.latN.setDoubleValue(lat);
me.lonN.setDoubleValue(lon);
me.altN.setDoubleValue(alt);
me.hdgN.setDoubleValue(hdg);
me.pitchN.setDoubleValue(ptch);
me.rollN.setDoubleValue(roll);
}
};
</syntaxhighlight>
missile.nas:
<syntaxhighlight lang="nasal">
var Missile = {
new: func(tgt = "none", src = "none", type = "none"){
if(tgt == "none" or src == "none" or type == "none"){
die("missile.nas: Error! Arguments not defined.");
}
if(isa(tgt, geo.Coord) == 0 or isa(src, geo.Coord) == 0){
die("missile.nas: Error! Arguments not correct.");
}
var m = { parents: [Missile] };
m.tgt = tgt;
m.src = src;
m.type = check_lib(type);
if(m.type == 0){
die("missile.nas: Error! Unsupported missile type.");
}
return m;
},
launch: func(off_x = 0, off_y = 0, off_z = 0){
me.model = ai.Obj.new("missile", "Aircraft/Generic/Stores/AIM-9/AIM-9.xml");
me.update();
},
update: func(){
settimer(func{
me.update();
}, 0);
}
};
var check_lib = func(type){
return type;
}
</syntaxhighlight>


PS Hooary, how do you sub-class an existing Nasal class?
PS Hooary, how do you sub-class an existing Nasal class?
Line 206: Line 71:
I'll try to contribute a little but time, sadly is missing here !
I'll try to contribute a little but time, sadly is missing here !
—[[User:5H1N0B1|5H1N0B1]] ([[User_talk:5H1N0B1|Talk]] | [[Special:Contributions/5H1N0B1|contribs]]) 22:02, 29 October 2014 (UTC)
—[[User:5H1N0B1|5H1N0B1]] ([[User_talk:5H1N0B1|Talk]] | [[Special:Contributions/5H1N0B1|contribs]]) 22:02, 29 October 2014 (UTC)
== Code Feedback ==


* you can probably use forindex/foreach loops in a few places
* you can probably use forindex/foreach loops in a few places
Line 214: Line 81:
* reset/re-init listeners should be registered to deal with sim resets/repositioning
* reset/re-init listeners should be registered to deal with sim resets/repositioning


BTW: good job so far ! We can move the code to the main article for collaboratively editing it there...--[[User:Hooray|Hooray]] ([[User talk:Hooray|talk]]) 21:11, 29 October 2014 (UTC)
 
----
== Things to keep in mind ==
Few things, just to be sure, and to remember :
Few things, just to be sure, and to remember :
* a) The missiles object have to be loaded/created only once the fire order have been triggered.
* a) The missiles object have to be loaded/created only once the fire order have been triggered.

Navigation menu